Просмотр исходного кода

Move pull into a non-main function

Brendan Abolivier 6 лет назад
Родитель
Сommit
0c9a6d112d
Подписано: Brendan Abolivier <contact@brendanabolivier.com> Идентификатор ключа GPG: 8EF1500759F70623
2 измененных файлов: 136 добавлений и 123 удалений
  1. 1
    123
      src/puller/main.go
  2. 135
    0
      src/puller/puller.go

+ 1
- 123
src/puller/main.go Просмотреть файл

@@ -1,22 +1,10 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"bytes"
5
-	"encoding/json"
6 4
 	"flag"
7
-	"fmt"
8
-	"io/ioutil"
9 5
 	"os"
10
-
11
-	"git"
12
-	"grafana"
13 6
 )
14 7
 
15
-type diffVersion struct {
16
-	oldVersion int
17
-	newVersion int
18
-}
19
-
20 8
 var (
21 9
 	grafanaURL     = flag.String("grafana-url", "", "Base URL of the Grafana instance")
22 10
 	grafanaAPIKey  = flag.String("api-key", "", "API key to use in authenticated requests")
@@ -26,8 +14,6 @@ var (
26 14
 )
27 15
 
28 16
 func main() {
29
-	dv := make(map[string]diffVersion)
30
-
31 17
 	flag.Parse()
32 18
 
33 19
 	if *grafanaURL == "" {
@@ -51,115 +37,7 @@ func main() {
51 37
 		os.Exit(1)
52 38
 	}
53 39
 
54
-	dbVersions, err := getDashboardsVersions()
55
-	if err != nil {
56
-		panic(err)
57
-	}
58
-
59
-	repo, err := git.Sync(
60
-		*repoURL,
61
-		*clonePath,
62
-		*privateKeyPath,
63
-	)
64
-	if err != nil {
65
-		panic(err)
66
-	}
67
-
68
-	w, err := repo.Worktree()
69
-	if err != nil {
70
-		panic(err)
71
-	}
72
-
73
-	client := grafana.NewClient(*grafanaURL, *grafanaAPIKey)
74
-	uris, err := client.GetDashboardsURIs()
75
-	if err != nil {
76
-		panic(err)
77
-	}
78
-
79
-	for _, uri := range uris {
80
-		dashboard, err := client.GetDashboard(uri)
81
-		if err != nil {
82
-			panic(err)
83
-		}
84
-
85
-		version, ok := dbVersions[dashboard.Slug]
86
-		if !ok || dashboard.Version > version {
87
-			slugExt := dashboard.Slug + ".json"
88
-			if err = rewriteFile(
89
-				*clonePath+"/"+slugExt,
90
-				dashboard.RawJSON,
91
-			); err != nil {
92
-				panic(err)
93
-			}
94
-
95
-			if _, err = w.Add(slugExt); err != nil {
96
-				panic(err)
97
-			}
98
-
99
-			dv[dashboard.Slug] = diffVersion{
100
-				oldVersion: version,
101
-				newVersion: dashboard.Version,
102
-			}
103
-		}
104
-	}
105
-
106
-	status, err := w.Status()
107
-	if err != nil {
108
-		panic(err)
109
-	}
110
-
111
-	if !status.IsClean() {
112
-		if err = writeVersions(dbVersions, dv); err != nil {
113
-			panic(err)
114
-		}
115
-
116
-		if _, err = w.Add("versions.json"); err != nil {
117
-			panic(err)
118
-		}
119
-
120
-		if _, err = git.Commit(getCommitMessage(dv), w); err != nil {
121
-			panic(err)
122
-		}
123
-
124
-	}
125
-
126
-	if err = git.Push(repo, *privateKeyPath); err != nil {
40
+	if err := Pull(); err != nil {
127 41
 		panic(err)
128 42
 	}
129 43
 }
130
-
131
-func rewriteFile(filename string, content []byte) error {
132
-	if err := os.Remove(filename); err != nil {
133
-		pe, ok := err.(*os.PathError)
134
-		if !ok || pe.Err.Error() != "no such file or directory" {
135
-			return err
136
-		}
137
-	}
138
-
139
-	indentedContent, err := indent(content)
140
-	if err != nil {
141
-		return err
142
-	}
143
-
144
-	return ioutil.WriteFile(filename, indentedContent, 0644)
145
-}
146
-
147
-func indent(srcJSON []byte) (indentedJSON []byte, err error) {
148
-	buf := bytes.NewBuffer(nil)
149
-	if err = json.Indent(buf, srcJSON, "", "\t"); err != nil {
150
-		return
151
-	}
152
-
153
-	indentedJSON, err = ioutil.ReadAll(buf)
154
-	return
155
-}
156
-
157
-func getCommitMessage(dv map[string]diffVersion) string {
158
-	message := "Updated dashboards\n"
159
-
160
-	for slug, diff := range dv {
161
-		message += fmt.Sprintf("%s: %d => %d\n", slug, diff.oldVersion, diff.newVersion)
162
-	}
163
-
164
-	return message
165
-}

+ 135
- 0
src/puller/puller.go Просмотреть файл

@@ -0,0 +1,135 @@
1
+package main
2
+
3
+import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"fmt"
7
+	"io/ioutil"
8
+	"os"
9
+
10
+	"git"
11
+	"grafana"
12
+)
13
+
14
+type diffVersion struct {
15
+	oldVersion int
16
+	newVersion int
17
+}
18
+
19
+func Pull() error {
20
+	dv := make(map[string]diffVersion)
21
+
22
+	dbVersions, err := getDashboardsVersions()
23
+	if err != nil {
24
+		return err
25
+	}
26
+
27
+	repo, err := git.Sync(
28
+		*repoURL,
29
+		*clonePath,
30
+		*privateKeyPath,
31
+	)
32
+	if err != nil {
33
+		return err
34
+	}
35
+
36
+	w, err := repo.Worktree()
37
+	if err != nil {
38
+		return err
39
+	}
40
+
41
+	client := grafana.NewClient(*grafanaURL, *grafanaAPIKey)
42
+	uris, err := client.GetDashboardsURIs()
43
+	if err != nil {
44
+		return err
45
+	}
46
+
47
+	for _, uri := range uris {
48
+		dashboard, err := client.GetDashboard(uri)
49
+		if err != nil {
50
+			return err
51
+		}
52
+
53
+		version, ok := dbVersions[dashboard.Slug]
54
+		if !ok || dashboard.Version > version {
55
+			slugExt := dashboard.Slug + ".json"
56
+			if err = rewriteFile(
57
+				*clonePath+"/"+slugExt,
58
+				dashboard.RawJSON,
59
+			); err != nil {
60
+				return err
61
+			}
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
+		}
72
+	}
73
+
74
+	status, err := w.Status()
75
+	if err != nil {
76
+		return err
77
+	}
78
+
79
+	if !status.IsClean() {
80
+		if err = writeVersions(dbVersions, dv); err != nil {
81
+			return err
82
+		}
83
+
84
+		if _, err = w.Add("versions.json"); err != nil {
85
+			return err
86
+		}
87
+
88
+		if _, err = git.Commit(getCommitMessage(dv), w); err != nil {
89
+			return err
90
+		}
91
+
92
+	}
93
+
94
+	if err = git.Push(repo, *privateKeyPath); err != nil {
95
+		return err
96
+	}
97
+
98
+	return nil
99
+}
100
+
101
+func rewriteFile(filename string, content []byte) error {
102
+	if err := os.Remove(filename); err != nil {
103
+		pe, ok := err.(*os.PathError)
104
+		if !ok || pe.Err.Error() != "no such file or directory" {
105
+			return err
106
+		}
107
+	}
108
+
109
+	indentedContent, err := indent(content)
110
+	if err != nil {
111
+		return err
112
+	}
113
+
114
+	return ioutil.WriteFile(filename, indentedContent, 0644)
115
+}
116
+
117
+func indent(srcJSON []byte) (indentedJSON []byte, err error) {
118
+	buf := bytes.NewBuffer(nil)
119
+	if err = json.Indent(buf, srcJSON, "", "\t"); err != nil {
120
+		return
121
+	}
122
+
123
+	indentedJSON, err = ioutil.ReadAll(buf)
124
+	return
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
+}