From 01e12b01d1f07c8504847db3bc8fcdde88e31b96 Mon Sep 17 00:00:00 2001 From: Andrii Abramov Date: Tue, 9 Nov 2021 22:12:55 +0200 Subject: [PATCH] - support multiple assertions --- README.md | 1 + cmd/init.go | 25 ++++++++++++++----------- goal.yaml | 34 +++++++++++++++++----------------- lib/commands.go | 42 ++++++++++++++++++++++++++---------------- 4 files changed, 58 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index bb1f85f..d958c92 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/cmd/init.go b/cmd/init.go index 268827a..5d0b525 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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", + }, }, }, }, diff --git a/goal.yaml b/goal.yaml index 9e218e1..14daf01 100644 --- a/goal.yaml +++ b/goal.yaml @@ -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 diff --git a/lib/commands.go b/lib/commands.go index 1f31d90..91adf80 100644 --- a/lib/commands.go +++ b/lib/commands.go @@ -25,7 +25,7 @@ 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"` } @@ -33,7 +33,7 @@ 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"` } @@ -41,7 +41,7 @@ type Command struct { Name string Cmd string Args []string - Assert *Assert + Assert []Assert Env string Desc string } @@ -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...) @@ -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() } @@ -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) @@ -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, }) } @@ -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,