go-vise

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

main.go (1564B)


      1 // Example: Use gdbm backend to retrieve resources.
      2 package main
      3 
      4 import (
      5 	"context"
      6 	"fmt"
      7 	"os"
      8 	"path"
      9 
     10 	testdataloader "github.com/peteole/testdata-loader"
     11 
     12 	"git.defalsify.org/vise.git/engine"
     13 	"git.defalsify.org/vise.git/resource"
     14 	"git.defalsify.org/vise.git/db"
     15 	gdbmdb "git.defalsify.org/vise.git/db/gdbm"
     16 	fsdb "git.defalsify.org/vise.git/db/fs"
     17 )
     18 
     19 var (
     20 	baseDir = testdataloader.GetBasePath()
     21 	scriptDir = path.Join(baseDir, "examples", "gdbm")
     22 	dbFile = path.Join(scriptDir, "vise.gdbm")
     23 )
     24 
     25 func do(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     26 	return resource.Result{
     27 		Content: "bye",
     28 	}, nil
     29 }
     30 	     
     31 func main() {
     32 	ctx := context.Background()
     33 	root := "root"
     34 	fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir)
     35 
     36 	store := gdbmdb.NewGdbmDb()
     37 	err := store.Connect(ctx, dbFile)
     38 	if err != nil {
     39 		panic(err)
     40 	}
     41 
     42 	tg := resource.NewDbResource(store)
     43 	tg.Without(db.DATATYPE_MENU)
     44 	rs := resource.NewMenuResource()
     45 	rs = rs.WithTemplateGetter(tg.GetTemplate)
     46 	rs = rs.WithCodeGetter(tg.GetCode)
     47 
     48 	fsStore := fsdb.NewFsDb()
     49 	fsStore.Connect(ctx, scriptDir)
     50 	rsf := resource.NewDbResource(fsStore)
     51 	rsf.WithOnly(db.DATATYPE_MENU)
     52 	rsf.AddLocalFunc("do", do)
     53 	rs.WithMenuGetter(rsf.GetMenu)
     54 	rs.WithEntryFuncGetter(rsf.FuncFor)
     55 
     56 	cfg := engine.Config{
     57 		Root: "root",
     58 		Language: "nor",
     59 	}
     60 	en := engine.NewEngine(cfg, rs)
     61 
     62 	err = engine.Loop(ctx, en, os.Stdin, os.Stdout, nil)
     63 	if err != nil {
     64 		fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
     65 		os.Exit(1)
     66 	}
     67 }