Brendan Abolivier 7 years ago
parent
commit
484f538cd7
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 changed files with 23 additions and 6 deletions
  1. 23
    6
      src/puller/puller.go

+ 23
- 6
src/puller/puller.go View File

25
 	client *grafana.Client,
25
 	client *grafana.Client,
26
 	repoURL string, clonePath string, privateKeyPath string,
26
 	repoURL string, clonePath string, privateKeyPath string,
27
 ) error {
27
 ) error {
28
-	dv := make(map[string]diffVersion)
29
-
30
-	dbVersions, err := getDashboardsVersions()
28
+	// Clone or pull the repo
29
+	repo, err := git.Sync(repoURL, clonePath, privateKeyPath)
31
 	if err != nil {
30
 	if err != nil {
32
 		return err
31
 		return err
33
 	}
32
 	}
34
 
33
 
35
-	repo, err := git.Sync(repoURL, clonePath, privateKeyPath)
34
+	w, err := repo.Worktree()
36
 	if err != nil {
35
 	if err != nil {
37
 		return err
36
 		return err
38
 	}
37
 	}
39
 
38
 
40
-	w, err := repo.Worktree()
39
+	// Get URIs for all known dashboards
40
+	uris, err := client.GetDashboardsURIs()
41
 	if err != nil {
41
 	if err != nil {
42
 		return err
42
 		return err
43
 	}
43
 	}
44
 
44
 
45
-	uris, err := client.GetDashboardsURIs()
45
+	dv := make(map[string]diffVersion)
46
+
47
+	// Load versions
48
+	dbVersions, err := getDashboardsVersions()
46
 	if err != nil {
49
 	if err != nil {
47
 		return err
50
 		return err
48
 	}
51
 	}
49
 
52
 
53
+	// Iterate over the dashboards URIs
50
 	for _, uri := range uris {
54
 	for _, uri := range uris {
55
+		// Retrieve the dashboard JSON
51
 		dashboard, err := client.GetDashboard(uri)
56
 		dashboard, err := client.GetDashboard(uri)
52
 		if err != nil {
57
 		if err != nil {
53
 			return err
58
 			return err
54
 		}
59
 		}
55
 
60
 
61
+		// Check if there's a version for this dashboard in the data loaded from
62
+		// the "versions.json" file. If there's a version and it's older (lower
63
+		// version number) than the version we just retrieved from the Grafana
64
+		// API, or if there's no known version (ok will be false), write the
65
+		// changes in the repo and add the modified file to the git index.
56
 		version, ok := dbVersions[dashboard.Slug]
66
 		version, ok := dbVersions[dashboard.Slug]
57
 		if !ok || dashboard.Version > version {
67
 		if !ok || dashboard.Version > version {
58
 			if err = addDashboardChangesToRepo(dashboard, w); err != nil {
68
 			if err = addDashboardChangesToRepo(dashboard, w); err != nil {
59
 				return err
69
 				return err
60
 			}
70
 			}
61
 
71
 
72
+			// We don't need to check for the value of ok because if ok is false
73
+			// version will be initialised to the 0-value of the int type, which
74
+			// is 0, so the previous version number will be considered to be 0,
75
+			// which is the behaviour we want.
62
 			dv[dashboard.Slug] = diffVersion{
76
 			dv[dashboard.Slug] = diffVersion{
63
 				oldVersion: version,
77
 				oldVersion: version,
64
 				newVersion: dashboard.Version,
78
 				newVersion: dashboard.Version,
71
 		return err
85
 		return err
72
 	}
86
 	}
73
 
87
 
88
+	// Check if there's uncommited changes, and if that's the case, commit them.
74
 	if !status.IsClean() {
89
 	if !status.IsClean() {
75
 		if err = commitNewVersions(dbVersions, dv, w); err != nil {
90
 		if err = commitNewVersions(dbVersions, dv, w); err != nil {
76
 			return err
91
 			return err
77
 		}
92
 		}
78
 	}
93
 	}
79
 
94
 
95
+	// Push the changes (we don't do it in the if clause above in case there are
96
+	// pending commits in the local repo that haven't been pushed yet).
80
 	if err = git.Push(repo, privateKeyPath); err != nil {
97
 	if err = git.Push(repo, privateKeyPath); err != nil {
81
 		return err
98
 		return err
82
 	}
99
 	}