1
1
mirror of https://github.com/mcuadros/ascode synced 2024-11-23 01:11:59 +01:00

cmd/run: add validation

This commit is contained in:
Máximo Cuadros 2020-04-10 00:26:26 +02:00
parent 4a1a60b57a
commit 1643ed115e
No known key found for this signature in database
GPG Key ID: 17A5DFEDC735AE4B
3 changed files with 30 additions and 13 deletions

@ -27,7 +27,8 @@ type RunCmd struct {
commonCmd commonCmd
ToHCL string `long:"to-hcl" description:"dumps resources to a hcl file"` 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 { PositionalArgs struct {
File string `positional-arg-name:"file" description:"starlark source file"` File string `positional-arg-name:"file" description:"starlark source file"`
} `positional-args:"true" required:"1"` } `positional-args:"true" required:"1"`
@ -37,7 +38,7 @@ type RunCmd struct {
func (c *RunCmd) Execute(args []string) error { func (c *RunCmd) Execute(args []string) error {
c.init() c.init()
out, err := c.runtime.ExecFile(c.PositionalArgs.File) _, err := c.runtime.ExecFile(c.PositionalArgs.File)
if err != nil { if err != nil {
if err, ok := err.(*starlark.EvalError); ok { if err, ok := err.(*starlark.EvalError); ok {
fmt.Println(err.Backtrace()) fmt.Println(err.Backtrace())
@ -48,10 +49,26 @@ func (c *RunCmd) Execute(args []string) error {
return err 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 { if c.ToHCL == "" && !c.PrintHCL {
return nil return nil
} }

@ -85,8 +85,8 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
// ExecFile parses, resolves, and executes a Starlark file. // ExecFile parses, resolves, and executes a Starlark file.
func (r *Runtime) ExecFile(filename string) (starlark.StringDict, error) { func (r *Runtime) ExecFile(filename string) (starlark.StringDict, error) {
filename, _ = osfilepath.Abs(filename) fullpath, _ := osfilepath.Abs(filename)
r.path, _ = osfilepath.Split(filename) r.path, _ = osfilepath.Split(fullpath)
thread := &starlark.Thread{Name: "thread", Load: r.load} thread := &starlark.Thread{Name: "thread", Load: r.load}
r.setLocals(thread) r.setLocals(thread)

@ -85,17 +85,17 @@ func BuiltinValidate() starlark.Value {
}) })
} }
// Validate honors the Vadiabler interface. // Validate honors the Validabler interface.
func (t *Terraform) Validate() (errs ValidationErrors) { func (t *Terraform) Validate() (errs ValidationErrors) {
if t.b != nil { if t.b != nil {
errs = append(errs, t.b.Validate()...) errs = append(errs, t.b.Validate()...)
} }
errs = append(errs, t.b.Validate()...) errs = append(errs, t.p.Validate()...)
return return
} }
// Validate honors the Vadiabler interface. // Validate honors the Validabler interface.
func (d *Dict) Validate() (errs ValidationErrors) { func (d *Dict) Validate() (errs ValidationErrors) {
for _, v := range d.Keys() { for _, v := range d.Keys() {
p, _, _ := d.Get(v) p, _, _ := d.Get(v)
@ -110,7 +110,7 @@ func (d *Dict) Validate() (errs ValidationErrors) {
return return
} }
// Validate honors the Vadiabler interface. // Validate honors the Validabler interface.
func (p *Provider) Validate() (errs ValidationErrors) { func (p *Provider) Validate() (errs ValidationErrors) {
errs = append(errs, p.Resource.Validate()...) errs = append(errs, p.Resource.Validate()...)
errs = append(errs, p.dataSources.Validate()...) errs = append(errs, p.dataSources.Validate()...)
@ -119,7 +119,7 @@ func (p *Provider) Validate() (errs ValidationErrors) {
return return
} }
// Validate honors the Vadiabler interface. // Validate honors the Validabler interface.
func (g *ResourceCollectionGroup) Validate() (errs ValidationErrors) { func (g *ResourceCollectionGroup) Validate() (errs ValidationErrors) {
names := make(sort.StringSlice, len(g.collections)) names := make(sort.StringSlice, len(g.collections))
var i int var i int
@ -136,7 +136,7 @@ func (g *ResourceCollectionGroup) Validate() (errs ValidationErrors) {
return return
} }
// Validate honors the Vadiabler interface. // Validate honors the Validabler interface.
func (c *ResourceCollection) Validate() (errs ValidationErrors) { func (c *ResourceCollection) Validate() (errs ValidationErrors) {
if c.nestedblock != nil { if c.nestedblock != nil {
l := c.Len() l := c.Len()
@ -161,7 +161,7 @@ func (c *ResourceCollection) Validate() (errs ValidationErrors) {
return return
} }
// Validate honors the Vadiabler interface. // Validate honors the Validabler interface.
func (r *Resource) Validate() ValidationErrors { func (r *Resource) Validate() ValidationErrors {
return append( return append(
r.doValidateAttributes(), r.doValidateAttributes(),