2019-07-11 23:59:04 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
2020-03-21 00:10:22 +01:00
|
|
|
ToHCL string `long:"to-hcl" description:"dumps resources to a hcl file"`
|
|
|
|
PrintHCL bool `long:"print-hcl" description:"print resources to a hcl file"`
|
2019-07-11 23:59:04 +02:00
|
|
|
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 {
|
2020-03-21 00:10:22 +01:00
|
|
|
if c.ToHCL == "" && !c.PrintHCL {
|
2019-07-11 23:59:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f := hclwrite.NewEmptyFile()
|
2020-03-20 23:49:24 +01:00
|
|
|
c.runtime.Terraform.ToHCL(f.Body())
|
2019-07-11 23:59:04 +02:00
|
|
|
|
2020-03-21 00:10:22 +01:00
|
|
|
if c.PrintHCL {
|
|
|
|
os.Stdout.Write(f.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ToHCL == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-11 23:59:04 +02:00
|
|
|
return ioutil.WriteFile(c.ToHCL, f.Bytes(), 0644)
|
|
|
|
}
|