From cb356c30ecbedc1ce5cef377205e85931d6b626c Mon Sep 17 00:00:00 2001 From: Luke Bennett Date: Wed, 1 May 2024 01:12:48 +0100 Subject: [PATCH] feat: initial --- README.md | 7 ++++++ cli/cli.go | 13 ++++++++++ go.mod | 3 +++ main.go | 9 +++++++ roulette/game.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ terraform.tfstate | 9 +++++++ 6 files changed, 103 insertions(+) create mode 100644 README.md create mode 100644 cli/cli.go create mode 100644 go.mod create mode 100644 main.go create mode 100644 roulette/game.go create mode 100644 terraform.tfstate diff --git a/README.md b/README.md new file mode 100644 index 0000000..edfa440 --- /dev/null +++ b/README.md @@ -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. diff --git a/cli/cli.go b/cli/cli.go new file mode 100644 index 0000000..3cf9a99 --- /dev/null +++ b/cli/cli.go @@ -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) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6ff6300 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module terraform-roulette + +go 1.22.2 diff --git a/main.go b/main.go new file mode 100644 index 0000000..3416f56 --- /dev/null +++ b/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "terraform-roulette/cli" +) + +func main() { + cli.RunCli() +} diff --git a/roulette/game.go b/roulette/game.go new file mode 100644 index 0000000..6656052 --- /dev/null +++ b/roulette/game.go @@ -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) +} diff --git a/terraform.tfstate b/terraform.tfstate new file mode 100644 index 0000000..07f29ad --- /dev/null +++ b/terraform.tfstate @@ -0,0 +1,9 @@ +{ + "version": 4, + "terraform_version": "1.6.6", + "serial": 1, + "lineage": "8350fc6f-2ab4-d272-2831-303c17f399ee", + "outputs": {}, + "resources": [], + "check_results": null +}