Browse Source

Clean up puller code

Brendan Abolivier 2 years ago
parent
commit
25e56112ee
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
3 changed files with 64 additions and 41 deletions
  1. 4
    1
      src/puller/main.go
  2. 29
    39
      src/puller/puller.go
  3. 31
    1
      src/puller/versions.go

+ 4
- 1
src/puller/main.go View File

@@ -3,6 +3,8 @@ package main
3 3
 import (
4 4
 	"flag"
5 5
 	"os"
6
+
7
+	"grafana"
6 8
 )
7 9
 
8 10
 var (
@@ -37,7 +39,8 @@ func main() {
37 39
 		os.Exit(1)
38 40
 	}
39 41
 
40
-	if err := Pull(); err != nil {
42
+	client := grafana.NewClient(*grafanaURL, *grafanaAPIKey)
43
+	if err := Pull(client); err != nil {
41 44
 		panic(err)
42 45
 	}
43 46
 }

+ 29
- 39
src/puller/puller.go View File

@@ -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
-}

+ 31
- 1
src/puller/versions.go View File

@@ -1,9 +1,14 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"encoding/json"
5
+	"fmt"
4 6
 	"io/ioutil"
5 7
 	"os"
6
-	"encoding/json"
8
+
9
+	"git"
10
+
11
+	gogit "gopkg.in/src-d/go-git.v4"
7 12
 )
8 13
 
9 14
 func getDashboardsVersions() (versions map[string]int, err error) {
@@ -43,3 +48,28 @@ func writeVersions(versions map[string]int, dv map[string]diffVersion) (err erro
43 48
 	filename := *clonePath + "/versions.json"
44 49
 	return rewriteFile(filename, indentedJSON)
45 50
 }
51
+
52
+func commitNewVersions(
53
+	versions map[string]int, dv map[string]diffVersion, worktree *gogit.Worktree,
54
+) (err error) {
55
+	if err = writeVersions(versions, dv); err != nil {
56
+		return err
57
+	}
58
+
59
+	if _, err = worktree.Add("versions.json"); err != nil {
60
+		return err
61
+	}
62
+
63
+	_, err = git.Commit(getCommitMessage(dv), worktree)
64
+	return
65
+}
66
+
67
+func getCommitMessage(dv map[string]diffVersion) string {
68
+	message := "Updated dashboards\n"
69
+
70
+	for slug, diff := range dv {
71
+		message += fmt.Sprintf("%s: %d => %d\n", slug, diff.oldVersion, diff.newVersion)
72
+	}
73
+
74
+	return message
75
+}