ussd/cmd/main.go

114 lines
2.6 KiB
Go
Raw Normal View History

2024-08-15 10:12:58 +02:00
package main
import (
"context"
"flag"
"fmt"
"os"
"path"
2024-08-15 12:49:36 +02:00
"git.defalsify.org/vise.git/cache"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/persist"
2024-08-15 10:12:58 +02:00
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state"
)
const (
USERFLAG_LANGUAGE_SET = iota + state.FLAG_USERSTART
)
type fsData struct {
2024-08-15 12:49:36 +02:00
path string
2024-08-15 10:12:58 +02:00
}
func (fsd *fsData) SetLanguageSelected(ctx context.Context, sym string, input []byte) (resource.Result, error) {
inputStr := string(input)
res := resource.Result{}
switch inputStr {
case "0":
res.FlagSet = []uint32{state.FLAG_LANG}
res.Content = "eng"
case "1":
res.FlagSet = []uint32{state.FLAG_LANG}
res.Content = "swa"
default:
}
return res, nil
}
2024-08-15 12:49:36 +02:00
var (
scriptDir = path.Join("services", "registration")
)
2024-08-15 10:12:58 +02:00
func main() {
var dir string
var root string
var size uint
var sessionId string
flag.StringVar(&dir, "d", ".", "resource dir to read from")
flag.UintVar(&size, "s", 0, "max size of output")
flag.StringVar(&root, "root", "registration", "entry point symbol")
2024-08-15 10:12:58 +02:00
flag.StringVar(&sessionId, "session-id", "default", "session id")
flag.Parse()
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
ctx := context.Background()
2024-08-15 12:49:36 +02:00
st := state.NewState(3)
rfs := resource.NewFsResource(scriptDir)
ca := cache.NewCache()
cfg := engine.Config{
Root: "registration",
2024-08-15 12:49:36 +02:00
SessionId: sessionId,
}
2024-08-15 10:12:58 +02:00
2024-08-15 12:49:36 +02:00
dp := path.Join(scriptDir, ".state")
err := os.MkdirAll(dp, 0700)
2024-08-15 10:12:58 +02:00
if err != nil {
2024-08-15 12:49:36 +02:00
fmt.Fprintf(os.Stderr, "state dir create exited with error: %v\n", err)
2024-08-15 10:12:58 +02:00
os.Exit(1)
}
2024-08-15 12:49:36 +02:00
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,
}
rfs.AddLocalFunc("select_language", fs.SetLanguageSelected)
2024-08-15 10:12:58 +02:00
cont, err := en.Init(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err)
os.Exit(1)
}
if !cont {
_, err = en.WriteResult(ctx, os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "dead init write error: %v\n", err)
os.Exit(1)
}
2024-08-15 12:49:36 +02:00
err = en.Finish()
if err != nil {
fmt.Fprintf(os.Stderr, "engine finish error: %v\n", err)
os.Exit(1)
}
2024-08-15 10:12:58 +02:00
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)
}
}