go-vise

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

main.go (997B)


      1 // Example: Graceful termination that will be resumed from top on next execution.
      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 	fsdb "git.defalsify.org/vise.git/db/fs"
     15 )
     16 
     17 var (
     18 	baseDir = testdataloader.GetBasePath()
     19 	scriptDir = path.Join(baseDir, "examples", "quit")
     20 )
     21 
     22 func quit(ctx context.Context, sym string, input []byte) (resource.Result, error) {
     23 	return resource.Result{
     24 		Content: "quitter!",
     25 	}, nil
     26 }
     27 
     28 func main() {
     29 	ctx := context.Background()
     30 	store := fsdb.NewFsDb()
     31 	err := store.Connect(ctx, scriptDir)
     32 	if err != nil {
     33 		panic(err)
     34 	}
     35 	rs := resource.NewDbResource(store)
     36 	cfg := engine.Config{
     37 		Root: "root",
     38 	}
     39 	rs.AddLocalFunc("quitcontent", quit)
     40 
     41 	en := engine.NewEngine(cfg, rs)
     42 	err = engine.Loop(ctx, en, os.Stdin, os.Stdout, nil)
     43 	if err != nil {
     44 		fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
     45 		os.Exit(1)
     46 	}
     47 }