浏览代码

Use config in puller

父节点
当前提交
6f4f0257c5
签署人:: Brendan Abolivier <contact@brendanabolivier.com> GPG 密钥 ID: 8EF1500759F70623
共有 3 个文件被更改,包括 28 次插入48 次删除
  1. 7
    29
      src/puller/main.go
  2. 13
    14
      src/puller/puller.go
  3. 8
    5
      src/puller/versions.go

+ 7
- 29
src/puller/main.go 查看文件

2
 
2
 
3
 import (
3
 import (
4
 	"flag"
4
 	"flag"
5
-	"os"
6
 
5
 
6
+	"config"
7
 	"grafana"
7
 	"grafana"
8
 )
8
 )
9
 
9
 
10
 var (
10
 var (
11
-	grafanaURL     = flag.String("grafana-url", "", "Base URL of the Grafana instance")
12
-	grafanaAPIKey  = flag.String("api-key", "", "API key to use in authenticated requests")
13
-	clonePath      = flag.String("clone-path", "/tmp/grafana-dashboards", "Path to directory where the repo will be cloned")
14
-	repoURL        = flag.String("git-repo", "", "SSH URL for the Git repository, without the user part")
15
-	privateKeyPath = flag.String("private-key", "", "Path to the private key used to talk with the Git remote")
11
+	configFile = flag.String("config", "config.yaml", "Path to the configuration file")
16
 )
12
 )
17
 
13
 
18
 func main() {
14
 func main() {
19
 	flag.Parse()
15
 	flag.Parse()
20
 
16
 
21
-	if *grafanaURL == "" {
22
-		println("Error: No Grafana URL provided")
23
-		flag.Usage()
24
-		os.Exit(1)
25
-	}
26
-	if *grafanaAPIKey == "" {
27
-		println("Error: No Grafana API key provided")
28
-		flag.Usage()
29
-		os.Exit(1)
30
-	}
31
-	if *repoURL == "" {
32
-		println("Error: No Git repository provided")
33
-		flag.Usage()
34
-		os.Exit(1)
35
-	}
36
-	if *privateKeyPath == "" {
37
-		println("Error: No private key provided")
38
-		flag.Usage()
39
-		os.Exit(1)
17
+	cfg, err := config.Load(*configFile)
18
+	if err != nil {
19
+		panic(err)
40
 	}
20
 	}
41
 
21
 
42
-	client := grafana.NewClient(*grafanaURL, *grafanaAPIKey)
43
-	if err := PullGrafanaAndCommit(
44
-		client, *repoURL, *clonePath, *privateKeyPath,
45
-	); err != nil {
22
+	client := grafana.NewClient(cfg.Grafana.BaseURL, cfg.Grafana.APIKey)
23
+	if err := PullGrafanaAndCommit(client, cfg); err != nil {
46
 		panic(err)
24
 		panic(err)
47
 	}
25
 	}
48
 }
26
 }

+ 13
- 14
src/puller/puller.go 查看文件

6
 	"io/ioutil"
6
 	"io/ioutil"
7
 	"os"
7
 	"os"
8
 
8
 
9
+	"config"
9
 	"git"
10
 	"git"
10
 	"grafana"
11
 	"grafana"
11
 
12
 
21
 // PullGrafanaAndCommit pulls all the dashboards from Grafana then commits each
22
 // PullGrafanaAndCommit pulls all the dashboards from Grafana then commits each
22
 // of them to Git except for those that have a newer or equal version number
23
 // of them to Git except for those that have a newer or equal version number
23
 // already versionned in the repo
24
 // already versionned in the repo
