Skip to content

Commit

Permalink
- update readme example
Browse files Browse the repository at this point in the history
- add self-complete terraform example
  • Loading branch information
aaabramov committed Nov 8, 2021
1 parent f91d061 commit ec998ff
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 122 deletions.
100 changes: 67 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,53 +29,87 @@ workspace:
- workspace
- show

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

tf-apply-stage:
desc: Terraform apply on stage
assert:
desc: Check if on stage workspace
ref: workspace # References goal above
expect: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
args:
- apply
- -var-file
- vars/stage.tfvars
tf-plan:
envs:
dev:
desc: Terraform plan on dev
assert:
desc: Check if on dev workspace
ref: workspace # References goal above
expect: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
cmd: terraform
args:
- plan
- -var-file
- vars/dev.tfvars
stage:
desc: Terraform plan on stage
assert:
desc: Check if on stage workspace
ref: workspace # References goal above
expect: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
args:
- plan
- -var-file
- vars/stage.tfvars

tf-apply:
envs:
dev:
desc: Terraform apply on dev
assert:
desc: Check if on dev workspace
ref: workspace # References goal above
expect: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
cmd: terraform
args:
- apply
- -var-file
- vars/dev.tfvars
stage:
desc: Terraform apply on stage
assert:
desc: Check if on stage workspace
ref: workspace # References goal above
expect: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
args:
- apply
- -var-file
- vars/stage.tfvars
```

Simply type `goal` to see list of available goals and their dependencies:

```shell
$ goal
Available goals:
+----------------+--------------------------------+-----------------------------+--------------------------------+
| GOAL | CLI | DESCRIPTION | ASSERTIONS |
+----------------+--------------------------------+-----------------------------+--------------------------------+
| tf-apply-dev | terraform apply -var-file | Terraform apply on dev | [workspace] Check if on dev |
| | vars/dev.tfvars | | workspace |
| tf-apply-stage | terraform apply -var-file | Terraform apply on stage | [workspace] Check if on stage |
| | vars/stage.tfvars | | workspace |
| workspace | terraform workspace show | Current terraform workspace | |
+----------------+--------------------------------+-----------------------------+--------------------------------+
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| GOAL | ENVIRONMENT | CLI | DESCRIPTION | ASSERTIONS |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| workspace | | terraform workspace show | Current terraform workspace | |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| tf-plan | dev | terraform plan -var-file | Terraform plan on dev | [workspace] Check if on dev |
| | | vars/dev.tfvars | | workspace |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| tf-plan | stage | terraform plan -var-file | Terraform plan on stage | [workspace] Check if on stage |
| | | vars/stage.tfvars | | workspace |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| tf-apply | dev | terraform apply -var-file | Terraform apply on dev | [workspace] Check if on dev |
| | | vars/dev.tfvars | | workspace |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| tf-apply | stage | terraform apply -var-file | Terraform apply on stage | [workspace] Check if on stage |
| | | vars/stage.tfvars | | workspace |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
```
Let's see if _goal_ would allow us to apply terraform configuration on wrong environment:
```shell
$ terraform workspace show
dev
$ goal tf-apply-stage
$ goal tf-apply --on stage
⚙️ Exec tf-apply-stage
⌛ Check precondition: Check if on stage workspace
❗ Precondition failed: workspace
Expand Down
6 changes: 5 additions & 1 deletion cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ func (c *Commands) exec(name string, env string) {

command, exists := c.getWithEnv(name, env)
if exists {
info("⚙️ Exec %s", command.Name)
msg := fmt.Sprintf("🔨 Exec %s", command.Name)
if env != "" {
msg += " on " + env
}
info(msg)
if command.Assert != nil {
info("⌛ Check precondition: %s", command.Assert.Desc)
// TODO: env or !env?
Expand Down
4 changes: 0 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"io/ioutil"
Expand All @@ -40,9 +39,6 @@ var rootCmd = &cobra.Command{
return res, cobra.ShellCompDirectiveNoFileComp
},
Run: func(cmd *cobra.Command, args []string) {
if env != "" {
fmt.Println("Running on " + env)
}
if len(args) > 0 {
commands.exec(strings.TrimSpace(args[0]), env)
} else {
Expand Down
1 change: 1 addition & 0 deletions examples/terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.terraform/
35 changes: 35 additions & 0 deletions examples/terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Test it out

```shell
$ terraform workspace new dev
$ terraform workspace new stage
$ goal
Available goals:
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| GOAL | ENVIRONMENT | CLI | DESCRIPTION | ASSERTIONS |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| tf-plan | dev | terraform plan -var-file | Terraform plan on dev | [workspace] Check if on dev |
| | | vars/dev.tfvars | | workspace |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| tf-plan | stage | terraform plan -var-file | Terraform plan on stage | [workspace] Check if on stage |
| | | vars/stage.tfvars | | workspace |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| tf-apply | dev | terraform apply -var-file | Terraform apply on dev | [workspace] Check if on dev |
| | | vars/dev.tfvars | | workspace |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| tf-apply | stage | terraform apply -var-file | Terraform apply on stage | [workspace] Check if on stage |
| | | vars/stage.tfvars | | workspace |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
| workspace | | terraform workspace show | Current terraform workspace | |
+-----------+-------------+--------------------------------+-----------------------------+--------------------------------+
$ terraform workspace show
stage
$ goal tf-plan --on dev
Running on dev
⚙️ Exec tf-plan
⌛ Check precondition: Check if on dev workspace
❗ Precondition failed: workspace
Output: "stage"
Expected: "dev"
CLI: terraform workspace show
```
70 changes: 48 additions & 22 deletions examples/terraform/goal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,52 @@ workspace:
- workspace
- show

tf-apply-dev:
desc: Terraform apply on dev
assert:
desc: Check if on dev workspace
ref: workspace # References goal above
expect: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
cmd: terraform
args:
- apply
- -var-file
- vars/dev.tfvars
tf-plan:
envs:
dev:
desc: Terraform plan on dev
assert:
desc: Check if on dev workspace
ref: workspace # References goal above
expect: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
cmd: terraform
args:
- plan
- -var-file
- vars/dev.tfvars
stage:
desc: Terraform plan on stage
assert:
desc: Check if on stage workspace
ref: workspace # References goal above
expect: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
args:
- plan
- -var-file
- vars/stage.tfvars

tf-apply-stage:
desc: Terraform apply on stage
assert:
desc: Check if on stage workspace
ref: workspace # References goal above
expect: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
args:
- apply
- -var-file
- vars/stage.tfvars
tf-apply:
envs:
dev:
desc: Terraform apply on dev
assert:
desc: Check if on dev workspace
ref: workspace # References goal above
expect: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
cmd: terraform
args:
- apply
- -var-file
- vars/dev.tfvars
stage:
desc: Terraform apply on stage
assert:
desc: Check if on stage workspace
ref: workspace # References goal above
expect: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
args:
- apply
- -var-file
- vars/stage.tfvars
Empty file added examples/terraform/main.tf
Empty file.
Empty file.
Empty file.
101 changes: 39 additions & 62 deletions goal.yaml
Original file line number Diff line number Diff line change
@@ -1,79 +1,56 @@
# Note: this on is not implemented yet!
date:
desc: Current date
cmd: date

current-context:
desc: Current kubectl context
cmd: kubectl
workspace:
desc: Current terraform workspace
cmd: terraform
args:
- config
- current-context
- workspace
- show

helm-dry-run:
tf-plan:
envs:
dev:
desc: Dry run upgrade on dev
desc: Terraform plan on dev
assert:
desc: Check if on dev cluster
ref: current-context
expect: dev-cluster
cmd: helm
desc: Check if on dev workspace
ref: workspace # References goal above
expect: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
cmd: terraform
args:
- upgrade
- release-name
- -f
- values.yaml
- -f
- values/dev.yaml
- .
- --dry-run
- plan
- -var-file
- vars/dev.tfvars
stage:
desc: Dry run upgrade on stage
desc: Terraform plan on stage
assert:
desc: Check if on stage cluster
ref: current-context
expect: stage-cluster
cmd: helm
desc: Check if on stage workspace
ref: workspace # References goal above
expect: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
args:
- upgrade
- release-name
- -f
- values.yaml
- -f
- values/stage.yaml
- .
- --dry-run
- plan
- -var-file
- vars/stage.tfvars

helm-upgrade:
tf-apply:
envs:
dev:
desc: Upgrade on dev
desc: Terraform apply on dev
assert:
desc: Check if on dev cluster
ref: current-context
expect: dev-cluster
cmd: helm
desc: Check if on dev workspace
ref: workspace # References goal above
expect: dev # Checks whether trimmed output from 'ref' goal is equal to "dev"
cmd: terraform
args:
- upgrade
- release-name
- -f
- values.yaml
- -f
- values/dev.yaml
- .
- apply
- -var-file
- vars/dev.tfvars
stage:
desc: Upgrade on stage
desc: Terraform apply on stage
assert:
desc: Check if on stage cluster
ref: current-context
expect: stage-cluster
cmd: helm
desc: Check if on stage workspace
ref: workspace # References goal above
expect: stage # Checks whether trimmed output from 'ref' goal is equal to "stage"
cmd: terraform
args:
- upgrade
- release-name
- -f
- values.yaml
- -f
- values/stage.yaml
- .
- apply
- -var-file
- vars/stage.tfvars

0 comments on commit ec998ff

Please sign in to comment.