cmd/hakurei/print: serialise array for ps
Some checks failed
Test / Create distribution (push) Successful in 34s
Test / Sandbox (push) Failing after 58s
Test / Sandbox (race detector) (push) Failing after 1m4s
Test / Hakurei (push) Failing after 1m17s
Test / Hakurei (race detector) (push) Failing after 1m31s
Test / Hpkg (push) Failing after 1m31s
Test / Flake checks (push) Has been skipped

Wanted to do this for a long time, since the key is redundant. This also makes it easier to migrate to the new store interface.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-11-02 16:15:09 +09:00
parent 23ae7822bf
commit 3b004525f9
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 51 additions and 7 deletions

View File

@ -1,9 +1,11 @@
package main
import (
"bytes"
"fmt"
"io"
"log"
"maps"
"slices"
"strconv"
"strings"
@ -177,10 +179,8 @@ func printPs(output io.Writer, now time.Time, s store.Compat, short, flagJSON bo
}
if !short && flagJSON {
es := make(map[string]*hst.State, len(entries))
for id, instance := range entries {
es[id.String()] = instance
}
es := slices.Collect(maps.Values(entries))
slices.SortFunc(es, func(a, b *hst.State) int { return bytes.Compare(a.ID[:], b.ID[:]) })
encodeJSON(log.Fatal, output, short, es)
return
}

View File

@ -1,10 +1,12 @@
package main
import (
"bytes"
"strings"
"testing"
"time"
"hakurei.app/container/check"
"hakurei.app/hst"
"hakurei.app/internal/store"
)
@ -530,8 +532,25 @@ func TestPrintPs(t *testing.T) {
4cf073bd 3405691582 9 (org.chromium.Chromium) 1h2m32s
`},
{"valid short", map[hst.ID]*hst.State{testID: testState}, true, false, "4cf073bd\n"},
{"valid json", map[hst.ID]*hst.State{testID: testState}, false, true, `{
"8e2c76b066dabe574cf073bdb46eb5c1": {
{"valid json", map[hst.ID]*hst.State{testID: testState, (hst.ID)(bytes.Repeat([]byte{0xaa}, len(hst.ID{}))): {
ID: (hst.ID)(bytes.Repeat([]byte{0xaa}, len(hst.ID{}))),
PID: 0xbeef,
ShimPID: 0xcafe,
Config: &hst.Config{
ID: "uk.gensokyo.cat",
Enablements: hst.NewEnablements(hst.EWayland | hst.EPulse),
Identity: 1,
Container: &hst.ContainerConfig{
Shell: check.MustAbs("/bin/sh"),
Home: check.MustAbs("/data/data/uk.gensokyo.cat"),
Path: check.MustAbs("/usr/bin/cat"),
Args: []string{"cat"},
Flags: hst.FUserns,
},
},
Time: time.Unix(0, 0xdeadbeef),
}}, false, true, `[
{
"instance": "8e2c76b066dabe574cf073bdb46eb5c1",
"pid": 3405691582,
"shim_pid": 3735928559,
@ -683,8 +702,33 @@ func TestPrintPs(t *testing.T) {
"share_tmpdir": true
},
"time": "1970-01-01T00:00:00.000000009Z"
},
{
"instance": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"pid": 48879,
"shim_pid": 51966,
"id": "uk.gensokyo.cat",
"enablements": {
"wayland": true,
"pulse": true
},
"identity": 1,
"groups": null,
"container": {
"env": null,
"filesystem": null,
"shell": "/bin/sh",
"home": "/data/data/uk.gensokyo.cat",
"path": "/usr/bin/cat",
"args": [
"cat"
],
"userns": true,
"map_real_uid": false
},
"time": "1970-01-01T09:00:03.735928559+09:00"
}
}
]
`},
{"valid short json", map[hst.ID]*hst.State{testID: testState}, true, true, `["8e2c76b066dabe574cf073bdb46eb5c1"]
`},