|  | @@ -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 |  }
 |