ussd/cmd/main.go

114 lines
2.6 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"os"
"path"
"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"
)
const (
USERFLAG_LANGUAGE_SET = iota + state.FLAG_USERSTART
)
type fsData struct {
path string
}
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
}
var (
scriptDir = path.Join("services", "registration")
)
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")
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()
st := state.NewState(3)
rfs := resource.NewFsResource(scriptDir)
ca := cache.NewCache()
cfg := engine.Config{
Root: "registration",
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,
}
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)
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)
}
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)
}
}