Skip to content

Commit 1643ed1

Browse files
committed
cmd/run: add validation
1 parent 4a1a60b commit 1643ed1

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

cmd/run.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type RunCmd struct {
2727
commonCmd
2828

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

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

51-
return c.dumpToHCL(out)
52+
c.validate()
53+
return c.dumpToHCL()
5254
}
5355

54-
func (c *RunCmd) dumpToHCL(ctx starlark.StringDict) error {
56+
func (c *RunCmd) validate() {
57+
if c.NoValidate {
58+
return
59+
}
60+
61+
errs := c.runtime.Terraform.Validate()
62+
for _, err := range errs {
63+
fmt.Fprintln(os.Stderr, err)
64+
}
65+
66+
if len(errs) != 0 {
67+
os.Exit(1)
68+
}
69+
}
70+
71+
func (c *RunCmd) dumpToHCL() error {
5572
if c.ToHCL == "" && !c.PrintHCL {
5673
return nil
5774
}

starlark/runtime/runtime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime {
8585

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

9191
thread := &starlark.Thread{Name: "thread", Load: r.load}
9292
r.setLocals(thread)

starlark/types/validate.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ func BuiltinValidate() starlark.Value {
8585
})
8686
}
8787

88-
// Validate honors the Vadiabler interface.
88+
// Validate honors the Validabler interface.
8989
func (t *Terraform) Validate() (errs ValidationErrors) {
9090
if t.b != nil {
9191
errs = append(errs, t.b.Validate()...)
9292
}
9393

94-
errs = append(errs, t.b.Validate()...)
94+
errs = append(errs, t.p.Validate()...)
9595
return
9696
}
9797

98-
// Validate honors the Vadiabler interface.
98+
// Validate honors the Validabler interface.
9999
func (d *Dict) Validate() (errs ValidationErrors) {
100100
for _, v := range d.Keys() {
101101
p, _, _ := d.Get(v)
@@ -110,7 +110,7 @@ func (d *Dict) Validate() (errs ValidationErrors) {
110110
return
111111
}
112112

113-
// Validate honors the Vadiabler interface.
113+
// Validate honors the Validabler interface.
114114
func (p *Provider) Validate() (errs ValidationErrors) {
115115
errs = append(errs, p.Resource.Validate()...)
116116
errs = append(errs, p.dataSources.Validate()...)
@@ -119,7 +119,7 @@ func (p *Provider) Validate() (errs ValidationErrors) {
119119
return
120120
}
121121

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

139-
// Validate honors the Vadiabler interface.
139+
// Validate honors the Validabler interface.
140140
func (c *ResourceCollection) Validate() (errs ValidationErrors) {
141141
if c.nestedblock != nil {
142142
l := c.Len()
@@ -161,7 +161,7 @@ func (c *ResourceCollection) Validate() (errs ValidationErrors) {
161161
return
162162
}
163163

164-
// Validate honors the Vadiabler interface.
164+
// Validate honors the Validabler interface.
165165
func (r *Resource) Validate() ValidationErrors {
166166
return append(
167167
r.doValidateAttributes(),

0 commit comments

Comments
 (0)