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