From dd94818f20c7a37bdd7c6129551759db1b09b65e Mon Sep 17 00:00:00 2001 From: Ophestra Date: Thu, 23 Oct 2025 22:51:10 +0900 Subject: hst/instance: define instance state This is now part of the hst API. This change also improves identifier generation and serialisation. Signed-off-by: Ophestra --- cmd/hakurei/parse.go | 2 +- cmd/hakurei/print.go | 8 +++--- cmd/hakurei/print_test.go | 68 ++++++++++++----------------------------------- 3 files changed, 22 insertions(+), 56 deletions(-) (limited to 'cmd') diff --git a/cmd/hakurei/parse.go b/cmd/hakurei/parse.go index 4a24c90a..42eaf737 100644 --- a/cmd/hakurei/parse.go +++ b/cmd/hakurei/parse.go @@ -61,7 +61,7 @@ func tryFd(msg message.Msg, name string) io.ReadCloser { } } -func tryShort(msg message.Msg, name string) (config *hst.Config, entry *state.State) { +func tryShort(msg message.Msg, name string) (config *hst.Config, entry *hst.State) { likePrefix := false if len(name) <= 32 { likePrefix = true diff --git a/cmd/hakurei/print.go b/cmd/hakurei/print.go index 8f6e7139..c28146ad 100644 --- a/cmd/hakurei/print.go +++ b/cmd/hakurei/print.go @@ -41,7 +41,7 @@ func printShowSystem(output io.Writer, short, flagJSON bool) { // printShowInstance writes a representation of [state.State] or [hst.Config] to output. func printShowInstance( output io.Writer, now time.Time, - instance *state.State, config *hst.Config, + instance *hst.State, config *hst.Config, short, flagJSON bool) (valid bool) { valid = true @@ -168,7 +168,7 @@ func printShowInstance( // printPs writes a representation of active instances to output. func printPs(output io.Writer, now time.Time, s state.Store, short, flagJSON bool) { - var entries state.Entries + var entries map[hst.ID]*hst.State if e, err := state.Join(s); err != nil { log.Fatalf("cannot join store: %v", err) } else { @@ -179,7 +179,7 @@ func printPs(output io.Writer, now time.Time, s state.Store, short, flagJSON boo } if !short && flagJSON { - es := make(map[string]*state.State, len(entries)) + es := make(map[string]*hst.State, len(entries)) for id, instance := range entries { es[id.String()] = instance } @@ -249,7 +249,7 @@ func printPs(output io.Writer, now time.Time, s state.Store, short, flagJSON boo // expandedStateEntry stores [state.State] alongside a string representation of its [state.ID]. type expandedStateEntry struct { s string - *state.State + *hst.State } // newPrinter returns a configured, wrapped [tabwriter.Writer]. diff --git a/cmd/hakurei/print_test.go b/cmd/hakurei/print_test.go index bc65bac3..da5c815a 100644 --- a/cmd/hakurei/print_test.go +++ b/cmd/hakurei/print_test.go @@ -10,13 +10,13 @@ import ( ) var ( - testID = state.ID{ + testID = hst.ID{ 0x8e, 0x2c, 0x76, 0xb0, 0x66, 0xda, 0xbe, 0x57, 0x4c, 0xf0, 0x73, 0xbd, 0xb4, 0x6e, 0xb5, 0xc1, } - testState = &state.State{ + testState = &hst.State{ ID: testID, PID: 0xDEADBEEF, Config: hst.Template(), @@ -31,7 +31,7 @@ func TestPrintShowInstance(t *testing.T) { testCases := []struct { name string - instance *state.State + instance *hst.State config *hst.Config short, json bool want string @@ -185,24 +185,7 @@ App {"json nil", nil, nil, false, true, `null `, true}, {"json instance", testState, nil, false, true, `{ - "instance": [ - 142, - 44, - 118, - 176, - 102, - 218, - 190, - 87, - 76, - 240, - 115, - 189, - 180, - 110, - 181, - 193 - ], + "instance": "8e2c76b066dabe574cf073bdb46eb5c1", "pid": 3735928559, "config": { "id": "org.chromium.Chromium", @@ -530,43 +513,26 @@ func TestPrintPs(t *testing.T) { testCases := []struct { name string - entries state.Entries + entries map[hst.ID]*hst.State short, json bool want string }{ - {"no entries", make(state.Entries), false, false, " Instance PID Application Uptime\n"}, - {"no entries short", make(state.Entries), true, false, ""}, - {"nil instance", state.Entries{testID: nil}, false, false, " Instance PID Application Uptime\n"}, - {"state corruption", state.Entries{state.ID{}: testState}, false, false, " Instance PID Application Uptime\n"}, + {"no entries", make(map[hst.ID]*hst.State), false, false, " Instance PID Application Uptime\n"}, + {"no entries short", make(map[hst.ID]*hst.State), true, false, ""}, + {"nil instance", map[hst.ID]*hst.State{testID: nil}, false, false, " Instance PID Application Uptime\n"}, + {"state corruption", map[hst.ID]*hst.State{hst.ID{}: testState}, false, false, " Instance PID Application Uptime\n"}, - {"valid pd", state.Entries{testID: &state.State{ID: testID, PID: 1 << 8, Config: new(hst.Config), Time: testAppTime}}, false, false, ` Instance PID Application Uptime + {"valid pd", map[hst.ID]*hst.State{testID: {ID: testID, PID: 1 << 8, Config: new(hst.Config), Time: testAppTime}}, false, false, ` Instance PID Application Uptime 8e2c76b0 256 0 (app.hakurei.8e2c76b0) 1h2m32s `}, - {"valid", state.Entries{testID: testState}, false, false, ` Instance PID Application Uptime + {"valid", map[hst.ID]*hst.State{testID: testState}, false, false, ` Instance PID Application Uptime 8e2c76b0 3735928559 9 (org.chromium.Chromium) 1h2m32s `}, - {"valid short", state.Entries{testID: testState}, true, false, "8e2c76b0\n"}, - {"valid json", state.Entries{testID: testState}, false, true, `{ + {"valid short", map[hst.ID]*hst.State{testID: testState}, true, false, "8e2c76b0\n"}, + {"valid json", map[hst.ID]*hst.State{testID: testState}, false, true, `{ "8e2c76b066dabe574cf073bdb46eb5c1": { - "instance": [ - 142, - 44, - 118, - 176, - 102, - 218, - 190, - 87, - 76, - 240, - 115, - 189, - 180, - 110, - 181, - 193 - ], + "instance": "8e2c76b066dabe574cf073bdb46eb5c1", "pid": 3735928559, "config": { "id": "org.chromium.Chromium", @@ -721,7 +687,7 @@ func TestPrintPs(t *testing.T) { } } `}, - {"valid short json", state.Entries{testID: testState}, true, true, `["8e2c76b066dabe574cf073bdb46eb5c1"] + {"valid short json", map[hst.ID]*hst.State{testID: testState}, true, true, `["8e2c76b066dabe574cf073bdb46eb5c1"] `}, } @@ -741,9 +707,9 @@ func TestPrintPs(t *testing.T) { } // stubStore implements [state.Store] and returns test samples via [state.Joiner]. -type stubStore state.Entries +type stubStore map[hst.ID]*hst.State -func (s stubStore) Join() (state.Entries, error) { return state.Entries(s), nil } +func (s stubStore) Join() (map[hst.ID]*hst.State, error) { return s, nil } func (s stubStore) Do(int, func(c state.Cursor)) (bool, error) { panic("unreachable") } func (s stubStore) List() ([]int, error) { panic("unreachable") } func (s stubStore) Close() error { return nil } -- cgit v1.3.1