cmd/hakurei/parse: use new store interface
Some checks failed
Test / Create distribution (push) Successful in 35s
Test / Sandbox (push) Failing after 1m12s
Test / Hakurei (push) Failing after 1m19s
Test / Sandbox (race detector) (push) Failing after 2m17s
Test / Hakurei (race detector) (push) Failing after 2m23s
Test / Hpkg (push) Successful in 4m6s
Test / Flake checks (push) Has been skipped
Some checks failed
Test / Create distribution (push) Successful in 35s
Test / Sandbox (push) Failing after 1m12s
Test / Hakurei (push) Failing after 1m19s
Test / Sandbox (race detector) (push) Failing after 2m17s
Test / Hakurei (race detector) (push) Failing after 2m23s
Test / Hpkg (push) Successful in 4m6s
Test / Flake checks (push) Has been skipped
This greatly reduces overhead and cleans up the code thanks to the iterator. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
fb397d5047
commit
9a07aa4dce
@ -302,10 +302,15 @@ func buildCommand(ctx context.Context, msg message.Msg, early *earlyHardeningErr
|
||||
|
||||
case 1: // instance
|
||||
name := args[0]
|
||||
config, entry := tryIdentifier(msg, name)
|
||||
if config == nil {
|
||||
|
||||
var config *hst.Config
|
||||
entry := tryIdentifier(msg, name)
|
||||
if entry == nil {
|
||||
config = tryPath(msg, name)
|
||||
} else {
|
||||
config = entry.Config
|
||||
}
|
||||
|
||||
if !printShowInstance(os.Stdout, time.Now().UTC(), entry, config, flagShort, flagJSON) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@ -81,26 +81,14 @@ func shortIdentifierString(s string) string {
|
||||
}
|
||||
|
||||
// tryIdentifier attempts to match [hst.State] from a [hex] representation of [hst.ID] or a prefix of its lower half.
|
||||
func tryIdentifier(msg message.Msg, name string) (config *hst.Config, entry *hst.State) {
|
||||
return tryIdentifierEntries(msg, name, func() map[hst.ID]*hst.State {
|
||||
var sc hst.Paths
|
||||
env.CopyPaths().Copy(&sc, new(outcome.Hsu).MustID(nil))
|
||||
s := store.NewMulti(msg, sc.SharePath)
|
||||
if entries, err := store.Join(s); err != nil {
|
||||
msg.GetLogger().Printf("cannot join store: %v", err) // not fatal
|
||||
return nil
|
||||
} else {
|
||||
return entries
|
||||
}
|
||||
})
|
||||
func tryIdentifier(msg message.Msg, name string) (entry *hst.State) {
|
||||
var sc hst.Paths
|
||||
env.CopyPaths().Copy(&sc, new(outcome.Hsu).MustID(nil))
|
||||
return tryIdentifierStore(msg, name, outcome.NewStore(&sc))
|
||||
}
|
||||
|
||||
// tryIdentifierEntries implements tryIdentifier with a custom entries pair getter.
|
||||
func tryIdentifierEntries(
|
||||
msg message.Msg,
|
||||
name string,
|
||||
getEntries func() map[hst.ID]*hst.State,
|
||||
) (config *hst.Config, entry *hst.State) {
|
||||
// tryIdentifierStore implements tryIdentifier on a custom [store.Store].
|
||||
func tryIdentifierStore(msg message.Msg, name string, s *store.Store) *hst.State {
|
||||
const (
|
||||
likeShort = 1 << iota
|
||||
likeFull
|
||||
@ -116,7 +104,7 @@ func tryIdentifierEntries(
|
||||
if c >= 'a' && c <= 'f' {
|
||||
continue
|
||||
}
|
||||
return
|
||||
return nil
|
||||
}
|
||||
likely |= likeShort
|
||||
} else if len(name) == hex.EncodedLen(len(hst.ID{})) {
|
||||
@ -124,40 +112,58 @@ func tryIdentifierEntries(
|
||||
}
|
||||
|
||||
if likely == 0 {
|
||||
return
|
||||
}
|
||||
entries := getEntries()
|
||||
if entries == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
entries, copyError := s.All()
|
||||
defer func() {
|
||||
if err := copyError(); err != nil {
|
||||
msg.GetLogger().Println(getMessage("cannot iterate over store:", err))
|
||||
}
|
||||
}()
|
||||
|
||||
switch {
|
||||
case likely&likeShort != 0:
|
||||
msg.Verbose("argument looks like short identifier")
|
||||
for id := range entries {
|
||||
v := id.String()
|
||||
if strings.HasPrefix(v[len(hst.ID{}):], name) {
|
||||
// match, use config from this state entry
|
||||
entry = entries[id]
|
||||
config = entry.Config
|
||||
break
|
||||
for eh := range entries {
|
||||
if eh.DecodeErr != nil {
|
||||
msg.Verbose(getMessage("skipping instance:", eh.DecodeErr))
|
||||
continue
|
||||
}
|
||||
|
||||
msg.Verbosef("instance %s skipped", v)
|
||||
if strings.HasPrefix(eh.ID.String()[len(hst.ID{}):], name) {
|
||||
var entry hst.State
|
||||
if _, err := eh.Load(&entry); err != nil {
|
||||
msg.GetLogger().Println(getMessage("cannot load state entry:", err))
|
||||
continue
|
||||
}
|
||||
return &entry
|
||||
}
|
||||
}
|
||||
return
|
||||
return nil
|
||||
|
||||
case likely&likeFull != 0:
|
||||
var likelyID hst.ID
|
||||
if likelyID.UnmarshalText([]byte(name)) != nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
msg.Verbose("argument looks like identifier")
|
||||
if ent, ok := entries[likelyID]; ok {
|
||||
entry = ent
|
||||
config = ent.Config
|
||||
for eh := range entries {
|
||||
if eh.DecodeErr != nil {
|
||||
msg.Verbose(getMessage("skipping instance:", eh.DecodeErr))
|
||||
continue
|
||||
}
|
||||
|
||||
if eh.ID == likelyID {
|
||||
var entry hst.State
|
||||
if _, err := eh.Load(&entry); err != nil {
|
||||
msg.GetLogger().Println(getMessage("cannot load state entry:", err))
|
||||
continue
|
||||
}
|
||||
return &entry
|
||||
}
|
||||
}
|
||||
return
|
||||
return nil
|
||||
|
||||
default:
|
||||
panic("unreachable")
|
||||
|
||||
@ -1,10 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hakurei.app/container/check"
|
||||
"hakurei.app/hst"
|
||||
"hakurei.app/internal/store"
|
||||
"hakurei.app/message"
|
||||
)
|
||||
|
||||
@ -23,17 +27,47 @@ func TestShortIdentifier(t *testing.T) {
|
||||
|
||||
func TestTryIdentifier(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
msg := message.NewMsg(nil)
|
||||
id := hst.ID{
|
||||
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
|
||||
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
|
||||
}
|
||||
withBase := func(extra ...hst.State) []hst.State {
|
||||
return append([]hst.State{
|
||||
{ID: (hst.ID)(bytes.Repeat([]byte{0xaa}, len(hst.ID{}))), PID: 0xbeef, ShimPID: 0xcafe, Config: hst.Template(), Time: time.Unix(0, 0xdeadbeef0)},
|
||||
{ID: (hst.ID)(bytes.Repeat([]byte{0xab}, len(hst.ID{}))), PID: 0x1beef, ShimPID: 0x1cafe, Config: hst.Template(), Time: time.Unix(0, 0xdeadbeef1)},
|
||||
{ID: (hst.ID)(bytes.Repeat([]byte{0xf0}, len(hst.ID{}))), PID: 0x2beef, ShimPID: 0x2cafe, Config: hst.Template(), Time: time.Unix(0, 0xdeadbeef2)},
|
||||
|
||||
{ID: (hst.ID)(bytes.Repeat([]byte{0xfe}, len(hst.ID{}))), PID: 0xbed, ShimPID: 0xfff, Config: func() *hst.Config {
|
||||
template := hst.Template()
|
||||
template.Identity = hst.IdentityMax
|
||||
return template
|
||||
}(), Time: time.Unix(0, 0xcafebabe0)},
|
||||
{ID: (hst.ID)(bytes.Repeat([]byte{0xfc}, len(hst.ID{}))), PID: 0x1bed, ShimPID: 0x1fff, Config: func() *hst.Config {
|
||||
template := hst.Template()
|
||||
template.Identity = 0xfc
|
||||
return template
|
||||
}(), Time: time.Unix(0, 0xcafebabe1)},
|
||||
{ID: (hst.ID)(bytes.Repeat([]byte{0xce}, len(hst.ID{}))), PID: 0x2bed, ShimPID: 0x2fff, Config: func() *hst.Config {
|
||||
template := hst.Template()
|
||||
template.Identity = 0xce
|
||||
return template
|
||||
}(), Time: time.Unix(0, 0xcafebabe2)},
|
||||
}, extra...)
|
||||
}
|
||||
sampleEntry := hst.State{
|
||||
ID: id,
|
||||
PID: 0xcafebabe,
|
||||
ShimPID: 0xdeadbeef,
|
||||
Config: hst.Template(),
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
s string
|
||||
entries map[hst.ID]*hst.State
|
||||
want *hst.State
|
||||
name string
|
||||
s string
|
||||
data []hst.State
|
||||
want *hst.State
|
||||
}{
|
||||
{"likely entries fault", "ffffffff", nil, nil},
|
||||
|
||||
@ -41,58 +75,37 @@ func TestTryIdentifier(t *testing.T) {
|
||||
{"likely short too long", "fffffffffffffffff", nil, nil},
|
||||
{"likely short invalid lower", "fffffff\x00", nil, nil},
|
||||
{"likely short invalid higher", "0000000\xff", nil, nil},
|
||||
{"short no match", "fedcba98", map[hst.ID]*hst.State{hst.ID{}: nil}, nil},
|
||||
{"short match", "fedcba98", map[hst.ID]*hst.State{
|
||||
hst.ID{}: nil,
|
||||
id: {
|
||||
ID: id,
|
||||
PID: 0xcafebabe,
|
||||
ShimPID: 0xdeadbeef,
|
||||
Config: hst.Template(),
|
||||
},
|
||||
}, &hst.State{
|
||||
ID: id,
|
||||
PID: 0xcafebabe,
|
||||
ShimPID: 0xdeadbeef,
|
||||
Config: hst.Template(),
|
||||
}},
|
||||
{"short match longer", "fedcba98765", map[hst.ID]*hst.State{
|
||||
hst.ID{}: nil,
|
||||
id: {
|
||||
ID: id,
|
||||
PID: 0xcafebabe,
|
||||
ShimPID: 0xdeadbeef,
|
||||
Config: hst.Template(),
|
||||
},
|
||||
}, &hst.State{
|
||||
ID: id,
|
||||
PID: 0xcafebabe,
|
||||
ShimPID: 0xdeadbeef,
|
||||
Config: hst.Template(),
|
||||
}},
|
||||
{"short no match", "fedcba98", withBase(), nil},
|
||||
{"short match", "fedcba98", withBase(sampleEntry), &sampleEntry},
|
||||
{"short match longer", "fedcba98765", withBase(sampleEntry), &sampleEntry},
|
||||
|
||||
{"likely long invalid", "0123456789abcdeffedcba987654321\x00", map[hst.ID]*hst.State{}, nil},
|
||||
{"long no match", "0123456789abcdeffedcba9876543210", map[hst.ID]*hst.State{hst.ID{}: nil}, nil},
|
||||
{"long match", "0123456789abcdeffedcba9876543210", map[hst.ID]*hst.State{
|
||||
hst.ID{}: nil,
|
||||
id: {
|
||||
ID: id,
|
||||
PID: 0xcafebabe,
|
||||
ShimPID: 0xdeadbeef,
|
||||
Config: hst.Template(),
|
||||
},
|
||||
}, &hst.State{
|
||||
ID: id,
|
||||
PID: 0xcafebabe,
|
||||
ShimPID: 0xdeadbeef,
|
||||
Config: hst.Template(),
|
||||
}},
|
||||
{"likely long invalid", "0123456789abcdeffedcba987654321\x00", nil, nil},
|
||||
{"long no match", "0123456789abcdeffedcba9876543210", withBase(), nil},
|
||||
{"long match", "0123456789abcdeffedcba9876543210", withBase(sampleEntry), &sampleEntry},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
s := store.New(check.MustAbs(t.TempDir()).Append("store"))
|
||||
for i := range tc.data {
|
||||
if h, err := s.Handle(tc.data[i].Identity); err != nil {
|
||||
t.Fatalf("Handle: error = %v", err)
|
||||
} else {
|
||||
var unlock func()
|
||||
if unlock, err = h.Lock(); err != nil {
|
||||
t.Fatalf("Lock: error = %v", err)
|
||||
}
|
||||
_, err = h.Save(&tc.data[i])
|
||||
unlock()
|
||||
if err != nil {
|
||||
t.Fatalf("Save: error = %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// store must not be written to beyond this point
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, got := tryIdentifierEntries(msg, tc.s, func() map[hst.ID]*hst.State { return tc.entries })
|
||||
got := tryIdentifierStore(msg, tc.s, s)
|
||||
if !reflect.DeepEqual(got, tc.want) {
|
||||
t.Errorf("tryIdentifier: %#v, want %#v", got, tc.want)
|
||||
}
|
||||
|
||||
@ -287,3 +287,11 @@ func mustPrintln(output io.Writer, a ...any) {
|
||||
log.Fatalf("cannot print: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// getMessage returns a [message.Error] message if available, or err prefixed with fallback otherwise.
|
||||
func getMessage(fallback string, err error) string {
|
||||
if m, ok := message.GetMessage(err); ok {
|
||||
return m
|
||||
}
|
||||
return fmt.Sprintln(fallback, err)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user