From 02bebe605a56c631df2ab10c53954fb38e5a9188 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 26 Mar 2025 18:17:54 +0000 Subject: [PATCH] Add reset tool --- devtools/admin/main.go | 17 +++++++++ go.mod | 2 +- go.sum | 4 +- internal/cmd/cmd.go | 85 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 3 deletions(-) diff --git a/devtools/admin/main.go b/devtools/admin/main.go index c3ce57f..7d42584 100644 --- a/devtools/admin/main.go +++ b/devtools/admin/main.go @@ -8,6 +8,7 @@ import ( "path" "git.defalsify.org/vise.git/logging" + "git.defalsify.org/vise.git/engine" "git.grassecon.net/grassrootseconomics/sarafu-vise/config" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application" @@ -18,6 +19,7 @@ import ( var ( logg = logging.NewVanilla().WithContextKey("SessionId") scriptDir = path.Join("services", "registration") + menuSeparator = ": " ) func main() { @@ -25,6 +27,8 @@ func main() { override := config.NewOverride() var sessionId string + var size uint + var engineDebug bool flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&override.DbConn, "c", "?", "default connection string (replaces all unspecified strings)") @@ -32,6 +36,8 @@ func main() { flag.StringVar(&override.UserConn, "userdata", "?", "userdata store connection string") flag.StringVar(&override.StateConn, "state", "?", "state store connection string") + flag.BoolVar(&engineDebug, "d", false, "use engine debug output") + flag.UintVar(&size, "s", 160, "max size of output") flag.Parse() config.Apply(override) @@ -50,7 +56,18 @@ func main() { os.Exit(1) } + cfg := engine.Config{ + Root: "root", + SessionId: sessionId, + OutputSize: uint32(size), + FlagCount: uint32(128), + MenuSeparator: menuSeparator, + EngineDebug: engineDebug, + ResetOnEmptyInput: true, + } + x := cmd.NewCmd(sessionId, flagParser) + x = x.WithEngine(cfg) err = x.Parse(flag.Args()) if err != nil { fmt.Fprintf(os.Stderr, "cmd parse fail: %v\n", err) diff --git a/go.mod b/go.mod index 1b06b27..e6bd876 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.grassecon.net/grassrootseconomics/sarafu-vise go 1.23.4 require ( - git.defalsify.org/vise.git v0.3.1 + git.defalsify.org/vise.git v0.3.2-0.20250326034808-b9c2294cbf1a git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250310093912-8145b4bd004b git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2 diff --git a/go.sum b/go.sum index 19c67fe..f6c0ec5 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.defalsify.org/vise.git v0.3.1 h1:A6FhMcur09ft/JzUPGXR+KpA17fltfeBnasyvLMZmq4= -git.defalsify.org/vise.git v0.3.1/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= +git.defalsify.org/vise.git v0.3.2-0.20250326034808-b9c2294cbf1a h1:5uLBUZC6armYgBPkuNEsQPtqT3qZxRfNP4HUdkhjm4I= +git.defalsify.org/vise.git v0.3.2-0.20250326034808-b9c2294cbf1a/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d h1:5mzLas+jxTUtusOKx4XvU+n2QvrV/mH17MnJRy46siQ= git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60= git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250310093912-8145b4bd004b h1:xiTpaqWWoF5qcnarY/9ZkT6aVdnKwqztb2gzIahJn4w= diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index ac286c1..2fa0477 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -5,6 +5,10 @@ import ( "fmt" "git.defalsify.org/vise.git/logging" + "git.defalsify.org/vise.git/engine" + "git.defalsify.org/vise.git/persist" + "git.defalsify.org/vise.git/resource" + "git.defalsify.org/vise.git/state" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application" "git.grassecon.net/grassrootseconomics/visedriver/storage" ) @@ -20,6 +24,8 @@ type Cmd struct { cmd int enable bool exec func(ctx context.Context, ss storage.StorageService) error + engineConfig *engine.Config + st *state.State } func NewCmd(sessionId string, flagParser *application.FlagManager) *Cmd { @@ -29,10 +35,71 @@ func NewCmd(sessionId string, flagParser *application.FlagManager) *Cmd { } } +func (c *Cmd) WithEngine(engineConfig engine.Config) *Cmd { + c.engineConfig = &engineConfig + return c +} + func (c *Cmd) Exec(ctx context.Context, ss storage.StorageService) error { return c.exec(ctx, ss) } +func (c *Cmd) engine(ctx context.Context, rs resource.Resource, pe *persist.Persister) (engine.Engine, error) { + if c.engineConfig == nil { + return nil, fmt.Errorf("engine config missing") + } + en := engine.NewEngine(*c.engineConfig, rs) + + st := pe.GetState() + if st == nil { + return nil, fmt.Errorf("persister state fail") + } + en = en.WithState(st) + st.UseDebug() + ca := pe.GetMemory() + if ca == nil { + return nil, fmt.Errorf("persister cache fail") + } + en = en.WithMemory(ca) + logg.DebugCtxf(ctx, "state loaded", "state", st) + return en, nil +} + +func (c *Cmd) execReset(ctx context.Context, ss storage.StorageService) error { + pe, err := ss.GetPersister(ctx) + if err != nil { + return fmt.Errorf("get persister error: %v", err) + } + rs, err := ss.GetResource(ctx) + if err != nil { + return fmt.Errorf("get resource error: %v", err) + } + dbResource, ok := rs.(*resource.DbResource) + if !ok { + return fmt.Errorf("get dbresource error: %v", err) + } + err = pe.Load(c.engineConfig.SessionId) + if err != nil { + return fmt.Errorf("persister load error: %v", err) + } + en, err := c.engine(ctx, dbResource, pe) + if err != nil { + return err + } + _, err = en.(*engine.DefaultEngine).Reset(ctx, false) + if err != nil { + return err + } + st := pe.GetState() + logg.DebugCtxf(ctx, "state after reset", "state", st) + + err = pe.Save(c.engineConfig.SessionId) + if err != nil { + return err + } + return nil +} + func (c *Cmd) execAdmin(ctx context.Context, ss storage.StorageService) error { pe, err := ss.GetPersister(ctx) if err != nil { @@ -76,6 +143,15 @@ func (c *Cmd) parseCmdAdmin(cmd string, param string, more []string) (bool, erro return false, nil } +func (c *Cmd) parseCmdReset(cmd string, param string, more []string) (bool, error) { + if cmd == "reset" { + c.enable = false + c.exec = c.execReset + return true, nil + } + return false, nil +} + func (c *Cmd) Parse(args []string) error { if len(args) < 2 { return fmt.Errorf("Wrong number of arguments: %v", args) @@ -92,5 +168,14 @@ func (c *Cmd) Parse(args []string) error { return nil } + r, err = c.parseCmdReset(cmd, param, args) + if err != nil { + return err + } + if r { + return nil + } + + return fmt.Errorf("unknown subcommand: %s", cmd) }