Skip to content

Commit fd4f59a

Browse files
author
Josh Newman
committed
add values to diff
1 parent 35a74c2 commit fd4f59a

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ Usage:
165165
ezparams diff <path 1> <path 2> [flags]
166166

167167
Flags:
168-
-h, --help help for diff
168+
-h, --help help for diff
169+
-v, --values show value diffs
169170

170171
Global Flags:
171172
--config string config file (default is $HOME/.ezparams.yaml)

cmd/diff.go

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,26 @@ var diffCmd = &cobra.Command{
2727
}
2828

2929
type diffRow struct {
30-
Key string
31-
Path int
30+
Key string
31+
Value1 string
32+
Value2 string
33+
Path int
3234
}
3335

3436
func runDiffCmd(cmd *cobra.Command, args []string) {
3537
path1 := args[0]
3638
path2 := args[1]
3739

40+
showValues, _ := cmd.Flags().GetBool("values")
41+
decrypt, _ := cmd.Flags().GetBool("decrypt")
42+
3843
fmt.Println(text.FgBlue.Sprintf("Getting diff between \"%s\" and \"%s\"...", path1, path2))
3944

4045
options := &getParamsOptions{
4146
Client: ssm.New(session),
4247
Path: &path1,
4348
Recursive: aws.Bool(true),
49+
Decrypt: &decrypt,
4450
}
4551

4652
params1 := getParams(options, []*ssm.Parameter{}, nil)
@@ -51,22 +57,32 @@ func runDiffCmd(cmd *cobra.Command, args []string) {
5157
tw := table.NewWriter()
5258
tw.Style().Format.Header = text.FormatLower
5359

54-
tw.AppendHeader(table.Row{path1, path2})
60+
headerRow := table.Row{path1, path2}
61+
62+
if showValues {
63+
headerRow = insertColumn(headerRow, 1, "Value")
64+
headerRow = insertColumn(headerRow, 3, "Value")
65+
}
66+
67+
tw.AppendHeader(headerRow)
5568

5669
var rows []*diffRow
5770

5871
for i := range params1 {
5972
name := *params1[i].Name
73+
val := *params1[i].Value
6074
key := strings.Replace(name, name[0:len(path1)+1], "", -1)
6175

6276
rows = append(rows, &diffRow{
63-
Key: key,
64-
Path: 1,
77+
Key: key,
78+
Value1: val,
79+
Path: 1,
6580
})
6681
}
6782

6883
for i := range params2 {
6984
name := *params2[i].Name
85+
val := *params2[i].Value
7086
key := strings.Replace(name, name[0:len(path2)+1], "", -1)
7187

7288
updated := false
@@ -76,47 +92,68 @@ func runDiffCmd(cmd *cobra.Command, args []string) {
7692
row := rows[j]
7793

7894
if row.Key == key {
95+
row.Value2 = val
7996
row.Path = 0
8097
updated = true
8198
}
8299
}
83100

84101
if !updated {
85102
rows = append(rows, &diffRow{
86-
Key: key,
87-
Path: 2,
103+
Key: key,
104+
Value2: val,
105+
Path: 2,
88106
})
89107
}
90108
}
91109

92110
sortDiffRows(rows)
93111

94112
for i := range rows {
95-
var text1 string
96-
var text2 string
113+
var key1 string
114+
var key2 string
97115

98116
key := rows[i].Key
99117
greenKey := text.FgGreen.Sprint(key)
100118
redKey := text.FgRed.Sprint(key)
101119

102120
switch path := rows[i].Path; path {
103121
case 0:
104-
text1 = key
105-
text2 = key
122+
key1 = key
123+
key2 = key
106124
case 1:
107-
text1 = greenKey
108-
text2 = redKey
125+
key1 = greenKey
126+
key2 = redKey
109127
case 2:
110-
text1 = redKey
111-
text2 = greenKey
128+
key1 = redKey
129+
key2 = greenKey
112130
}
113131

114-
tw.AppendRow(table.Row{text1, text2})
132+
row := table.Row{key1, key2}
133+
134+
if showValues {
135+
value1 := rows[i].Value1
136+
value2 := rows[i].Value2
137+
138+
// if both exist and there is a difference
139+
if value1 != "" && value2 != "" && value1 != value2 {
140+
value1 = text.FgYellow.Sprint(value1)
141+
value2 = text.FgYellow.Sprint(value2)
142+
}
143+
144+
row = insertColumn(row, 1, value1)
145+
row = insertColumn(row, 3, value2)
146+
}
147+
148+
tw.AppendRow(row)
115149
}
116150

117151
fmt.Println(tw.Render())
118152
}
119153

120154
func init() {
155+
diffCmd.Flags().BoolP("values", "v", false, "show value diffs")
156+
diffCmd.Flags().BoolP("decrypt", "d", true, "decrypt \"SecureString\" values")
157+
121158
rootCmd.AddCommand(diffCmd)
122159
}

0 commit comments

Comments
 (0)