24
-func PullGrafanaAndCommit(
25
-	client *grafana.Client,
26
-	repoURL string, clonePath string, privateKeyPath string,
27
-) error {
25
+func PullGrafanaAndCommit(client *grafana.Client, cfg *config.Config) error {
28
 	// Clone or pull the repo
26
 	// Clone or pull the repo
29
-	repo, err := git.Sync(repoURL, clonePath, privateKeyPath)
27
+	repo, err := git.Sync(cfg.Git.URL, cfg.Git.ClonePath, cfg.Git.PrivateKeyPath)
30
 	if err != nil {
28
 	if err != nil {
31
 		return err
29
 		return err
32
 	}
30
 	}
45
 	dv := make(map[string]diffVersion)
43
 	dv := make(map[string]diffVersion)
46
 
44
 
47
 	// Load versions
45
 	// Load versions
48
-	dbVersions, err := getDashboardsVersions()
46
+	dbVersions, err := getDashboardsVersions(cfg.Git.ClonePath)
49
 	if err != nil {
47
 	if err != nil {
50
 		return err
48
 		return err
51
 	}
49
 	}
65
 		// changes in the repo and add the modified file to the git index.
63
 		// changes in the repo and add the modified file to the git index.
66
 		version, ok := dbVersions[dashboard.Slug]
64
 		version, ok := dbVersions[dashboard.Slug]
67
 		if !ok || dashboard.Version > version {
65
 		if !ok || dashboard.Version > version {
68
-			if err = addDashboardChangesToRepo(dashboard, w); err != nil {
66
+			if err = addDashboardChangesToRepo(
67
+				dashboard, cfg.Git.ClonePath, w,
68
+			); err != nil {
69
 				return err
69
 				return err
70
 			}
70
 			}
71
 
71
 
87
 
87
 
88
 	// Check if there's uncommited changes, and if that's the case, commit them.
88
 	// Check if there's uncommited changes, and if that's the case, commit them.
89
 	if !status.IsClean() {
89
 	if !status.IsClean() {
90
-		if err = commitNewVersions(dbVersions, dv, w); err != nil {
90
+		if err = commitNewVersions(
91
+			dbVersions, dv, w, cfg.Git.ClonePath,
92
+		); err != nil {
91
 			return err
93
 			return err
92
 		}
94
 		}
93
 	}
95
 	}
94
 
96
 
95
 	// Push the changes (we don't do it in the if clause above in case there are
97
 	// 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).
98
 	// pending commits in the local repo that haven't been pushed yet).
97
-	if err = git.Push(repo, privateKeyPath); err != nil {
99
+	if err = git.Push(repo, cfg.Git.PrivateKeyPath); err != nil {
98
 		return err
100
 		return err
99
 	}
101
 	}
100
 
102
 
105
 // file to the git index so it can be comitted afterwards.
107
 // file to the git index so it can be comitted afterwards.
106
 // Returns an error if there was an issue with either of the steps.
108
 // Returns an error if there was an issue with either of the steps.
107
 func addDashboardChangesToRepo(
109
 func addDashboardChangesToRepo(
108
-	dashboard *grafana.Dashboard, worktree *gogit.Worktree,
110
+	dashboard *grafana.Dashboard, clonePath string, worktree *gogit.Worktree,
109
 ) error {
111
 ) error {
110
 	slugExt := dashboard.Slug + ".json"
112
 	slugExt := dashboard.Slug + ".json"
111
-	if err := rewriteFile(
112
-		*clonePath+"/"+slugExt,
113
-		dashboard.RawJSON,
114
-	); err != nil {
113
+	if err := rewriteFile(clonePath+"/"+slugExt, dashboard.RawJSON); err != nil {
115
 		return err
114
 		return err
116
 	}
115
 	}
117
 
116
 

+ 8
- 5
src/puller/versions.go 查看文件

16
 // If the file doesn't exist, returns an empty map.
16
 // If the file doesn't exist, returns an empty map.
17
 // Return an error if there was an issue looking for the file (except when the
17
 // Return an error if there was an issue looking for the file (except when the
18
 // file doesn't exist), reading it or formatting its content into a map.
18
 // file doesn't exist), reading it or formatting its content into a map.
19
-func getDashboardsVersions() (versions map[string]int, err error) {
19
+func getDashboardsVersions(clonePath string) (versions map[string]int, err error) {
20
 	versions = make(map[string]int)
20
 	versions = make(map[string]int)
21
 
21
 
22
-	filename := *clonePath + "/versions.json"
22
+	filename := clonePath + "/versions.json"
23
 
23
 
24
 	_, err = os.Stat(filename)
24
 	_, err = os.Stat(filename)
25
 	if os.IsNotExist(err) {
25
 	if os.IsNotExist(err) {
43
 // "versions.json" file.
43
 // "versions.json" file.
44
 // Returns an error if there was an issue when conerting to JSON, indenting or
44
 // Returns an error if there was an issue when conerting to JSON, indenting or
45
 // writing on disk.
45
 // writing on disk.
46
-func writeVersions(versions map[string]int, dv map[string]diffVersion) (err error) {
46
+func writeVersions(
47
+	versions map[string]int, dv map[string]diffVersion, clonePath string,
48
+) (err error) {
47
 	for slug, diff := range dv {
49
 	for slug, diff := range dv {
48
 		versions[slug] = diff.newVersion
50
 		versions[slug] = diff.newVersion
49
 	}
51
 	}
58
 		return
60
 		return
59
 	}
61
 	}
60
 
62
 
61
-	filename := *clonePath + "/versions.json"
63
+	filename := clonePath + "/versions.json"
62
 	return rewriteFile(filename, indentedJSON)
64
 	return rewriteFile(filename, indentedJSON)
63
 }
65
 }
64
 
66
 
69
 // file, adding it to the index or creating the commit.
71
 // file, adding it to the index or creating the commit.
70
 func commitNewVersions(
72
 func commitNewVersions(
71
 	versions map[string]int, dv map[string]diffVersion, worktree *gogit.Worktree,
73
 	versions map[string]int, dv map[string]diffVersion, worktree *gogit.Worktree,
74
+	clonePath string,
72
 ) (err error) {
75
 ) (err error) {
73
-	if err = writeVersions(versions, dv); err != nil {
76
+	if err = writeVersions(versions, dv, clonePath); err != nil {
74
 		return err
77
 		return err
75
 	}
78
 	}
76
 
79