Procházet zdrojové kódy

Use config in pusher

Brendan Abolivier před 6 roky
rodič
revize
e5b6f979c2
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
2 změnil soubory, kde provedl 20 přidání a 48 odebrání
  1. 13
    39
      src/pusher/main.go
  2. 7
    9
      src/pusher/webhook.go

+ 13
- 39
src/pusher/main.go Zobrazit soubor

@@ -2,60 +2,34 @@ package main
2 2
 
3 3
 import (
4 4
 	"flag"
5
-	"os"
6 5
 
6
+	"config"
7 7
 	"grafana"
8 8
 )
9 9
 
10
-// The Grafana API client needs to be global to the package since we need it in
11
-// the webhook handlers
10
+// The Grafana API client and the config need to be global to the package since
11
+// we need them in the webhook handlers.
12 12
 // TODO: Find a better way to pass it to the handlers
13
-var grafanaClient *grafana.Client
13
+var (
14
+	grafanaClient *grafana.Client
15
+	cfg           *config.Config
16
+)
14 17
 
15 18
 var (
16
-	grafanaURL       = flag.String("grafana-url", "", "Base URL of the Grafana instance")
17
-	grafanaAPIKey    = flag.String("api-key", "", "API key to use in authenticated requests")
18
-	clonePath        = flag.String("clone-path", "/tmp/grafana-dashboards", "Path to directory where the repo will be cloned")
19
-	repoURL          = flag.String("git-repo", "", "SSH URL for the Git repository, without the user part")
20
-	privateKeyPath   = flag.String("private-key", "", "Path to the private key used to talk with the Git remote")
21
-	webhookInterface = flag.String("webhook-interface", "127.0.0.1", "Interface on which the GitLab webhook will be exposed")
22
-	webhookPort      = flag.Int("webhook-port", 8080, "Port on which the GitLab webhook will be exposed")
23
-	webhookPath      = flag.String("webhook-path", "/gitlab-webhook", "Path at which GitLab should send payloads to the webhook")
24
-	webhookSecret    = flag.String("webhook-secret", "", "Secret GitLab will use to send payloads to the webhook")
19
+	configFile = flag.String("config", "config.yaml", "Path to the configuration file")
25 20
 )
26 21
 
27 22
 func main() {
28 23
 	flag.Parse()
29 24
 
30
-	if *grafanaURL == "" {
31
-		println("Error: No Grafana URL provided")
32
-		flag.Usage()
33
-		os.Exit(1)
34
-	}
35
-	if *grafanaAPIKey == "" {
36
-		println("Error: No Grafana API key provided")
37
-		flag.Usage()
38
-		os.Exit(1)
39
-	}
40
-	if *repoURL == "" {
41
-		println("Error: No Git repository provided")
42
-		flag.Usage()
43
-		os.Exit(1)
44
-	}
45
-	if *privateKeyPath == "" {
46
-		println("Error: No private key provided")
47
-		flag.Usage()
48
-		os.Exit(1)
49
-	}
50
-	if *webhookSecret == "" {
51
-		println("Error: No webhook secret provided")
52
-		flag.Usage()
53
-		os.Exit(1)
25
+	cfg, err := config.Load(*configFile)
26
+	if err != nil {
27
+		panic(err)
54 28
 	}
55 29
 
56
-	grafanaClient = grafana.NewClient(*grafanaURL, *grafanaAPIKey)
30
+	grafanaClient = grafana.NewClient(cfg.Grafana.BaseURL, cfg.Grafana.APIKey)
57 31
 
58
-	if err := SetupWebhook(); err != nil {
32
+	if err := SetupWebhook(cfg); err != nil {
59 33
 		panic(err)
60 34
 	}
61 35
 }

+ 7
- 9
src/pusher/webhook.go Zobrazit soubor

@@ -2,25 +2,25 @@ package main
2 2
 
3 3
 import (
4 4
 	"io/ioutil"
5
-	"strconv"
6 5
 	"strings"
7 6
 
7
+	"config"
8 8
 	puller "puller"
9 9
 
10 10
 	"gopkg.in/go-playground/webhooks.v3"
11 11
 	"gopkg.in/go-playground/webhooks.v3/gitlab"
12 12
 )
13 13
 
14
-func SetupWebhook() error {
14
+func SetupWebhook(cfg *config.Config) error {
15 15
 	hook := gitlab.New(&gitlab.Config{
16
-		Secret: "mysecret",
16
+		Secret: cfg.Webhook.Secret,
17 17
 	})
18 18
 	hook.RegisterEvents(HandlePush, gitlab.PushEvents)
19 19
 
20 20
 	return webhooks.Run(
21 21
 		hook,
22
-		*webhookInterface+":"+strconv.Itoa(*webhookPort),
23
-		*webhookPath,
22
+		cfg.Webhook.Interface+":"+cfg.Webhook.Port,
23
+		cfg.Webhook.Path,
24 24
 	)
25 25
 }
26 26
 
@@ -52,15 +52,13 @@ func HandlePush(payload interface{}, header webhooks.Header) {
52 52
 	// Grafana will auto-update the version number after we pushed the new
53 53
 	// dashboards, so we use the puller mechanic to pull the updated numbers and
54 54
 	// commit them in the git repo.
55
-	if err = puller.PullGrafanaAndCommit(
56
-		grafanaClient, *repoURL, *clonePath, *privateKeyPath,
57
-	); err != nil {
55
+	if err = puller.PullGrafanaAndCommit(grafanaClient, cfg); err != nil {
58 56
 		panic(err)
59 57
 	}
60 58
 }
61 59
 
62 60
 func pushFile(filename string) error {
63
-	filePath := *clonePath + "/" + filename
61
+	filePath := cfg.Git.ClonePath + "/" + filename
64 62
 	fileContent, err := ioutil.ReadFile(filePath)
65 63
 	if err != nil {
66 64
 		return err