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
|
case 1: // instance
|
||||||
name := args[0]
|
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)
|
config = tryPath(msg, name)
|
||||||
|
} else {
|
||||||
|
config = entry.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
if !printShowInstance(os.Stdout, time.Now().UTC(), entry, config, flagShort, flagJSON) {
|
if !printShowInstance(os.Stdout, time.Now().UTC(), entry, config, flagShort, flagJSON) {
|
||||||
os.Exit(1)
|
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.
|
// 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) {
|
func tryIdentifier(msg message.Msg, name string) (entry *hst.State) {
|
||||||
return tryIdentifierEntries(msg, name, func() map[hst.ID]*hst.State {
|
|
||||||
var sc hst.Paths
|
var sc hst.Paths
|
||||||
env.CopyPaths().Copy(&sc, new(outcome.Hsu).MustID(nil))
|
env.CopyPaths().Copy(&sc, new(outcome.Hsu).MustID(nil))
|
||||||
s := store.NewMulti(msg, sc.SharePath)
|
return tryIdentifierStore(msg, name, outcome.NewStore(&sc))
|
||||||
if entries, err := store.Join(s); err != nil {
|
|
||||||
msg.GetLogger().Printf("cannot join store: %v", err) // not fatal
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
return entries
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// tryIdentifierEntries implements tryIdentifier with a custom entries pair getter.
|
// tryIdentifierStore implements tryIdentifier on a custom [store.Store].
|
||||||
func tryIdentifierEntries(
|
func tryIdentifierStore(msg message.Msg, name string, s *store.Store) *hst.State {
|
||||||
msg message.Msg,
|
|
||||||
name string,
|
|
||||||
getEntries func() map[hst.ID]*hst.State,
|
|
||||||
) (config *hst.Config, entry *hst.State) {
|
|
||||||
const (
|
const (
|
||||||
likeShort = 1 << iota
|
likeShort = 1 << iota
|
||||||
likeFull
|
likeFull
|
||||||
@ -116,7 +104,7 @@ func tryIdentifierEntries(
|
|||||||
if c >= 'a' && c <= 'f' {
|
if c >= 'a' && c <= 'f' {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
likely |= likeShort
|
likely |= likeShort
|
||||||
} else if len(name) == hex.EncodedLen(len(hst.ID{})) {
|
} else if len(name) == hex.EncodedLen(len(hst.ID{})) {
|
||||||
@ -124,40 +112,58 @@ func tryIdentifierEntries(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if likely == 0 {
|
if likely == 0 {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
entries := getEntries()
|
|
||||||
if entries == nil {
|
entries, copyError := s.All()
|
||||||
return
|
defer func() {
|
||||||
|
if err := copyError(); err != nil {
|
||||||
|
msg.GetLogger().Println(getMessage("cannot iterate over store:", err))
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case likely&likeShort != 0:
|
case likely&likeShort != 0:
|
||||||
msg.Verbose("argument looks like short identifier")
|
msg.Verbose("argument looks like short identifier")
|
||||||
for id := range entries {
|
for eh := range entries {
|
||||||
v := id.String()
|
if eh.DecodeErr != nil {
|
||||||
if strings.HasPrefix(v[len(hst.ID{}):], name) {
|
msg.Verbose(getMessage("skipping instance:", eh.DecodeErr))
|
||||||
// match, use config from this state entry
|
continue
|
||||||
entry = entries[id]
|
|
||||||
config = entry.Config
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
return &entry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
case likely&likeFull != 0:
|
case likely&likeFull != 0:
|
||||||
var likelyID hst.ID
|
var likelyID hst.ID
|
||||||
if likelyID.UnmarshalText([]byte(name)) != nil {
|
if likelyID.UnmarshalText([]byte(name)) != nil {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
msg.Verbose("argument looks like identifier")
|
msg.Verbose("argument looks like identifier")
|
||||||
if ent, ok := entries[likelyID]; ok {
|
for eh := range entries {
|
||||||
entry = ent
|
if eh.DecodeErr != nil {
|
||||||
config = ent.Config
|
msg.Verbose(getMessage("skipping instance:", eh.DecodeErr))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
return
|
|
||||||
|
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 nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
|
|||||||
@ -1,10 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"hakurei.app/container/check"
|
||||||
"hakurei.app/hst"
|
"hakurei.app/hst"
|
||||||
|
"hakurei.app/internal/store"
|
||||||
"hakurei.app/message"
|
"hakurei.app/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,16 +27,46 @@ func TestShortIdentifier(t *testing.T) {
|
|||||||
|
|
||||||
func TestTryIdentifier(t *testing.T) {
|
func TestTryIdentifier(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
msg := message.NewMsg(nil)
|
msg := message.NewMsg(nil)
|
||||||
id := hst.ID{
|
id := hst.ID{
|
||||||
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
|
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
|
||||||
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
|
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 {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
s string
|
s string
|
||||||
entries map[hst.ID]*hst.State
|
data []hst.State
|
||||||
want *hst.State
|
want *hst.State
|
||||||
}{
|
}{
|
||||||
{"likely entries fault", "ffffffff", nil, nil},
|
{"likely entries fault", "ffffffff", nil, nil},
|
||||||
@ -41,58 +75,37 @@ func TestTryIdentifier(t *testing.T) {
|
|||||||
{"likely short too long", "fffffffffffffffff", nil, nil},
|
{"likely short too long", "fffffffffffffffff", nil, nil},
|
||||||
{"likely short invalid lower", "fffffff\x00", nil, nil},
|
{"likely short invalid lower", "fffffff\x00", nil, nil},
|
||||||
{"likely short invalid higher", "0000000\xff", nil, nil},
|
{"likely short invalid higher", "0000000\xff", nil, nil},
|
||||||
{"short no match", "fedcba98", map[hst.ID]*hst.State{hst.ID{}: nil}, nil},
|
{"short no match", "fedcba98", withBase(), nil},
|
||||||
{"short match", "fedcba98", map[hst.ID]*hst.State{
|
{"short match", "fedcba98", withBase(sampleEntry), &sampleEntry},
|
||||||
hst.ID{}: nil,
|
{"short match longer", "fedcba98765", withBase(sampleEntry), &sampleEntry},
|
||||||
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(),
|
|
||||||
}},
|
|
||||||
|
|
||||||
{"likely long invalid", "0123456789abcdeffedcba987654321\x00", map[hst.ID]*hst.State{}, nil},
|
{"likely long invalid", "0123456789abcdeffedcba987654321\x00", nil, nil},
|
||||||
{"long no match", "0123456789abcdeffedcba9876543210", map[hst.ID]*hst.State{hst.ID{}: nil}, nil},
|
{"long no match", "0123456789abcdeffedcba9876543210", withBase(), nil},
|
||||||
{"long match", "0123456789abcdeffedcba9876543210", map[hst.ID]*hst.State{
|
{"long match", "0123456789abcdeffedcba9876543210", withBase(sampleEntry), &sampleEntry},
|
||||||
hst.ID{}: nil,
|
|
||||||
id: {
|
|
||||||
ID: id,
|
|
||||||
PID: 0xcafebabe,
|
|
||||||
ShimPID: 0xdeadbeef,
|
|
||||||
Config: hst.Template(),
|
|
||||||
},
|
|
||||||
}, &hst.State{
|
|
||||||
ID: id,
|
|
||||||
PID: 0xcafebabe,
|
|
||||||
ShimPID: 0xdeadbeef,
|
|
||||||
Config: hst.Template(),
|
|
||||||
}},
|
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
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.Run(tc.name, func(t *testing.T) {
|
||||||
t.Parallel()
|
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) {
|
if !reflect.DeepEqual(got, tc.want) {
|
||||||
t.Errorf("tryIdentifier: %#v, want %#v", 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)
|
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