|
@@ -12,12 +12,18 @@ import (
|
12
|
12
|
"github.com/sirupsen/logrus"
|
13
|
13
|
)
|
14
|
14
|
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
15
|
19
|
func Setup(cfg *config.Config, client *grafana.Client, delRemoved bool) error {
|
|
20
|
+
|
16
|
21
|
r, needsSync, err := git.NewRepository(cfg.Git)
|
17
|
22
|
if err != nil {
|
18
|
23
|
return err
|
19
|
24
|
}
|
20
|
25
|
|
|
26
|
+
|
21
|
27
|
if needsSync {
|
22
|
28
|
if err = r.Sync(false); err != nil {
|
23
|
29
|
return err
|
|
@@ -26,6 +32,8 @@ func Setup(cfg *config.Config, client *grafana.Client, delRemoved bool) error {
|
26
|
32
|
|
27
|
33
|
errs := make(chan error, 1)
|
28
|
34
|
|
|
35
|
+
|
|
36
|
+
|
29
|
37
|
go func() {
|
30
|
38
|
if err = poller(cfg, r, client, delRemoved); err != nil {
|
31
|
39
|
errs <- err
|
|
@@ -37,10 +45,23 @@ func Setup(cfg *config.Config, client *grafana.Client, delRemoved bool) error {
|
37
|
45
|
return err
|
38
|
46
|
}
|
39
|
47
|
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
40
|
58
|
func poller(
|
41
|
59
|
cfg *config.Config, repo *git.Repository, client *grafana.Client,
|
42
|
60
|
delRemoved bool,
|
43
|
61
|
) (err error) {
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
44
|
65
|
latestCommit, err := repo.GetLatestCommit()
|
45
|
66
|
if err != nil {
|
46
|
67
|
return
|
|
@@ -51,38 +72,58 @@ func poller(
|
51
|
72
|
return
|
52
|
73
|
}
|
53
|
74
|
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
54
|
78
|
previousCommit := latestCommit
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
55
|
82
|
previousFilesContents := filesContents
|
56
|
83
|
|
|
84
|
+
|
57
|
85
|
for {
|
|
86
|
+
|
58
|
87
|
if err = repo.Sync(true); err != nil {
|
59
|
88
|
return
|
60
|
89
|
}
|
61
|
90
|
|
|
91
|
+
|
|
92
|
+
|
62
|
93
|
latestCommit, err = repo.GetLatestCommit()
|
63
|
94
|
if err != nil {
|
64
|
95
|
return
|
65
|
96
|
}
|
66
|
97
|
|
|
98
|
+
|
67
|
99
|
if previousCommit.Hash.String() != latestCommit.Hash.String() {
|
68
|
100
|
logrus.WithFields(logrus.Fields{
|
69
|
101
|
"previous_hash": previousCommit.Hash.String(),
|
70
|
102
|
"new_hash": latestCommit.Hash.String(),
|
71
|
103
|
}).Info("New commit(s) detected")
|
72
|
104
|
|
|
105
|
+
|
73
|
106
|
filesContents, err = repo.GetFilesContentsAtCommit(latestCommit)
|
74
|
107
|
if err != nil {
|
75
|
108
|
return err
|
76
|
109
|
}
|
77
|
110
|
|
|
111
|
+
|
|
112
|
+
|
78
|
113
|
modified, removed, err := repo.GetModifiedAndRemovedFiles(previousCommit, latestCommit)
|
79
|
114
|
if err != nil {
|
80
|
115
|
return err
|
81
|
116
|
}
|
82
|
117
|
|
|
118
|
+
|
|
119
|
+
|
83
|
120
|
mergedContents := mergeContents(modified, removed, filesContents, previousFilesContents)
|
|
121
|
+
|
|
122
|
+
|
84
|
123
|
common.PushFiles(modified, mergedContents, client)
|
85
|
124
|
|
|
125
|
+
|
|
126
|
+
|
86
|
127
|
if delRemoved {
|
87
|
128
|
common.DeleteDashboards(removed, mergedContents, client)
|
88
|
129
|
}
|
|
@@ -99,22 +140,36 @@ func poller(
|
99
|
140
|
}
|
100
|
141
|
}
|
101
|
142
|
|
|
143
|
+
|
102
|
144
|
previousCommit = latestCommit
|
103
|
145
|
previousFilesContents = filesContents
|
|
146
|
+
|
|
147
|
+
|
104
|
148
|
time.Sleep(time.Duration(cfg.Pusher.Config.Interval) * time.Second)
|
105
|
149
|
}
|
106
|
150
|
}
|
107
|
151
|
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
108
|
161
|
func mergeContents(
|
109
|
162
|
modified []string, removed []string,
|
110
|
163
|
filesContents map[string][]byte, previousFilesContents map[string][]byte,
|
111
|
164
|
) (merged map[string][]byte) {
|
112
|
165
|
merged = make(map[string][]byte)
|
113
|
166
|
|
|
167
|
+
|
114
|
168
|
for _, modifiedFile := range modified {
|
115
|
169
|
merged[modifiedFile] = filesContents[modifiedFile]
|
116
|
170
|
}
|
117
|
171
|
|
|
172
|
+
|
118
|
173
|
for _, removedFile := range removed {
|
119
|
174
|
merged[removedFile] = previousFilesContents[removedFile]
|
120
|
175
|
}
|