-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
194 lines (160 loc) · 3.53 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"flag"
"fmt"
"os"
"io/ioutil"
"encoding/json"
"sort"
"strings"
"github.com/fatih/color"
)
const (
No_Op = "no-op"
Create = "create"
Read = "read"
Update = "update"
Delete = "delete"
// Pseudo state
Recreate = "recreate"
)
var colorMap = map[string]color.Attribute{
No_Op: color.FgWhite,
Create: color.FgGreen,
Read: color.FgWhite,
Update: color.FgYellow,
Delete: color.FgRed,
Recreate: color.FgRed,
}
var (
excludeReads bool
noTrimPrefix bool
no_op = []string{No_Op}
create = []string{Create}
read = []string{Read}
update = []string{Update}
deleteCreate = []string{Delete, Create}
createDelete = []string{Create, Delete}
delete_ = []string{Delete}
)
func init() {
usage := "Exclude read operations from being displayed"
flag.BoolVar(&excludeReads, "exclude-reads", false, usage)
flag.BoolVar(&excludeReads, "x", false, usage+" (shorthand)")
flag.BoolVar(&noTrimPrefix, "no-trim", false, "don't trim the common prefix from addresses")
}
type Change struct {
Actions []string `json:"actions"`
}
type ChangeRepr struct {
Address string `json:"address"`
Change Change `json:"change"`
}
type Plan struct {
ResourceChanges []ChangeRepr `json:"resource_changes"`
}
type Output struct {
Address string
Type string
}
func (o Output) String() string {
return fmt.Sprintf("(%8v) %v", o.Type, o.Address)
}
func (o Output) Print() {
color.Set(colorMap[o.Type])
defer color.Unset()
fmt.Printf("(%8v) %v\n", o.Type, o.Address)
}
func ok(err error, msg string) {
if err != nil {
fmt.Printf("%v: %v", msg, err)
os.Exit(1)
}
}
func arrayEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func getOutput(change ChangeRepr) *Output {
o := &Output{Address: change.Address}
switch {
case arrayEqual(change.Change.Actions, no_op):
o.Type = No_Op
case arrayEqual(change.Change.Actions, create):
o.Type = Create
case arrayEqual(change.Change.Actions, read):
o.Type = Read
case arrayEqual(change.Change.Actions, update):
o.Type = Update
case arrayEqual(change.Change.Actions, deleteCreate):
o.Type = Recreate
case arrayEqual(change.Change.Actions, createDelete):
o.Type = Recreate
case arrayEqual(change.Change.Actions, delete_):
o.Type = Delete
default:
fmt.Printf("Unknown change sequence of %v for %v", change.Change.Actions, change.Address)
return nil
}
return o
}
func trimPrefix(changes []*Output) {
addresses := []string{}
for _, c := range changes {
addresses = append(addresses, c.Address)
}
sort.Strings(addresses)
if len(addresses) < 2 {
return
}
charCount := len(addresses[0])
if len(addresses[1]) < len(addresses[0]) {
charCount = len(addresses[1])
}
prefix := ""
for i := 0; i < charCount; i++ {
if addresses[0][i] == addresses[1][i] {
prefix = prefix + fmt.Sprintf("%c", addresses[0][i])
} else {
break
}
}
for _, c := range changes {
c.Address = strings.TrimPrefix(c.Address, prefix)
}
}
func main() {
flag.Parse()
bytes, err := ioutil.ReadAll(os.Stdin)
ok(err, "Error reading from stdin")
var plan Plan
err = json.Unmarshal(bytes, &plan)
ok(err, "Error unmarshalling")
changes := []*Output{}
for _, change := range plan.ResourceChanges {
output := getOutput(change)
if output == nil {
continue
}
if output.Type == No_Op {
continue
}
if output.Type == Read && excludeReads {
continue
}
changes = append(changes, output)
}
if !noTrimPrefix {
trimPrefix(changes)
}
for _, change := range changes {
change.Print()
}
}