-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
198 lines (162 loc) · 4.32 KB
/
exec.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package starbox
import (
"errors"
"time"
"github.com/1set/starlet"
"github.com/psanford/memfs"
)
// Run executes a script and returns the converted output.
func (s *Starbox) Run(script string) (starlet.StringAnyMap, error) {
s.mu.Lock()
defer s.mu.Unlock()
// prepare environment
if err := s.prepareScriptEnv(script); err != nil {
return nil, err
}
// run
s.hasExec = true
s.execTimes++
return s.mac.Run()
}
// RunFile executes a script file and returns the converted output.
func (s *Starbox) RunFile(file string) (starlet.StringAnyMap, error) {
s.mu.Lock()
defer s.mu.Unlock()
// prepare environment
if err := s.prepareEnv(); err != nil {
return nil, err
}
// run
s.hasExec = true
s.execTimes++
return s.mac.RunFile(file, s.modFS, nil)
}
// RunTimeout executes a script and returns the converted output.
func (s *Starbox) RunTimeout(script string, timeout time.Duration) (starlet.StringAnyMap, error) {
s.mu.Lock()
defer s.mu.Unlock()
// prepare environment
if err := s.prepareScriptEnv(script); err != nil {
return nil, err
}
// run
s.hasExec = true
s.execTimes++
return s.mac.RunWithTimeout(timeout, nil)
}
// REPL starts a REPL session.
func (s *Starbox) REPL() error {
s.mu.Lock()
defer s.mu.Unlock()
// prepare environment -- no need to set script content
if err := s.prepareScriptEnv(""); err != nil {
return err
}
// run
s.hasExec = true
s.execTimes++
s.mac.REPL()
return nil
}
// RunInspect executes a script and then REPL with result and returns the converted output.
func (s *Starbox) RunInspect(script string) (starlet.StringAnyMap, error) {
s.mu.Lock()
defer s.mu.Unlock()
// prepare environment
if err := s.prepareScriptEnv(script); err != nil {
return nil, err
}
// run script
s.hasExec = true
s.execTimes++
out, err := s.mac.Run()
// repl
s.mac.REPL()
return out, err
}
// InspectCondFunc is a function type for inspecting the converted output of Run*() and decide whether to continue.
type InspectCondFunc func(starlet.StringAnyMap, error) bool
// RunInspectIf executes a script and then REPL with result and returns the converted output, if the condition is met.
// The condition function is called with the converted output and the error from Run*(), and returns true if REPL is needed.
func (s *Starbox) RunInspectIf(script string, cond InspectCondFunc) (starlet.StringAnyMap, error) {
s.mu.Lock()
defer s.mu.Unlock()
// prepare environment
if err := s.prepareScriptEnv(script); err != nil {
return nil, err
}
// run script
s.hasExec = true
s.execTimes++
out, err := s.mac.Run()
// repl
if cond(out, err) {
s.mac.REPL()
}
return out, err
}
// CallStarlarkFunc executes a function defined in Starlark with arguments and returns the converted output.
func (s *Starbox) CallStarlarkFunc(name string, args ...interface{}) (interface{}, error) {
if s == nil || s.mac == nil {
return nil, errors.New("no starlet machine")
}
// lock it
s.mu.Lock()
defer s.mu.Unlock()
// call it
return s.mac.Call(name, args...)
}
func (s *Starbox) prepareScriptEnv(script string) (err error) {
// if it's not the first run, set the script content only
if s.hasExec {
s.mac.SetScriptContent([]byte(script))
return nil
}
// prepare environment
if err = s.prepareEnv(); err != nil {
return err
}
// set script
s.mac.SetScript("box.star", []byte(script), s.modFS)
// all is done
return nil
}
func (s *Starbox) prepareEnv() (err error) {
// set custom tag and print function
if s.structTag != "" {
s.mac.SetCustomTag(s.structTag)
}
if s.printFunc != nil {
s.mac.SetPrintFunc(s.printFunc)
}
// set variables
s.mac.SetGlobals(s.globals)
// extract module loaders
preMods, lazyMods, modNames, err := s.extractModLoaders()
if err != nil {
return err
}
// set modules to machine
if len(preMods) > 0 || len(lazyMods) > 0 {
s.mac.SetPreloadModules(preMods)
s.mac.SetLazyloadModules(lazyMods)
}
// prepare script modules
if len(s.scriptMods) > 0 && s.modFS == nil {
rootFS := memfs.New()
for fp, scr := range s.scriptMods {
// TODO: support directory/file.star later
if err := rootFS.WriteFile(fp, []byte(scr), 0644); err != nil {
return err
}
modNames = append(modNames, fp)
}
s.modFS = rootFS
}
// set load module names
s.modNames = modNames
s.mac.AddGlobals(starlet.StringAnyMap{
"__modules__": starlarkStringList(modNames),
})
return nil
}