vise/examples/profile/main.go

77 lines
1.8 KiB
Go
Raw Normal View History

2023-04-15 08:06:03 +02:00
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
2023-04-15 08:06:03 +02:00
"os"
"path"
testdataloader "github.com/peteole/testdata-loader"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/engine"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
)
2023-04-16 09:51:16 +02:00
const (
USERFLAG_IDENTIFIED = iota + state.FLAG_USERSTART
)
2023-04-15 08:06:03 +02:00
var (
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "examples", "profile")
)
func nameSave(sym string, input []byte, ctx context.Context) (resource.Result, error) {
log.Printf("writing name to file")
2023-04-15 08:06:03 +02:00
fp := path.Join(scriptDir, "myname.txt")
err := ioutil.WriteFile(fp, input, 0600)
return resource.Result{}, err
}
func emailSave(sym string, input []byte, ctx context.Context) (resource.Result, error) {
log.Printf("writing email to file")
fp := path.Join(scriptDir, "myemail.txt")
err := ioutil.WriteFile(fp, input, 0600)
return resource.Result{}, err
}
2023-04-15 08:06:03 +02:00
func main() {
var dir string
var root string
var size uint
var sessionId string
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.Parse()
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
2023-04-16 09:51:16 +02:00
st := state.NewState(1)
2023-04-15 08:06:03 +02:00
rs := resource.NewFsResource(scriptDir)
rs.AddLocalFunc("do_name_save", nameSave)
rs.AddLocalFunc("do_email_save", emailSave)
2023-04-15 08:06:03 +02:00
ca := cache.NewCache()
cfg := engine.Config{
Root: "root",
SessionId: sessionId,
OutputSize: uint32(size),
}
ctx := context.Background()
2023-04-16 09:51:16 +02:00
en, err := engine.NewEngine(cfg, &st, &rs, ca, ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "engine create fail: %v\n", err)
os.Exit(1)
}
2023-04-15 08:06:03 +02:00
2023-04-16 09:51:16 +02:00
err = engine.Loop(&en, os.Stdin, os.Stdout, ctx)
2023-04-15 08:06:03 +02:00
if err != nil {
2023-04-16 09:51:16 +02:00
fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
2023-04-15 08:06:03 +02:00
os.Exit(1)
}
}