Skip to content

Commit

Permalink
- support multiple assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
aaabramov committed Nov 9, 2021
1 parent 0ec84dd commit 01e12b0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 44 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ $ goal tf-apply --on stage
- [ ] Recursive dependencies
- [ ] Assertions
- [X] ref output
- [X] support multiple assertions
- [ ] recursive assertions?
- [ ] raw CLI output -- bad pattern?
- [ ] Global aliases in `$HOME` directory?
Expand Down
25 changes: 14 additions & 11 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,25 @@ var defaultGoals = map[string]lib.YamlGoal{
Desc: "Terraform apply on dev",
Cmd: "terraform",
Args: []string{"apply", "-var-file", "vars/dev.tfvars"},
Assert: &lib.Assert{
Desc: "Check if on dev workspace",
Ref: "workspace",
Expect: "dev",
Fix: "terraform workspace select dev",
},
Assert: []lib.Assert{
{
Desc: "Check if on dev workspace",
Ref: "workspace",
Expect: "dev",
Fix: "terraform workspace select dev",
}},
},
"stage": {
Desc: "Terraform apply on stage",
Cmd: "terraform",
Args: []string{"apply", "-var-file", "vars/stage.tfvars"},
Assert: &lib.Assert{
Desc: "Check if on stage workspace",
Ref: "workspace",
Expect: "stage",
Fix: "terraform workspace select stage",
Assert: []lib.Assert{
{
Desc: "Check if on stage workspace",
Ref: "workspace",
Expect: "stage",
Fix: "terraform workspace select stage",
},
},
},
},
Expand Down
34 changes: 17 additions & 17 deletions goal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ tf-apply:
dev:
cmd: terraform
args:
- apply
- -var-file
- vars/dev.tfvars
- apply
- -var-file
- vars/dev.tfvars
assert:
desc: Check if on dev workspace
ref: workspace
expect: dev
fix: terraform workspace select dev
- desc: Check if on dev workspace
ref: workspace
expect: dev
fix: terraform workspace select dev
desc: Terraform apply on dev
stage:
cmd: terraform
args:
- apply
- -var-file
- vars/stage.tfvars
- apply
- -var-file
- vars/stage.tfvars
assert:
desc: Check if on stage workspace
ref: workspace
expect: stage
fix: terraform workspace select stage
- desc: Check if on stage workspace
ref: workspace
expect: stage
fix: terraform workspace select stage
desc: Terraform apply on stage
workspace:
cmd: terraform
args:
- apply
- -var-file
- vars/dev.tfvars
- apply
- -var-file
- vars/dev.tfvars
42 changes: 26 additions & 16 deletions lib/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ func (a Assert) String() string {
type YamlEnvGoal struct {
Cmd string `yaml:"cmd"`
Args []string `yaml:"args,omitempty"`
Assert *Assert `yaml:"assert,omitempty"`
Assert []Assert `yaml:"assert,omitempty"`
Desc string `yaml:"desc"`
}

type YamlGoal struct {
Envs *map[string]YamlEnvGoal `yaml:"envs,omitempty"`
Cmd string `yaml:"cmd,omitempty"`
Args []string `yaml:"args,omitempty"`
Assert *Assert `yaml:"assert,omitempty"`
Assert []Assert `yaml:"assert,omitempty"`
Desc string `yaml:"desc,omitempty"`
}

type Command struct {
Name string
Cmd string
Args []string
Assert *Assert
Assert []Assert
Env string
Desc string
}
Expand Down Expand Up @@ -124,8 +124,8 @@ func (c *Commands) Exec(name string, env string) {
msg += " on " + env
}
Info(msg)
if command.Assert != nil {
c.runAssertion(*command.Assert)
for _, assert := range command.Assert {
c.runAssertion(assert)
}

cmd := osexec.Command(command.Cmd, command.Args...)
Expand Down Expand Up @@ -169,16 +169,17 @@ func (c *Commands) Render() {
tablewriter.Colors{tablewriter.Normal},
)
for _, cmd := range c.Commands {
assertion := ""
if cmd.Assert != nil {
ref, exists := c.get(cmd.Assert.Ref)
var assertions []string

for _, assert := range cmd.Assert {
ref, exists := c.get(assert.Ref)
if exists {
assertion = fmt.Sprintf("[%s] %s", ref.Name, cmd.Assert.Desc)
assertions = append(assertions, fmt.Sprintf("- [%s] %s", ref.Name, assert.Desc))
} else {

}
}
table.Append([]string{cmd.Name, cmd.Env, cmd.Cli(), cmd.Desc, assertion})
table.Append([]string{cmd.Name, cmd.Env, cmd.Cli(), cmd.Desc, strings.Join(assertions, "\n")})
}
table.Render()
}
Expand All @@ -202,10 +203,15 @@ func normalizeArgs(args []string) []string {
}
}

func validateAssert(name string, env string, assert *Assert) {
if assert == nil {
return
func normalizeAsserts(args []Assert) []Assert {
if args == nil {
return []Assert{}
} else {
return args
}
}

func validateAssert(name string, env string, assert Assert) {
if assert.Ref == "" {
if env == "" {
Fatal("Malformed goals. %s.assert.ref could not be empty", name)
Expand All @@ -222,13 +228,15 @@ func parseEnvCommands(name string, envs map[string]YamlEnvGoal) []Command {
if envCommand.Cmd == "" {
Fatal("Malformed goals. %s.%s.cmd could not be empty", name, env)
}
validateAssert(name, env, envCommand.Assert)
for _, assert := range envCommand.Assert {
validateAssert(name, env, assert)
}
commands = append(commands, Command{
Name: name,
Cmd: envCommand.Cmd,
Args: args,
Desc: envCommand.Desc,
Assert: envCommand.Assert,
Assert: normalizeAsserts(envCommand.Assert),
Env: env,
})
}
Expand All @@ -247,7 +255,9 @@ func ParseCommands(bytes []byte) (*Commands, error) {
if command.Envs != nil {
res = append(res, parseEnvCommands(name, *command.Envs)...)
} else {
validateAssert(name, "", command.Assert)
for _, assert := range command.Assert {
validateAssert(name, "", assert)
}
args := normalizeArgs(command.Args)
res = append(res, Command{
Name: name,
Expand Down

0 comments on commit 01e12b0

Please sign in to comment.