瀏覽代碼

Use config in puller

Brendan Abolivier 6 年之前
父節點
當前提交
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,47 +2,25 @@ package main
2 2
 
3 3
 import (
4 4
 	"flag"
5
-	"os"
6 5
 
6
+	"config"
7 7
 	"grafana"
8 8
 )
9 9
 
10 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 14
 func main() {
19 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 24
 		panic(err)
47 25
 	}
48 26
 }

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

@@ -6,6 +6,7 @@ import (
6 6
 	"io/ioutil"
7 7
 	"os"
8 8
 
9
+	"config"
9 10
 	"git"
10 11
 	"grafana"
11 12
 
@@ -21,12 +22,9 @@ type diffVersion struct {
21 22
 // PullGrafanaAndCommit pulls all the dashboards from Grafana then commits each
22 23
 // of them to Git except for those that have a newer or equal version number
23 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 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 28
 	if err != nil {
31 29
 		return err
32 30
 	}
@@ -45,7 +43,7 @@ func PullGrafanaAndCommit(
45 43
 	dv := make(map[string]diffVersion)
46 44
 
47 45
 	// Load versions
48
-	dbVersions, err := getDashboardsVersions()
46
+	dbVersions, err := getDashboardsVersions(cfg.Git.ClonePath)
49 47
 	if err != nil {
50 48
 		return err
51 49
 	}
@@ -65,7 +63,9 @@ func PullGrafanaAndCommit(
65 63
 		// changes in the repo and add the modified file to the git index.
66 64
 		version, ok := dbVersions[dashboard.Slug]
67 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 69
 				return err
70 70
 			}
71 71
 
@@ -87,14 +87,16 @@ func PullGrafanaAndCommit(
87 87
 
88 88
 	// Check if there's uncommited changes, and if that's the case, commit them.
89 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 93
 			return err
92 94
 		}
93 95
 	}
94 96
 
95 97
 	// Push the changes (we don't do it in the if clause above in case there are
96 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 100
 		return err
99 101
 	}
100 102
 
@@ -105,13 +107,10 @@ func PullGrafanaAndCommit(
105 107
 // file to the git index so it can be comitted afterwards.
106 108
 // Returns an error if there was an issue with either of the steps.
107 109
 func addDashboardChangesToRepo(
108
-	dashboard *grafana.Dashboard, worktree *gogit.Worktree,
110
+	dashboard *grafana.Dashboard, clonePath string, worktree *gogit.Worktree,
109 111
 ) error {
110 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 114
 		return err
116 115
 	}
117 116
 

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

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