2023-04-15 08:06:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2023-04-16 09:13:09 +02:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
baseDir = testdataloader.GetBasePath()
|
|
|
|
scriptDir = path.Join(baseDir, "examples", "profile")
|
|
|
|
)
|
|
|
|
|
|
|
|
func nameSave(sym string, input []byte, ctx context.Context) (resource.Result, error) {
|
2023-04-16 09:13:09 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-16 09:25:44 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
st := state.NewState(0)
|
|
|
|
rs := resource.NewFsResource(scriptDir)
|
2023-04-16 09:25:44 +02:00
|
|
|
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()
|
|
|
|
en := engine.NewEngine(cfg, &st, &rs, ca, ctx)
|
|
|
|
|
|
|
|
err := engine.Loop(&en, os.Stdin, os.Stdout, ctx)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "loop exited with error: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|