mirror of
https://github.com/mcuadros/ascode
synced 2024-11-22 17:02:03 +01:00
starlark/runtime: basic implementation
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
This commit is contained in:
parent
5bfd5f4654
commit
0a5a2cf646
13
examples/encoding.star
Normal file
13
examples/encoding.star
Normal file
@ -0,0 +1,13 @@
|
||||
load("encoding/json", "json")
|
||||
load("encoding/base64", "base64")
|
||||
|
||||
ignition = provider("ignition", "1.1.0")
|
||||
|
||||
user = ignition.data.user()
|
||||
user.name = "foo"
|
||||
user.uid = 42
|
||||
user.groups = ["foo", "bar"]
|
||||
user.system = True
|
||||
|
||||
print(json.dumps(user.__dict__))
|
||||
print(base64.encode("foo"))
|
12
main.go
12
main.go
@ -6,12 +6,11 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/ascode-dev/ascode/starlark/runtime"
|
||||
"github.com/ascode-dev/ascode/starlark/types"
|
||||
"github.com/ascode-dev/ascode/terraform"
|
||||
|
||||
"github.com/hashicorp/hcl2/hclwrite"
|
||||
"go.starlark.net/repl"
|
||||
"go.starlark.net/resolve"
|
||||
"go.starlark.net/starlark"
|
||||
)
|
||||
|
||||
@ -19,14 +18,9 @@ func main() {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
|
||||
pm := &terraform.PluginManager{".providers"}
|
||||
resolve.AllowFloat = true
|
||||
runtime := runtime.NewRuntime(pm)
|
||||
|
||||
thread := &starlark.Thread{Name: "thread", Load: repl.MakeLoad()}
|
||||
predeclared := starlark.StringDict{
|
||||
"provider": types.BuiltinProvider(pm),
|
||||
}
|
||||
|
||||
out, err := starlark.ExecFile(thread, os.Args[1], nil, predeclared)
|
||||
out, err := runtime.ExecFile(os.Args[1])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if err, ok := err.(*starlark.EvalError); ok {
|
||||
|
54
starlark/runtime/runtime.go
Normal file
54
starlark/runtime/runtime.go
Normal file
@ -0,0 +1,54 @@
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/ascode-dev/ascode/starlark/types"
|
||||
"github.com/ascode-dev/ascode/terraform"
|
||||
"github.com/qri-io/starlib/encoding/base64"
|
||||
"github.com/qri-io/starlib/encoding/csv"
|
||||
"github.com/qri-io/starlib/encoding/json"
|
||||
"go.starlark.net/repl"
|
||||
"go.starlark.net/resolve"
|
||||
"go.starlark.net/starlark"
|
||||
)
|
||||
|
||||
func init() {
|
||||
resolve.AllowFloat = true
|
||||
}
|
||||
|
||||
type LoadModuleFunc func() (starlark.StringDict, error)
|
||||
|
||||
type Runtime struct {
|
||||
predeclared starlark.StringDict
|
||||
modules map[string]LoadModuleFunc
|
||||
fallbackLoad func(t *starlark.Thread, module string) (starlark.StringDict, error)
|
||||
}
|
||||
|
||||
func NewRuntime(pm *terraform.PluginManager) *Runtime {
|
||||
return &Runtime{
|
||||
fallbackLoad: repl.MakeLoad(),
|
||||
modules: map[string]LoadModuleFunc{
|
||||
"encoding/json": json.LoadModule,
|
||||
"encoding/base64": base64.LoadModule,
|
||||
"encoding/csv": csv.LoadModule,
|
||||
},
|
||||
predeclared: starlark.StringDict{
|
||||
"provider": types.BuiltinProvider(pm),
|
||||
"hcl": types.BuiltinHCL(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Runtime) ExecFile(filename string) (starlark.StringDict, error) {
|
||||
thread := &starlark.Thread{Name: "thread", Load: r.load}
|
||||
return starlark.ExecFile(thread, os.Args[1], nil, r.predeclared)
|
||||
}
|
||||
|
||||
func (r *Runtime) load(t *starlark.Thread, module string) (starlark.StringDict, error) {
|
||||
if m, ok := r.modules[module]; ok {
|
||||
return m()
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user