-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
93 lines (76 loc) · 1.53 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/fatih/color"
"github.com/risor-io/risor"
)
type State struct {
Name string
Running bool
}
type Service struct {
name string
running bool
startCount int
stopCount int
}
func (s *State) IsRunning() bool {
return s.Running
}
func (s *Service) Start() error {
if s.running {
return fmt.Errorf("service %s already running", s.name)
}
s.running = true
s.startCount++
return nil
}
func (s *Service) Stop() error {
if !s.running {
return fmt.Errorf("service %s not running", s.name)
}
s.running = false
s.stopCount++
return nil
}
func (s *Service) SetName(name string) {
s.name = name
}
func (s *Service) GetName() string {
return s.name
}
func (s *Service) PrintState() {
fmt.Printf("printing state... name: %s running %t\n", s.name, s.running)
}
func (s *Service) GetState() *State {
return &State{
Name: s.name,
Running: s.running,
}
}
const defaultExample = `
svc.SetName("My Service")
svc.Start()
state := svc.GetState()
print("STATE:", state, type(state))
state.IsRunning()
`
var red = color.New(color.FgRed).SprintfFunc()
func main() {
var code string
flag.StringVar(&code, "code", defaultExample, "Code to evaluate")
flag.Parse()
ctx := context.Background()
// Initialize the service
svc := &Service{}
// Run the Risor code which can access the service as `svc`
result, err := risor.Eval(ctx, code, risor.WithGlobal("svc", svc))
if err != nil {
fmt.Println(red(err.Error()))
os.Exit(1)
}
fmt.Println("RESULT:", result)
}