1
1
mirror of https://github.com/mcuadros/ascode synced 2024-11-23 01:11:59 +01:00
ascode/cmd/run.go
Máximo Cuadros e1c229b80c *: project org change
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
2019-08-01 20:43:59 +02:00

67 lines
1.5 KiB
Go

package cmd
import (
"fmt"
"io/ioutil"
"os"
"github.com/mcuadros/ascode/starlark/types"
"github.com/hashicorp/hcl2/hclwrite"
"go.starlark.net/starlark"
)
const (
RunCmdShortDescription = "Run parses, resolves, and executes a Starlark file."
RunCmdLongDescription = RunCmdShortDescription + "\n\n" +
"When a provider is instantiated is automatically installed, at the \n" +
"default location (~/.terraform.d/plugins), this can be overrided \n" +
"using the flag `--plugin-dir=<PATH>`. \n\n" +
"The Starlark file can be \"transpiled\" to a HCL file using the flag \n" +
"`--to-hcl=<FILE>`. This file can be used directly with Terraform init \n" +
"and plan commands.\n"
)
type RunCmd struct {
commonCmd
ToHCL string `long:"to-hcl" description:"dumps context resources to a hcl file"`
PositionalArgs struct {
File string `positional-arg-name:"file" description:"starlark source file"`
} `positional-args:"true" required:"1"`
}
func (c *RunCmd) Execute(args []string) error {
c.init()
out, err := c.runtime.ExecFile(c.PositionalArgs.File)
if err != nil {
if err, ok := err.(*starlark.EvalError); ok {
fmt.Println(err.Backtrace())
os.Exit(1)
return nil
}
return err
}
return c.dumpToHCL(out)
}
func (c *RunCmd) dumpToHCL(ctx starlark.StringDict) error {
if c.ToHCL == "" {
return nil
}
f := hclwrite.NewEmptyFile()
for _, v := range ctx {
p, ok := v.(*types.Provider)
if !ok {
continue
}
p.ToHCL(f.Body())
}
return ioutil.WriteFile(c.ToHCL, f.Bytes(), 0644)
}