|
@@ -3,12 +3,13 @@ package main
|
3
|
3
|
import (
|
4
|
4
|
"bytes"
|
5
|
5
|
"encoding/json"
|
6
|
|
- "fmt"
|
7
|
6
|
"io/ioutil"
|
8
|
7
|
"os"
|
9
|
8
|
|
10
|
9
|
"git"
|
11
|
10
|
"grafana"
|
|
11
|
+
|
|
12
|
+ gogit "gopkg.in/src-d/go-git.v4"
|
12
|
13
|
)
|
13
|
14
|
|
14
|
15
|
type diffVersion struct {
|
|
@@ -16,7 +17,7 @@ type diffVersion struct {
|
16
|
17
|
newVersion int
|
17
|
18
|
}
|
18
|
19
|
|
19
|
|
-func Pull() error {
|
|
20
|
+func Pull(client *grafana.Client) error {
|
20
|
21
|
dv := make(map[string]diffVersion)
|
21
|
22
|
|
22
|
23
|
dbVersions, err := getDashboardsVersions()
|
|
@@ -24,11 +25,7 @@ func Pull() error {
|
24
|
25
|
return err
|
25
|
26
|
}
|
26
|
27
|
|
27
|
|
- repo, err := git.Sync(
|
28
|
|
- *repoURL,
|
29
|
|
- *clonePath,
|
30
|
|
- *privateKeyPath,
|
31
|
|
- )
|
|
28
|
+ repo, err := git.Sync(*repoURL, *clonePath, *privateKeyPath)
|
32
|
29
|
if err != nil {
|
33
|
30
|
return err
|
34
|
31
|
}
|
|
@@ -38,7 +35,6 @@ func Pull() error {
|
38
|
35
|
return err
|
39
|
36
|
}
|
40
|
37
|
|
41
|
|
- client := grafana.NewClient(*grafanaURL, *grafanaAPIKey)
|
42
|
38
|
uris, err := client.GetDashboardsURIs()
|
43
|
39
|
if err != nil {
|
44
|
40
|
return err
|
|
@@ -52,22 +48,11 @@ func Pull() error {
|
52
|
48
|
|
53
|
49
|
version, ok := dbVersions[dashboard.Slug]
|
54
|
50
|
if !ok || dashboard.Version > version {
|
55
|
|
- slugExt := dashboard.Slug + ".json"
|
56
|
|
- if err = rewriteFile(
|
57
|
|
- *clonePath+"/"+slugExt,
|
58
|
|
- dashboard.RawJSON,
|
|
51
|
+ if err = addDashboardChangesToRepo(
|
|
52
|
+ dashboard, version, w, &dv,
|
59
|
53
|
); err != nil {
|
60
|
54
|
return err
|
61
|
55
|
}
|
62
|
|
-
|
63
|
|
- if _, err = w.Add(slugExt); err != nil {
|
64
|
|
- return err
|
65
|
|
- }
|
66
|
|
-
|
67
|
|
- dv[dashboard.Slug] = diffVersion{
|
68
|
|
- oldVersion: version,
|
69
|
|
- newVersion: dashboard.Version,
|
70
|
|
- }
|
71
|
56
|
}
|
72
|
57
|
}
|
73
|
58
|
|
|
@@ -77,24 +62,39 @@ func Pull() error {
|
77
|
62
|
}
|
78
|
63
|
|
79
|
64
|
if !status.IsClean() {
|
80
|
|
- if err = writeVersions(dbVersions, dv); err != nil {
|
|
65
|
+ if err = commitNewVersions(dbVersions, dv, w); err != nil {
|
81
|
66
|
return err
|
82
|
67
|
}
|
|
68
|
+ }
|
83
|
69
|
|
84
|
|
- if _, err = w.Add("versions.json"); err != nil {
|
85
|
|
- return err
|
86
|
|
- }
|
|
70
|
+ if err = git.Push(repo, *privateKeyPath); err != nil {
|
|
71
|
+ return err
|
|
72
|
+ }
|
87
|
73
|
|
88
|
|
- if _, err = git.Commit(getCommitMessage(dv), w); err != nil {
|
89
|
|
- return err
|
90
|
|
- }
|
|
74
|
+ return nil
|
|
75
|
+}
|
91
|
76
|
|
|
77
|
+func addDashboardChangesToRepo(
|
|
78
|
+ dashboard *grafana.Dashboard, oldVersion int, worktree *gogit.Worktree,
|
|
79
|
+ dv *map[string]diffVersion,
|
|
80
|
+) error {
|
|
81
|
+ slugExt := dashboard.Slug + ".json"
|
|
82
|
+ if err := rewriteFile(
|
|
83
|
+ *clonePath+"/"+slugExt,
|
|
84
|
+ dashboard.RawJSON,
|
|
85
|
+ ); err != nil {
|
|
86
|
+ return err
|
92
|
87
|
}
|
93
|
88
|
|
94
|
|
- if err = git.Push(repo, *privateKeyPath); err != nil {
|
|
89
|
+ if _, err := worktree.Add(slugExt); err != nil {
|
95
|
90
|
return err
|
96
|
91
|
}
|
97
|
92
|
|
|
93
|
+ (*dv)[dashboard.Slug] = diffVersion{
|
|
94
|
+ oldVersion: oldVersion,
|
|
95
|
+ newVersion: dashboard.Version,
|
|
96
|
+ }
|
|
97
|
+
|
98
|
98
|
return nil
|
99
|
99
|
}
|
100
|
100
|
|
|
@@ -123,13 +123,3 @@ func indent(srcJSON []byte) (indentedJSON []byte, err error) {
|
123
|
123
|
indentedJSON, err = ioutil.ReadAll(buf)
|
124
|
124
|
return
|
125
|
125
|
}
|
126
|
|
-
|
127
|
|
-func getCommitMessage(dv map[string]diffVersion) string {
|
128
|
|
- message := "Updated dashboards\n"
|
129
|
|
-
|
130
|
|
- for slug, diff := range dv {
|
131
|
|
- message += fmt.Sprintf("%s: %d => %d\n", slug, diff.oldVersion, diff.newVersion)
|
132
|
|
- }
|
133
|
|
-
|
134
|
|
- return message
|
135
|
|
-}
|