-
Notifications
You must be signed in to change notification settings - Fork 43
/
starlark_exec.go
91 lines (78 loc) · 2.49 KB
/
starlark_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
// Copyright (c) 2020 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package starlark
import (
"fmt"
"io"
"github.com/vladimirvivien/echo"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
type Executor struct {
thread *starlark.Thread
predecs starlark.StringDict
result starlark.StringDict
}
func New() *Executor {
return &Executor{
thread: newThreadLocal(),
predecs: newPredeclareds(),
}
}
func (e *Executor) Exec(name string, source io.Reader) error {
result, err := starlark.ExecFile(e.thread, name, source, e.predecs)
if err != nil {
if evalErr, ok := err.(*starlark.EvalError); ok {
return fmt.Errorf(evalErr.Backtrace())
}
return err
}
e.result = result
return nil
}
// newThreadLocal creates the execution thread
// and populates default values in the thread.
func newThreadLocal() *starlark.Thread {
thread := &starlark.Thread{Name: "crashd"}
addDefaultCrashdConf(thread)
addDefaultSSHConf(thread)
addDefaultKubeConf(thread)
return thread
}
// newPredeclareds creates string dictionary containing the
// global built-ins values and functions available to the
// runing script.
func newPredeclareds() starlark.StringDict {
return starlark.StringDict{
"os": setupOSStruct(),
identifiers.crashdCfg: starlark.NewBuiltin(identifiers.crashdCfg, crashdConfigFn),
identifiers.sshCfg: starlark.NewBuiltin(identifiers.sshCfg, sshConfigFn),
identifiers.hostListProvider: starlark.NewBuiltin(identifiers.hostListProvider, hostListProvider),
identifiers.resources: starlark.NewBuiltin(identifiers.resources, resourcesFunc),
identifiers.run: starlark.NewBuiltin(identifiers.run, runFunc),
identifiers.kubeCfg: starlark.NewBuiltin(identifiers.kubeCfg, kubeConfigFn),
}
}
func kwargsToStringDict(kwargs []starlark.Tuple) (starlark.StringDict, error) {
if len(kwargs) == 0 {
return starlark.StringDict{}, nil
}
e := echo.New()
dictionary := make(starlark.StringDict)
for _, tup := range kwargs {
key, value := tup[0], tup[1]
if value.Type() == "string" {
unquoted := trimQuotes(value.String())
value = starlark.String(e.Eval(unquoted))
}
dictionary[trimQuotes(key.String())] = value
}
return dictionary, nil
}
func kwargsToStruct(kwargs []starlark.Tuple) (*starlarkstruct.Struct, error) {
dict, err := kwargsToStringDict(kwargs)
if err != nil {
return &starlarkstruct.Struct{}, err
}
return starlarkstruct.FromStringDict(starlarkstruct.Default, dict), nil
}