forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_for_push.go
226 lines (193 loc) · 6.48 KB
/
ui_for_push.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package ui
import (
"errors"
"fmt"
"reflect"
"sort"
"strings"
"code.cloudfoundry.org/cli/types"
"github.com/fatih/color"
)
var ErrValueMissmatch = errors.New("values provided were of different types")
type Change struct {
Header string
CurrentValue interface{}
NewValue interface{}
HiddenValue bool
}
// DisplayChangesForPush will display the set of changes via
// DisplayChangeForPush in the order given.
func (ui *UI) DisplayChangesForPush(changeSet []Change) error {
if len(changeSet) == 0 {
return nil
}
var columnWidth int
for _, change := range changeSet {
if width := wordSize(ui.TranslateText(change.Header)); width > columnWidth {
columnWidth = width
}
}
for _, change := range changeSet {
padding := columnWidth - wordSize(ui.TranslateText(change.Header)) + 3
err := ui.DisplayChangeForPush(change.Header, padding, change.HiddenValue, change.CurrentValue, change.NewValue)
if err != nil {
return err
}
}
return nil
}
// DisplayChangeForPush will display the header and old/new value with the
// appropriately red/green minuses and pluses.
func (ui *UI) DisplayChangeForPush(header string, stringTypePadding int, hiddenValue bool, originalValue interface{}, newValue interface{}) error {
ui.terminalLock.Lock()
defer ui.terminalLock.Unlock()
originalType := reflect.ValueOf(originalValue).Type()
newType := reflect.ValueOf(newValue).Type()
if originalType != newType {
return ErrValueMissmatch
}
offset := strings.Repeat(" ", stringTypePadding)
switch oVal := originalValue.(type) {
case int:
nVal := newValue.(int)
ui.displayDiffForInt(offset, header, oVal, nVal)
case types.NullInt:
nVal := newValue.(types.NullInt)
ui.displayDiffForNullInt(offset, header, oVal, nVal)
case string:
nVal := newValue.(string)
ui.displayDiffForString(offset, header, hiddenValue, oVal, nVal)
case []string:
nVal := newValue.([]string)
if len(oVal) == 0 && len(nVal) == 0 {
return nil
}
ui.displayDiffForStrings(offset, header, oVal, nVal)
case map[string]string:
nVal := newValue.(map[string]string)
if len(oVal) == 0 && len(nVal) == 0 {
return nil
}
ui.displayDiffForMapStringString(offset, header, oVal, nVal)
default:
panic(fmt.Sprintf("diff display does not have case for '%s'", header))
}
return nil
}
func (ui UI) displayDiffForInt(offset string, header string, oldValue int, newValue int) {
if oldValue != newValue {
formattedOld := fmt.Sprintf("- %s%s%d", ui.TranslateText(header), offset, oldValue)
formattedNew := fmt.Sprintf("+ %s%s%d", ui.TranslateText(header), offset, newValue)
if oldValue != 0 {
fmt.Fprintln(ui.Out, ui.modifyColor(formattedOld, color.New(color.FgRed)))
}
fmt.Fprintln(ui.Out, ui.modifyColor(formattedNew, color.New(color.FgGreen)))
} else {
fmt.Fprintf(ui.Out, " %s%s%d\n", ui.TranslateText(header), offset, oldValue)
}
}
func (ui UI) displayDiffForMapStringString(offset string, header string, oldMap map[string]string, newMap map[string]string) {
var oldKeys []string
for key := range oldMap {
oldKeys = append(oldKeys, key)
}
var newKeys []string
for key := range newMap {
newKeys = append(newKeys, key)
}
sortedKeys := sortedUniqueArray(oldKeys, newKeys)
fmt.Fprintf(ui.Out, " %s\n", ui.TranslateText(header))
for _, key := range sortedKeys {
newVal, ok := newMap[key]
if !ok {
formattedOld := fmt.Sprintf("- %s", key)
fmt.Fprintln(ui.Out, ui.modifyColor(formattedOld, color.New(color.FgRed)))
continue
}
oldVal, ok := oldMap[key]
if !ok {
formattedNew := fmt.Sprintf("+ %s", key)
fmt.Fprintln(ui.Out, ui.modifyColor(formattedNew, color.New(color.FgGreen)))
continue
}
if oldVal == newVal {
fmt.Fprintf(ui.Out, " %s\n", key)
} else {
formattedOld := fmt.Sprintf("- %s", key)
fmt.Fprintln(ui.Out, ui.modifyColor(formattedOld, color.New(color.FgRed)))
formattedNew := fmt.Sprintf("+ %s", key)
fmt.Fprintln(ui.Out, ui.modifyColor(formattedNew, color.New(color.FgGreen)))
}
}
}
func (ui UI) displayDiffForNullInt(offset string, header string, oldValue types.NullInt, newValue types.NullInt) {
if oldValue != newValue {
formattedOld := fmt.Sprintf("- %s%s%d", ui.TranslateText(header), offset, oldValue.Value)
formattedNew := fmt.Sprintf("+ %s%s%d", ui.TranslateText(header), offset, newValue.Value)
if oldValue.IsSet {
fmt.Fprintln(ui.Out, ui.modifyColor(formattedOld, color.New(color.FgRed)))
}
fmt.Fprintln(ui.Out, ui.modifyColor(formattedNew, color.New(color.FgGreen)))
} else {
fmt.Fprintf(ui.Out, " %s%s%d\n", ui.TranslateText(header), offset, oldValue.Value)
}
}
func (ui UI) displayDiffForString(offset string, header string, hiddenValue bool, oVal string, nVal string) {
if oVal != nVal {
var formattedOld, formattedNew string
if hiddenValue {
formattedOld = fmt.Sprintf("- %s%s%s", ui.TranslateText(header), offset, RedactedValue)
formattedNew = fmt.Sprintf("+ %s%s%s", ui.TranslateText(header), offset, RedactedValue)
} else {
formattedOld = fmt.Sprintf("- %s%s%s", ui.TranslateText(header), offset, oVal)
formattedNew = fmt.Sprintf("+ %s%s%s", ui.TranslateText(header), offset, nVal)
}
if oVal != "" {
fmt.Fprintln(ui.Out, ui.modifyColor(formattedOld, color.New(color.FgRed)))
}
if nVal != "" {
fmt.Fprintln(ui.Out, ui.modifyColor(formattedNew, color.New(color.FgGreen)))
}
} else {
if hiddenValue {
fmt.Fprintf(ui.Out, " %s%s%s\n", ui.TranslateText(header), offset, RedactedValue)
} else {
fmt.Fprintf(ui.Out, " %s%s%s\n", ui.TranslateText(header), offset, oVal)
}
}
}
func (ui UI) displayDiffForStrings(offset string, header string, oldList []string, newList []string) {
fmt.Fprintf(ui.Out, " %s\n", ui.TranslateText(header))
fullList := sortedUniqueArray(oldList, newList)
for _, item := range fullList {
inOld := existsIn(item, oldList)
inNew := existsIn(item, newList)
if inOld && inNew {
fmt.Fprintf(ui.Out, " %s\n", item)
} else if inOld {
formattedOld := fmt.Sprintf("- %s", item)
fmt.Fprintln(ui.Out, ui.modifyColor(formattedOld, color.New(color.FgRed)))
} else {
formattedNew := fmt.Sprintf("+ %s", item)
fmt.Fprintln(ui.Out, ui.modifyColor(formattedNew, color.New(color.FgGreen)))
}
}
}
func existsIn(str string, ary []string) bool {
for _, val := range ary {
if val == str {
return true
}
}
return false
}
func sortedUniqueArray(ary1 []string, ary2 []string) []string {
uniq := append([]string{}, ary1...)
for _, str := range ary2 {
if !existsIn(str, uniq) {
uniq = append(uniq, str)
}
}
sort.Strings(uniq)
return uniq
}