go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

main.go (1570B)


      1 // Example: Profile data completion menu.
      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/cache"
     14 	"git.defalsify.org/vise.git/engine"
     15 	"git.defalsify.org/vise.git/resource"
     16 	"git.defalsify.org/vise.git/state"
     17 	"git.defalsify.org/vise.git/db"
     18 	fsdb "git.defalsify.org/vise.git/db/fs"
     19 )
     20 
     21 var (
     22 	baseDir = testdataloader.GetBasePath()
     23 	scriptDir = path.Join(baseDir, "examples", "static")
     24 	emptyResult = resource.Result{}
     25 )
     26 
     27 func out(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     28 	return resource.Result{
     29 		Content: "foo",	
     30 	}, nil
     31 }
     32 
     33 
     34 func main() {
     35 	var useInternal bool
     36 	root := "root"
     37 	dir := scriptDir
     38 	flag.BoolVar(&useInternal, "i", false, "use internal function for render")
     39 	flag.Parse()
     40 	fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
     41 
     42 	ctx := context.Background()
     43 	st := state.NewState(0)
     44 	store := fsdb.NewFsDb()
     45 	err := store.Connect(ctx, scriptDir)
     46 	if err != nil {
     47 		panic(err)
     48 	}
     49 	rs := resource.NewDbResource(store)
     50 	rs.With(db.DATATYPE_STATICLOAD)
     51 
     52 	if useInternal {
     53 		rs.AddLocalFunc("out", out)
     54 	}
     55 	ca := cache.NewCache()
     56 	cfg := engine.Config{
     57 		Root: root,
     58 	}
     59 	en := engine.NewEngine(cfg, rs)
     60 	en = en.WithState(st)
     61 	en = en.WithMemory(ca)
     62 
     63 	_, err = en.Exec(ctx, []byte{})
     64 	if err != nil {
     65 		fmt.Fprintf(os.Stderr, "exec error: %v\n", err)
     66 		os.Exit(1)
     67 	}
     68 	_, err = en.Flush(ctx, os.Stdout)
     69 	if err != nil {
     70 		fmt.Fprintf(os.Stderr, "flush error: %v\n", err)
     71 		os.Exit(1)
     72 	}
     73 }