1
1
Fork 0
mirror of https://github.com/mcuadros/ascode synced 2024-05-08 16:46:18 +02: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

View File

@ -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
}

View File

@ -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)

View File

@ -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(),