Skip to content

Commit

Permalink
- rename assert.equals -> assert.expect
Browse files Browse the repository at this point in the history
- add optional `assert.fix` on how to fix the issue
  • Loading branch information
Andrii Abramov committed Nov 8, 2021
1 parent 9fbfb1a commit f22704b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tf-apply-dev:
assert:
desc: Check if on dev workspace
ref: workspace # References goal above
equals: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
expect: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
cmd: terraform
args:
- apply
Expand All @@ -46,7 +46,7 @@ tf-apply-stage:
assert:
desc: Check if on stage workspace
ref: workspace # References goal above
equals: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
expect: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
args:
- apply
Expand Down Expand Up @@ -96,6 +96,7 @@ $ goal tf-apply-stage
## Project plan
- [X] Pipe STDIN for "yes/no" inputs, etc.
- [X] Add `assert.fix`. Display when assertion failed, e.g. `terraform workspace select dev`
- [ ] Simpler `brew tap aaabramov/goal`
- [ ] Manual approvals for proceeding like `assert.approval`
- [ ] Add "environment" management to avoid tf-plan-dev, tf-plan-stage, tf-plan-prod, etc.
Expand Down
15 changes: 10 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
type Assert struct {
Desc string `yaml:"desc"`
Ref string `yaml:"ref"`
Equals string `yaml:"equals"`
Expect string `yaml:"expect"`
Fix string `yaml:"fix"`
}

func (a Assert) String() string {
return fmt.Sprintf("Assert{ref:'%s',equals:'%s'}", a.Ref, a.Equals)
return fmt.Sprintf("Assert{ref:'%s',expect:'%s'}", a.Ref, a.Expect)
}

type YamlCommand struct {
Expand Down Expand Up @@ -65,14 +66,18 @@ func (c *Commands) exec(name string) {

if exists {
out := strings.TrimSpace(getOutput(ref))
if out != command.Assert.Equals {
fatal(
if out != command.Assert.Expect {
msg := fmt.Sprintf(
"Precondition failed: %s\n\tOutput: \"%s\"\n\tExpected: \"%s\"\n\tCLI: %s",
ref.Name,
out,
command.Assert.Equals,
command.Assert.Expect,
ref.cli(),
)
if command.Assert.Fix != "" {
msg += fmt.Sprintf("\n\tFix: %s", command.Assert.Fix)
}
fatal(msg)
} else {
info("✅ Precondition: " + command.Assert.Desc)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/helm/goal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ helm-upgrade-dev-dry-run:
assert:
desc: Check if on dev cluster
ref: current-context
equals: dev-cluster
expect: dev-cluster
cmd: helm
args:
- upgrade
Expand All @@ -27,7 +27,7 @@ helm-upgrade-dev:
assert:
desc: Check if on dev cluster
ref: current-context
equals: dev-cluster
expect: dev-cluster
cmd: helm
args:
- upgrade
Expand Down
4 changes: 2 additions & 2 deletions examples/terraform/goal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tf-apply-dev:
assert:
desc: Check if on dev workspace
ref: workspace # References goal above
equals: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
expect: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
cmd: terraform
args:
- apply
Expand All @@ -22,7 +22,7 @@ tf-apply-stage:
assert:
desc: Check if on stage workspace
ref: workspace # References goal above
equals: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
expect: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
args:
- apply
Expand Down
53 changes: 30 additions & 23 deletions goal.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
workspace:
desc: Current terraform workspace
cmd: terraform
test:
desc: Echo test data
cmd: echo
args:
- workspace
- show
- -n
- dev

tf-apply-dev:
desc: Terraform apply on dev
test1:
desc: Test 1
cmd: echo
assert:
desc: Check if on dev workspace
ref: workspace # References goal above
equals: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
cmd: terraform
desc: Check if dev
ref: test
expect: dev
args:
- apply
- -var-file
- vars/dev.tfvars
- Passed.

tf-apply-stage:
desc: Terraform apply on stage
test2:
desc: Test 1
cmd: echo
assert:
desc: Check if on stage workspace
ref: workspace # References goal above
equals: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
desc: Check if stage
ref: test
expect: stage
fix: terraform workspace select stage
args:
- apply
- -var-file
- vars/stage.tfvars
- Should not pass!

# Test stdin
scala:
desc: Run scala REPL
cmd: scala

# Test env vars being passed
env:
desc: Show env vars
cmd: env

0 comments on commit f22704b

Please sign in to comment.