main.go (2016B)
1 // Example: Input checker. 2 package main 3 4 import ( 5 "context" 6 "flag" 7 "fmt" 8 "os" 9 "path" 10 11 testdataloader "github.com/peteole/testdata-loader" 12 13 "git.defalsify.org/vise.git/engine" 14 "git.defalsify.org/vise.git/resource" 15 "git.defalsify.org/vise.git/state" 16 fsdb "git.defalsify.org/vise.git/db/fs" 17 ) 18 19 var ( 20 baseDir = testdataloader.GetBasePath() 21 scriptDir = path.Join(baseDir, "examples", "validate") 22 emptyResult = resource.Result{} 23 ) 24 25 const ( 26 USERFLAG_HAVESOMETHING = state.FLAG_USERSTART 27 ) 28 29 type verifyResource struct { 30 *resource.DbResource 31 st *state.State 32 } 33 34 func(vr *verifyResource) verify(ctx context.Context, sym string, input []byte) (resource.Result, error) { 35 var err error 36 if string(input) == "something" { 37 vr.st.SetFlag(USERFLAG_HAVESOMETHING) 38 } 39 return resource.Result{ 40 Content: "", 41 }, err 42 } 43 44 func(vr *verifyResource) again(ctx context.Context, sym string, input []byte) (resource.Result, error) { 45 vr.st.ResetFlag(USERFLAG_HAVESOMETHING) 46 return resource.Result{}, nil 47 } 48 49 func main() { 50 var root string 51 var size uint 52 var sessionId string 53 flag.UintVar(&size, "s", 0, "max size of output") 54 flag.StringVar(&root, "root", "root", "entry point symbol") 55 flag.StringVar(&sessionId, "session-id", "default", "session id") 56 flag.Parse() 57 fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir) 58 59 ctx := context.Background() 60 ctx = context.WithValue(ctx, "SessionId", sessionId) 61 st := state.NewState(1) 62 store := fsdb.NewFsDb() 63 err := store.Connect(ctx, scriptDir) 64 if err != nil { 65 panic(err) 66 } 67 rsf := resource.NewDbResource(store) 68 rs := verifyResource{rsf, st} 69 rs.AddLocalFunc("verifyinput", rs.verify) 70 rs.AddLocalFunc("again", rs.again) 71 72 cfg := engine.Config{ 73 Root: "root", 74 SessionId: sessionId, 75 OutputSize: uint32(size), 76 } 77 78 en := engine.NewEngine(cfg, rs) 79 en = en.WithState(st) 80 err = engine.Loop(ctx, en, os.Stdin, os.Stdout, nil) 81 if err != nil { 82 fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err) 83 os.Exit(1) 84 } 85 }