Skip to content

Commit

Permalink
feat: initial
Browse files Browse the repository at this point in the history
  • Loading branch information
lbennett-stacki committed May 1, 2024
0 parents commit cb356c3
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# terraform-roulette

Terraform if it was inspired by roulette.

50% chance of your `plan` or `apply` being an auto approved apply (great job!), 25% chance of your stack being destroyed.

Should only be used for Friday deploys.
13 changes: 13 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cli

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

func RunCli() {
command := os.Args[1]
args := os.Args[2:]

roulette.Play(command, args)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module terraform-roulette

go 1.22.2
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"terraform-roulette/cli"
)

func main() {
cli.RunCli()
}
62 changes: 62 additions & 0 deletions roulette/game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package roulette

import (
"math/rand"
"os"
"os/exec"
"strings"
)

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

func assertTerraformCommand(command string) {
cmd := exec.Command("terraform", command, "--help")
cmd.Stderr = os.Stderr

err := cmd.Run()
if err != nil {
os.Exit(1)
}
}

func execTerraform(commands []string, args []string) {
cmdArgs := append(commands, args...)
cmd := exec.Command("terraform", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin

err := cmd.Run()
if err != nil {
os.Exit(1)
}
}

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

return commands
}

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

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

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

commands = spinTheWheel(commands)

execTerraform(commands, args)
}
9 changes: 9 additions & 0 deletions terraform.tfstate
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 4,
"terraform_version": "1.6.6",
"serial": 1,
"lineage": "8350fc6f-2ab4-d272-2831-303c17f399ee",
"outputs": {},
"resources": [],
"check_results": null
}

0 comments on commit cb356c3

Please sign in to comment.