-
Notifications
You must be signed in to change notification settings - Fork 303
/
print.go
56 lines (44 loc) · 1.16 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
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 Extension struct{}
func NewExtension() Extension {
return Extension{}
}
func (Extension) 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
}
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
}