Skip to content

Commit

Permalink
cmd/run: add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Apr 9, 2020
1 parent 4a1a60b commit 1643ed1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
25 changes: 21 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type RunCmd struct {
commonCmd

ToHCL string `long:"to-hcl" description:"dumps resources to a hcl file"`
PrintHCL bool `long:"print-hcl" description:"print resources to a hcl file"`
PrintHCL bool `long:"print-hcl" description:"prints resources to a hcl file"`
NoValidate bool `long:"no-validate" description:"skips the validation of the resources"`
PositionalArgs struct {
File string `positional-arg-name:"file" description:"starlark source file"`
} `positional-args:"true" required:"1"`
Expand All @@ -37,7 +38,7 @@ type RunCmd struct {
func (c *RunCmd) Execute(args []string) error {
c.init()

out, err := c.runtime.ExecFile(c.PositionalArgs.File)
_, err := c.runtime.ExecFile(c.PositionalArgs.File)
if err != nil {
if err, ok := err.(*starlark.EvalError); ok {
fmt.Println(err.Backtrace())
Expand All @@ -48,10 +49,26 @@ func (c *RunCmd) Execute(args []string) error {
return err
}

return c.dumpToHCL(out)
c.validate()
return c.dumpToHCL()
}

func (c *RunCmd) dumpToHCL(ctx starlark.StringDict) error {
func (c *RunCmd) validate() {
if c.NoValidate {
return
}

errs := c.runtime.Terraform.Validate()
for _, err := range errs {
fmt.Fprintln(os.Stderr, err)
}

if len(errs) != 0 {
os.Exit(1)
}
}

func (c *RunCmd) dumpToHCL() error {
if c.ToHCL == "" && !c.PrintHCL {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions starlark/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {

// ExecFile parses, resolves, and executes a Starlark file.
func (r *Runtime) ExecFile(filename string) (starlark.StringDict, error) {
filename, _ = osfilepath.Abs(filename)
r.path, _ = osfilepath.Split(filename)
fullpath, _ := osfilepath.Abs(filename)
r.path, _ = osfilepath.Split(fullpath)

thread := &starlark.Thread{Name: "thread", Load: r.load}
r.setLocals(thread)
Expand Down
14 changes: 7 additions & 7 deletions starlark/types/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ func BuiltinValidate() starlark.Value {
})
}

// Validate honors the Vadiabler interface.
// Validate honors the Validabler interface.
func (t *Terraform) Validate() (errs ValidationErrors) {
if t.b != nil {
errs = append(errs, t.b.Validate()...)
}

errs = append(errs, t.b.Validate()...)
errs = append(errs, t.p.Validate()...)
return
}

// Validate honors the Vadiabler interface.
// Validate honors the Validabler interface.
func (d *Dict) Validate() (errs ValidationErrors) {
for _, v := range d.Keys() {
p, _, _ := d.Get(v)
Expand All @@ -110,7 +110,7 @@ func (d *Dict) Validate() (errs ValidationErrors) {
return
}

// Validate honors the Vadiabler interface.
// Validate honors the Validabler interface.
func (p *Provider) Validate() (errs ValidationErrors) {
errs = append(errs, p.Resource.Validate()...)
errs = append(errs, p.dataSources.Validate()...)
Expand All @@ -119,7 +119,7 @@ func (p *Provider) Validate() (errs ValidationErrors) {
return
}

// Validate honors the Vadiabler interface.
// Validate honors the Validabler interface.
func (g *ResourceCollectionGroup) Validate() (errs ValidationErrors) {
names := make(sort.StringSlice, len(g.collections))
var i int
Expand All @@ -136,7 +136,7 @@ func (g *ResourceCollectionGroup) Validate() (errs ValidationErrors) {
return
}

// Validate honors the Vadiabler interface.
// Validate honors the Validabler interface.
func (c *ResourceCollection) Validate() (errs ValidationErrors) {
if c.nestedblock != nil {
l := c.Len()
Expand All @@ -161,7 +161,7 @@ func (c *ResourceCollection) Validate() (errs ValidationErrors) {
return
}

// Validate honors the Vadiabler interface.
// Validate honors the Validabler interface.
func (r *Resource) Validate() ValidationErrors {
return append(
r.doValidateAttributes(),
Expand Down

0 comments on commit 1643ed1

Please sign in to comment.