This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
forked from leominov/gitlab-project-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.go
66 lines (54 loc) · 1.78 KB
/
diff.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
package main
import (
"fmt"
"strings"
"github.com/spf13/viper"
"gopkg.in/d4l3k/messagediff.v1"
)
type changes struct {
Added []string
Modified []string
Removed []string
}
// computeDiff returns updated and added diff, removed diff, removed list, bool for change
func computeDiff(old, new map[string]interface{}) (string, string, changes, bool) {
var diffs strings.Builder
var removedDiffs strings.Builder
var changedItems changes
equal := true
diff, _ := messagediff.DeepDiff(old, new)
for p, a := range diff.Added {
equal = false
s := fmt.Sprintf("\t+ %v = %v\n", p.String(), mask(p.String(), a))
diffs.WriteString(formatter.Bold(formatter.Green(s)).String())
changedItems.Added = append(changedItems.Added, pathToKey(p))
}
for p, m := range diff.Modified {
oldVal := old
// for comparing lists of maps which messagediff always sets as modified
if fmt.Sprint(oldVal[pathToKey(p)]) == fmt.Sprint(m) {
continue
}
equal = false
s := fmt.Sprintf("\t~ %s = %v => %v\n", p.String(), mask(p.String(), oldVal[pathToKey(p)]), mask(p.String(), m))
diffs.WriteString(formatter.Bold(formatter.Yellow(s)).String())
changedItems.Modified = append(changedItems.Modified, pathToKey(p))
}
for p, r := range diff.Removed {
s := fmt.Sprintf("\t- %v = %v\n", p.String(), mask(p.String(), r))
removedDiffs.WriteString(formatter.Bold(formatter.Red(s)).String())
changedItems.Removed = append(changedItems.Removed, pathToKey(p))
}
return diffs.String(), removedDiffs.String(), changedItems, equal
}
func pathToKey(p *messagediff.Path) string {
return strings.Trim(p.String(), "[\"]")
}
func mask(key string, value interface{}) interface{} {
for _, k := range viper.GetStringSlice("mask") {
if strings.Contains(key, k) {
return "**redacted**"
}
}
return value
}