-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
160 lines (145 loc) · 3.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/ktr0731/go-fuzzyfinder"
"golang.org/x/exp/slices"
)
const version = "0.1.0"
type Resource struct {
Name string
Filename string
Line string
}
// get all terraform resources available for removal
func getTerraformResourcesToRemove(working_dir string) []map[string]string {
var cmd strings.Builder
cmd.WriteString("terraform state list")
stdout, _, err := shellout(cmd.String(), true)
if err != nil {
fmt.Printf("No terraform state found\n")
os.Exit(1)
}
stateResources := strings.Split(stdout, "\n")
module, _ := tfconfig.LoadModule(working_dir)
managedResources := (*module).ManagedResources
resources := []map[string]string{}
for _, managedResource := range managedResources {
k := managedResource.Type
v := managedResource.Name
r := k + "." + v
if slices.Contains(stateResources, r) {
resource := make(map[string]string)
resource["Name"] = r
resource["Filename"] = filepath.Base(managedResource.Pos.Filename)
resource["Line"] = strconv.Itoa(managedResource.Pos.Line)
resources = append(resources, resource)
}
}
return resources
}
// get the current working directory
func getDir() string {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
return pwd
}
// build the fuzzyfinder menu
func fuzzyMenu(resources []Resource) []string {
resourcesToRemove := []string{}
idx, err := fuzzyfinder.FindMulti(
resources,
func(i int) string {
return resources[i].Name
},
fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
if i == -1 {
return ""
}
return fmt.Sprintf("Terraform resource: %s\nSource code file: %s\nLine: %s\n",
resources[i].Name,
resources[i].Filename,
resources[i].Line,
)
}))
if err != nil {
log.Fatal(err)
}
for _, i := range idx {
resourcesToRemove = append(resourcesToRemove, resources[i].Name)
}
return resourcesToRemove
}
// execute the given command in either bash or powershell depending on the detected os
func shellout(command string, silent bool) (string, string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := &exec.Cmd{}
if runtime.GOOS == "windows" {
cmd = exec.Command("powershell", "-command", command)
} else {
cmd = exec.Command("bash", "-c", command)
}
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if silent != true {
fmt.Println(cmd.Stdout)
fmt.Println(cmd.Stderr)
}
return stdout.String(), stderr.String(), err
}
func main() {
forceDestroy := flag.Bool("force", false, "If true, do not prompt for confirmation when removing a resource from the state")
flag.Parse()
workingDir := getDir()
resourcesAvailableForRemoval := getTerraformResourcesToRemove(workingDir)
if len(resourcesAvailableForRemoval) == 0 {
fmt.Printf("No resources available for removal were found\n")
os.Exit(1)
}
resources := []Resource{}
for _, resource := range resourcesAvailableForRemoval {
resources = append(resources, Resource{
resource["Name"], resource["Filename"], resource["Line"],
})
}
resourcesToRemove := fuzzyMenu(resources)
reader := bufio.NewReader(os.Stdin)
for _, resourceToRemove := range resourcesToRemove {
input := ""
for {
if *forceDestroy == false {
fmt.Printf("Remove %s from terraform state? [y/n]\n", resourceToRemove)
input, _ = reader.ReadString('\n')
input = strings.TrimSpace(input)
} else {
input = "y"
}
if input == "y" {
var cmd strings.Builder
tfImportCommand := "terraform state rm " + resourceToRemove
cmd.WriteString(tfImportCommand)
fmt.Printf("Executing: %s\n", cmd.String())
shellout(cmd.String(), false)
break
} else if input == "n" {
break
} else {
fmt.Printf("%s\n", "Invalid selection. Please select [y/n]")
}
}
}
}