-
Notifications
You must be signed in to change notification settings - Fork 296
/
print.go
82 lines (66 loc) · 1.75 KB
/
print.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
package print
import (
"errors"
"go.starlark.net/starlark"
"github.com/tilt-dev/tilt/internal/tiltfile/starkit"
"github.com/tilt-dev/tilt/pkg/logger"
)
// Additional functions for print output.
type Plugin struct{}
func NewPlugin() Plugin {
return Plugin{}
}
func (Plugin) OnStart(env *starkit.Environment) error {
err := env.AddBuiltin("warn", warn)
if err != nil {
return err
}
err = env.AddBuiltin("fail", fail)
if err != nil {
return err
}
err = env.AddBuiltin("exit", exit)
if err != nil {
return err
}
return nil
}
func fail(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var msg string
err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs, "msg", &msg)
if err != nil {
return nil, err
}
return nil, errors.New(msg)
}
func warn(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var msg string
err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs, "msg", &msg)
if err != nil {
return nil, err
}
ctx, err := starkit.ContextFromThread(thread)
if err != nil {
return nil, err
}
logger.Get(ctx).Warnf("%s", msg)
return starlark.None, nil
}
func exit(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var codeVal starlark.Value
err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs, "code?", &codeVal)
if err != nil {
return nil, err
}
ctx, err := starkit.ContextFromThread(thread)
if err != nil {
return nil, err
}
if codeVal != nil && codeVal != starlark.None {
code := codeVal.String()
if code != "" {
logger.Get(ctx).Infof("%s", code)
}
}
return starlark.None, starkit.ErrStopExecution
}