From fb7b35af7461faaaaca997eaafda0890c8f96ed7 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 15 Aug 2024 13:49:36 +0300 Subject: [PATCH] directly use the persistedEngine --- .gitignore | 4 ++- cmd/main.go | 58 ++++++++++++++++++++++++--------- go-vise | 1 + services/registration/root | 1 + services/registration/root.vis | 5 +++ services/registration/root_swa | 1 + services/registration/terms.vis | 2 ++ 7 files changed, 55 insertions(+), 17 deletions(-) create mode 160000 go-vise create mode 100644 services/registration/root create mode 100644 services/registration/root.vis create mode 100644 services/registration/root_swa diff --git a/.gitignore b/.gitignore index ddd3da6..54ad3ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ **/*.env covprofile go.work* -services/registration/*.bin \ No newline at end of file +**/*/*.bin +**/*/.state/ +cmd/.state/ diff --git a/cmd/main.go b/cmd/main.go index 3ba7865..d9b0692 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,8 +6,10 @@ import ( "fmt" "os" "path" - "git.defalsify.org/vise.git/engine" + "git.defalsify.org/vise.git/cache" + "git.defalsify.org/vise.git/engine" + "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/state" ) @@ -17,7 +19,7 @@ const ( ) type fsData struct { - path string + path string } func (fsd *fsData) SetLanguageSelected(ctx context.Context, sym string, input []byte) (resource.Result, error) { @@ -36,33 +38,54 @@ func (fsd *fsData) SetLanguageSelected(ctx context.Context, sym string, input [] return res, nil } +var ( + scriptDir = path.Join("services", "registration") +) + func main() { var dir string var root string var size uint var sessionId string - var persist bool flag.StringVar(&dir, "d", ".", "resource dir to read from") flag.UintVar(&size, "s", 0, "max size of output") flag.StringVar(&root, "root", "root", "entry point symbol") flag.StringVar(&sessionId, "session-id", "default", "session id") - flag.BoolVar(&persist, "persist", false, "use state persistence") flag.Parse() fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) - fp := path.Join(dir, sessionId) + ctx := context.Background() + st := state.NewState(3) + rfs := resource.NewFsResource(scriptDir) + ca := cache.NewCache() + cfg := engine.Config{ + Root: "root", + SessionId: sessionId, + } + + dp := path.Join(scriptDir, ".state") + err := os.MkdirAll(dp, 0700) + if err != nil { + fmt.Fprintf(os.Stderr, "state dir create exited with error: %v\n", err) + os.Exit(1) + } + pr := persist.NewFsPersister(dp) + en, err := engine.NewPersistedEngine(ctx, cfg, pr, rfs) + if err != nil { + pr = pr.WithContent(&st, ca) + err = pr.Save(cfg.SessionId) + en, err = engine.NewPersistedEngine(ctx, cfg, pr, rfs) + if err != nil { + fmt.Fprintf(os.Stderr, "engine create exited with error: %v\n", err) + os.Exit(1) + } + } + + fp := path.Join(dp, sessionId) fs := &fsData{ path: fp, } - - ctx := context.Background() - en, rs, err := engine.NewSizedEnginee(dir, uint32(size), persist, &sessionId) - rs.AddLocalFunc("select_language", fs.SetLanguageSelected) - - if err != nil { - fmt.Fprintf(os.Stderr, "engine create error: %v", err) - os.Exit(1) - } + rfs.AddLocalFunc("select_language", fs.SetLanguageSelected) cont, err := en.Init(ctx) if err != nil { fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err) @@ -74,14 +97,17 @@ func main() { fmt.Fprintf(os.Stderr, "dead init write error: %v\n", err) os.Exit(1) } + err = en.Finish() + if err != nil { + fmt.Fprintf(os.Stderr, "engine finish error: %v\n", err) + os.Exit(1) + } os.Stdout.Write([]byte{0x0a}) os.Exit(0) } - err = engine.Loop(ctx, en, os.Stdin, os.Stdout) if err != nil { fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err) os.Exit(1) } - } diff --git a/go-vise b/go-vise new file mode 160000 index 0000000..ef9a4c3 --- /dev/null +++ b/go-vise @@ -0,0 +1 @@ +Subproject commit ef9a4c3073e7e878846693d4aa7fd0cf1cc977a7 diff --git a/services/registration/root b/services/registration/root new file mode 100644 index 0000000..c6dabe8 --- /dev/null +++ b/services/registration/root @@ -0,0 +1 @@ +Welcome to Sarafu Network! diff --git a/services/registration/root.vis b/services/registration/root.vis new file mode 100644 index 0000000..1017a0f --- /dev/null +++ b/services/registration/root.vis @@ -0,0 +1,5 @@ +MOUT english 0 +MOUT kiswahili 1 +HALT +INCMP terms 0 +INCMP terms 1 diff --git a/services/registration/root_swa b/services/registration/root_swa new file mode 100644 index 0000000..e5ab103 --- /dev/null +++ b/services/registration/root_swa @@ -0,0 +1 @@ +Karibu Sarafu Network! \ No newline at end of file diff --git a/services/registration/terms.vis b/services/registration/terms.vis index 59b1e06..572e7e8 100644 --- a/services/registration/terms.vis +++ b/services/registration/terms.vis @@ -1,2 +1,4 @@ +LOAD select_language 0 +RELOAD select_language MOUT yes 0 MOUT no 1