Skip to content

Commit

Permalink
feat: spin on plan and destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
lbennett-stacki committed May 1, 2024
1 parent b458b4a commit 12e1065
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
13 changes: 0 additions & 13 deletions cli/cli.go

This file was deleted.

17 changes: 17 additions & 0 deletions cli/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cli

import (
"os"
)

func ParseArgs() (string, []string) {
command := ""
args := []string{}

if len(os.Args) > 1 {
command = os.Args[1]
args = os.Args[2:]
}

return command, args
}
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package main

import (
"terraform-roulette/cli"
"terraform-roulette/roulette"
)

func main() {
cli.RunCli()
command, args := cli.ParseArgs()

roulette.Play(command, args)
}
27 changes: 22 additions & 5 deletions roulette/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"strings"
)

var options = map[string]int{
var overrides = map[string]int{
"destroy": 25,
"apply --auto-approve": 50,
}

var overrideables = []string{"apply", "plan", "destroy"}

func assertTerraformCommand(command string) {
cmd := exec.Command("terraform", command, "--help")
cmd.Stderr = os.Stderr
Expand All @@ -36,22 +38,37 @@ func execTerraform(commands []string, args []string) {
}

func spinTheWheel(commands []string) []string {
for option, weight := range options {
if rand.Intn(100) < weight {
commands = strings.Split(option, " ")
target := rand.Intn(100)

for override, weight := range overrides {
if target < weight {
commands = strings.Split(override, " ")
break
}
}

return commands
}

func isOverridable(command string) bool {
result := false

for _, overrideable := range overrideables {
if command == overrideable {
result = true
break
}
}

return result
}

func Play(command string, args []string) {
assertTerraformCommand(command)

commands := strings.Split(command, " ")

if !strings.HasPrefix(command, "apply") {
if !isOverridable(command) {
execTerraform(commands, args)
os.Exit(0)
}
Expand Down

0 comments on commit 12e1065

Please sign in to comment.