1
0
mirror of https://github.com/drone/drone-cli.git synced 2024-11-23 01:11:57 +01:00

enable loading files

This commit is contained in:
Brad Rydzewski 2019-02-19 21:53:15 -08:00
parent 44813127f3
commit 00907ac6ef
3 changed files with 47 additions and 8 deletions

@ -0,0 +1,8 @@
def docker(repo):
return {
'name': 'docker',
'image': 'plugins/docker',
'settings': {
'repo': repo,
},
}

@ -1,3 +1,8 @@
# cd drone/starlark/samples
# drone script --source pipeline.py --stdout
load('docker.py', 'docker');
def build(version):
return {
'name': 'build',
@ -15,5 +20,6 @@ def main():
'steps': [
build('1.11'),
build('1.12'),
docker('octocat/hello-world'),
],
}

@ -37,10 +37,6 @@ var Command = cli.Command{
Usage: "target file",
Value: ".drone.yml",
},
cli.BoolFlag{
Name: "stream",
Usage: "Write output as a YAML stream.",
},
cli.BoolTFlag{
Name: "format",
Usage: "Write output as formatted YAML",
@ -49,10 +45,6 @@ var Command = cli.Command{
Name: "stdout",
Usage: "Write output to stdout",
},
cli.BoolFlag{
Name: "string",
Usage: "Expect a string, manifest as plain text",
},
},
}
@ -68,6 +60,7 @@ func generate(c *cli.Context) error {
thread := &starlark.Thread{
Name: "drone",
Print: func(_ *starlark.Thread, msg string) { fmt.Println(msg) },
Load: makeLoad(),
}
globals, err := starlark.ExecFile(thread, source, data, nil)
if err != nil {
@ -201,3 +194,35 @@ func goQuoteIsSafe(s string) bool {
}
return true
}
// https://github.com/google/starlark-go/blob/4eb76950c5f02ec5bcfd3ca898231a6543942fd9/repl/repl.go#L175
func makeLoad() func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
type entry struct {
globals starlark.StringDict
err error
}
var cache = make(map[string]*entry)
return func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
e, ok := cache[module]
if e == nil {
if ok {
// request for package whose loading is in progress
return nil, fmt.Errorf("cycle in load graph")
}
// Add a placeholder to indicate "load in progress".
cache[module] = nil
// Load it.
thread := &starlark.Thread{Name: "exec " + module, Load: thread.Load}
globals, err := starlark.ExecFile(thread, module, nil, nil)
e = &entry{globals, err}
// Update the cache.
cache[module] = e
}
return e.globals, e.err
}
}