-
Notifications
You must be signed in to change notification settings - Fork 11
/
util.go
148 lines (134 loc) 路 3.71 KB
/
util.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
/*
Copyright 漏 2023 Takafumi Miyanaga miya.org.0309@gmai.com
*/
package cmd
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/gookit/color"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
func executePlan(cmd *cobra.Command, option string) ([]string, error) {
action, _ := cmd.Flags().GetString("filter")
validValues := maps.Keys(actionPatternMap)
if err := validateFlagValue(action, validValues); action != "" && err != nil {
return nil, err
}
p, _ := cmd.Flags().GetInt("parallel")
planCmd := exec.Command("terraform", "plan", "-no-color", fmt.Sprintf("--parallelism=%d", p))
if option != "" {
planCmd = exec.Command("terraform", "plan", option, "-no-color", fmt.Sprintf("--parallelism=%d", p))
}
out, err := planCmd.CombinedOutput()
if err != nil {
color.Red.Println(string(out))
return nil, err
}
actionPattern := actionPatternMap[action]
resources := extractResource(out, actionPattern)
if len(resources) == 0 {
return nil, ErrNotFound
}
options := make([]string, 0, 100)
options = append(options, exitOpt)
return append(options, resources...), nil
}
func extractResource(input []byte, actionPattern string) []string {
re := regexp.MustCompile(`#\s(.*?)\s(.*)`)
matches := re.FindAllSubmatch(input, -1)
resourceActions := make([]string, 0, len(matches))
if actionPattern != "" {
for _, match := range matches {
resource := string(match[1])
action := string(match[2])
if actionPattern == action {
resourceAction := fmt.Sprintf("%s %s", resource, action)
resourceActions = append(resourceActions, resourceAction)
}
}
return resourceActions
}
for _, match := range matches {
resource := string(match[1])
action := string(match[2])
if slices.Contains(defaultActionPattern, action) {
resourceAction := fmt.Sprintf("%s %s", resource, action)
resourceActions = append(resourceActions, resourceAction)
}
}
return resourceActions
}
func dropAction(strs []string) []string {
var result []string
for _, s := range strs {
s = strings.TrimSpace(s)
parts := strings.Split(s, " ")
if len(parts) > 0 {
result = append(result, parts[0])
}
}
return result
}
func slice2String(slice []string) string {
var buffer bytes.Buffer
if len(slice) == 1 {
buffer.WriteString(slice[0])
return buffer.String()
}
buffer.WriteString(`{'`)
for i, item := range slice {
buffer.WriteString(item)
if i < len(slice)-1 {
buffer.WriteString("','")
}
}
buffer.WriteString(`'}`)
return buffer.String()
}
func genTargetCmd(cmd *cobra.Command, action, target string) bytes.Buffer {
var buf bytes.Buffer
buf.WriteString("terraform " + action + " -target=" + target)
p, _ := cmd.Flags().GetInt("parallel")
buf.WriteString(fmt.Sprintf(" --parallelism=%d", p))
return buf
}
func isYes(reader *bufio.Reader) bool {
color.Red.Print("Enter a value: ")
text, _ := reader.ReadString('\n')
return strings.TrimSpace(text) == "yes"
}
func confirm(buf bytes.Buffer) *exec.Cmd {
buf.WriteString(" -auto-approve")
confirm := exec.Command("sh", "-c", buf.String())
confirm.Stdout = os.Stdout
confirm.Stderr = os.Stderr
return confirm
}
func targetCmd(buf bytes.Buffer) *exec.Cmd {
cmd := exec.Command("sh", "-c", buf.String())
cmd.Stdout = os.Stdout
return cmd
}
func summary(items []string) error {
color.Green.Println("==============馃帀 Selected Resources 馃帀==============")
for _, v := range items {
fmt.Println(v)
}
color.Green.Println("====================================================")
return nil
}
func validateFlagValue(value string, validValues []string) error {
for _, v := range validValues {
if value == v {
return nil
}
}
return fmt.Errorf("invalid value: %s, valid values are: %v", value, validValues)
}