Browse Source

Clean up puller code

Brendan Abolivier 7 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
 import (
3
 import (
4
 	"flag"
4
 	"flag"
5
 	"os"
5
 	"os"
6
+
7
+	"grafana"
6
 )
8
 )
7
 
9
 
8
 var (
10
 var (
37
 		os.Exit(1)
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
 		panic(err)
44
 		panic(err)
42
 	}
45
 	}
43
 }
46
 }

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

3
 import (
3
 import (
4
 	"bytes"
4
 	"bytes"
5
 	"encoding/json"
5
 	"encoding/json"
6
-	"fmt"
7
 	"io/ioutil"
6
 	"io/ioutil"
8
 	"os"
7
 	"os"
9
 
8
 
10
 	"git"
9
 	"git"
11
 	"grafana"
10
 	"grafana"
11
+
12
+	gogit "gopkg.in/src-d/go-git.v4"
12
 )
13
 )
13
 
14
 
14
 type diffVersion struct {
15
 type diffVersion struct {
16
 	newVersion int
17
 	newVersion int
17
 }
18
 }
18
 
19
 
19
-func Pull() error {
20
+func Pull(client *grafana.Client) error {
20
 	dv := make(map[string]diffVersion)
21
 	dv := make(map[string]diffVersion)
21
 
22
 
22
 	dbVersions, err := getDashboardsVersions()
23
 	dbVersions, err := getDashboardsVersions()
24
 		return err
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
 	if err != nil {
29
 	if err != nil {
33
 		return err
30
 		return err
34
 	}
31
 	}
38
 		return err
35
 		return err
39
 	}
36
 	}
40
 
37
 
41
-	client := grafana.NewClient(*grafanaURL, *grafanaAPIKey)
42
 	uris, err := client.GetDashboardsURIs()
38
 	uris, err := client.GetDashboardsURIs()
43
 	if err != nil {
39
 	if err != nil {
44
 		return err
40
 		return err
52
 
48
 
53
 		version, ok := dbVersions[dashboard.Slug]
49
 		version, ok := dbVersions[dashboard.Slug]
54
 		if !ok || dashboard.Version > version {
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
 			); err != nil {
53
 			); err != nil {
60
 				return err
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
 	}
62
 	}
78
 
63
 
79
 	if !status.IsClean() {
64
 	if !status.IsClean() {
80
-		if err = writeVersions(dbVersions, dv); err != nil {
65
+		if err = commitNewVersions(dbVersions, dv, w); err != nil {
81
 			return err
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
 		return err
90
 		return err
96
 	}
91
 	}
97
 
92
 
93
+	(*dv)[dashboard.Slug] = diffVersion{
94
+		oldVersion: oldVersion,
95
+		newVersion: dashboard.Version,
96
+	}
97
+
98
 	return nil
98
 	return nil
99
 }
99
 }
100
 
100
 
123
 	indentedJSON, err = ioutil.ReadAll(buf)
123
 	indentedJSON, err = ioutil.ReadAll(buf)
124
 	return
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
 package main
1
 package main
2
 
2
 
3
 import (
3
 import (
4
+	"encoding/json"
5
+	"fmt"
4
 	"io/ioutil"
6
 	"io/ioutil"
5
 	"os"
7
 	"os"
6
-	"encoding/json"
8
+
9
+	"git"
10
+
11
+	gogit "gopkg.in/src-d/go-git.v4"
7
 )
12
 )
8
 
13
 
9
 func getDashboardsVersions() (versions map[string]int, err error) {
14
 func getDashboardsVersions() (versions map[string]int, err error) {
43
 	filename := *clonePath + "/versions.json"
48
 	filename := *clonePath + "/versions.json"
44
 	return rewriteFile(filename, indentedJSON)
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
+}