From 1643ed115e6a4a59b302f3a3df0cefd3ab866fa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Fri, 10 Apr 2020 00:26:26 +0200 Subject: [PATCH] cmd/run: add validation --- cmd/run.go | 25 +++++++++++++++++++++---- starlark/runtime/runtime.go | 4 ++-- starlark/types/validate.go | 14 +++++++------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 7744baa..0b9be00 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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"` @@ -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()) @@ -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 } diff --git a/starlark/runtime/runtime.go b/starlark/runtime/runtime.go index b3c1779..ed411e5 100644 --- a/starlark/runtime/runtime.go +++ b/starlark/runtime/runtime.go @@ -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) diff --git a/starlark/types/validate.go b/starlark/types/validate.go index e759318..c196003 100644 --- a/starlark/types/validate.go +++ b/starlark/types/validate.go @@ -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) @@ -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()...) @@ -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 @@ -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() @@ -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(),