2019-06-27 00:27:39 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-05 03:43:25 +02:00
|
|
|
"fmt"
|
2019-06-27 00:27:39 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2019-07-05 07:46:15 +02:00
|
|
|
"github.com/ascode-dev/ascode/starlark/types"
|
|
|
|
"github.com/ascode-dev/ascode/terraform"
|
|
|
|
|
2019-07-05 03:43:25 +02:00
|
|
|
"github.com/hashicorp/hcl2/hclwrite"
|
2019-07-02 03:37:00 +02:00
|
|
|
"go.starlark.net/repl"
|
2019-06-27 00:27:39 +02:00
|
|
|
"go.starlark.net/resolve"
|
|
|
|
"go.starlark.net/starlark"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
|
2019-07-05 07:46:15 +02:00
|
|
|
pm := &terraform.PluginManager{".providers"}
|
2019-06-27 00:27:39 +02:00
|
|
|
resolve.AllowFloat = true
|
2019-07-02 03:37:00 +02:00
|
|
|
|
2019-07-05 07:46:15 +02:00
|
|
|
provider := starlark.NewBuiltin("provider", func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
2019-06-27 00:27:39 +02:00
|
|
|
name := args.Index(0).(starlark.String)
|
2019-07-02 03:37:00 +02:00
|
|
|
version := args.Index(1).(starlark.String)
|
|
|
|
|
2019-07-05 07:46:15 +02:00
|
|
|
return types.MakeProvider(pm, string(name), string(version))
|
2019-06-27 00:27:39 +02:00
|
|
|
})
|
|
|
|
|
2019-07-02 03:37:00 +02:00
|
|
|
thread := &starlark.Thread{Name: "thread", Load: repl.MakeLoad()}
|
2019-06-27 00:27:39 +02:00
|
|
|
predeclared := starlark.StringDict{
|
2019-07-05 07:46:15 +02:00
|
|
|
"provider": provider,
|
2019-06-27 00:27:39 +02:00
|
|
|
}
|
|
|
|
|
2019-07-05 03:43:25 +02:00
|
|
|
out, err := starlark.ExecFile(thread, os.Args[1], nil, predeclared)
|
|
|
|
if err != nil {
|
|
|
|
if err, ok := err.(*starlark.EvalError); ok {
|
|
|
|
log.Fatal(err.Backtrace())
|
|
|
|
}
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range out {
|
2019-07-05 07:46:15 +02:00
|
|
|
p, ok := v.(*types.Provider)
|
2019-07-05 03:43:25 +02:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
f := hclwrite.NewEmptyFile()
|
|
|
|
p.ToHCL(f.Body())
|
|
|
|
|
|
|
|
fmt.Println(string(f.Bytes()))
|
2019-06-27 00:27:39 +02:00
|
|
|
}
|
|
|
|
}
|