forked from mihirsoni/odfe-monitor-cli
/
diff.go
96 lines (88 loc) · 3.35 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
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
package commands
import (
"fmt"
"os"
"github.com/mihirsoni/odfe-monitor-cli/destination"
"github.com/mihirsoni/odfe-monitor-cli/monitor"
mapset "github.com/deckarep/golang-set"
"github.com/fatih/color"
"github.com/sergi/go-diff/diffmatchpatch"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
var diff = &cobra.Command{
Use: "diff",
Short: "Show changes between remote and local monitors",
Long: `This command will show difference between remote and local monitors.`,
Run: showDiff,
}
var dmp = diffmatchpatch.New()
func showDiff(cmd *cobra.Command, args []string) {
destinations, err := destination.GetRemote(esClient)
check(err)
localMonitors, localMonitorSet, err := monitor.GetAllLocal(rootDir)
check(err)
if localMonitorSet.Cardinality() == 0 {
log.Info("There are no monitors")
os.Exit(1)
}
allRemoteMonitors, remoteMonitorsSet, err := monitor.GetAllRemote(esClient, destinations)
check(err)
unTrackedMonitors := remoteMonitorsSet.Difference(localMonitorSet)
allNewMonitors := localMonitorSet.Difference(remoteMonitorsSet)
allCommonMonitors := remoteMonitorsSet.Intersect(localMonitorSet)
changedMonitors := mapset.NewSet()
allCommonMonitorsIt := allCommonMonitors.Iterator()
for commonMonitor := range allCommonMonitorsIt.C {
monitorName := commonMonitor.(string)
if isMonitorChanged(localMonitors[monitorName], allRemoteMonitors[monitorName]) == true {
changedMonitors.Add(commonMonitor)
}
}
hasDeleted := unTrackedMonitors.Cardinality() > 0
hasModified := changedMonitors.Cardinality() > 0
hasCreated := allNewMonitors.Cardinality() > 0
//All New Monitors
if hasCreated {
log.Debug("New monitors to be publushed", allNewMonitors)
fmt.Println("---------------------------------------------------------")
fmt.Println(" These monitors are currently missing in alerting ")
fmt.Println("---------------------------------------------------------")
for newMonitor := range allNewMonitors.Iterator().C {
monitorName := newMonitor.(string)
localYaml, _ := yaml.Marshal(localMonitors[monitorName])
color.Green(string(localYaml))
}
}
if hasModified {
log.Debug("Common monitors to be updated", allCommonMonitors)
fmt.Println("---------------------------------------------------------")
fmt.Println(" These are existing monitors, which have been modified ")
fmt.Println("---------------------------------------------------------")
for monitorToBeUpdated := range changedMonitors.Iterator().C {
monitorName := monitorToBeUpdated.(string)
localYaml, err := yaml.Marshal(localMonitors[monitorName])
check(err)
remoteYml, err := yaml.Marshal(allRemoteMonitors[monitorName])
check(err)
diffs := dmp.DiffMain(string(remoteYml), string(localYaml), true)
diffs = dmp.DiffCleanupSemantic(diffs)
fmt.Println(dmp.DiffPrettyText(diffs))
}
}
if hasDeleted {
log.Debug("Un-tracked monitors to be deleted", unTrackedMonitors)
fmt.Println("---------------------------------------------------------")
fmt.Println(" These monitors will be deleted if push with the --delete flag")
fmt.Println("---------------------------------------------------------")
for newMonitor := range unTrackedMonitors.Iterator().C {
monitorName := newMonitor.(string)
remoteYml, _ := yaml.Marshal(allRemoteMonitors[monitorName])
color.Red(string(remoteYml))
}
}
}
func init() {
rootCmd.AddCommand(diff)
}