|  | @@ -25,20 +25,33 @@ type diffVersion struct {
 | 
	
		
			
			| 25 | 25 |  // which name starts with "test", then commits each of them to Git except for
 | 
	
		
			
			| 26 | 26 |  // those that have a newer or equal version number already versionned in the
 | 
	
		
			
			| 27 | 27 |  // repo.
 | 
	
		
			
			| 28 |  | -func PullGrafanaAndCommit(client *grafana.Client, cfg *config.Config) error {
 | 
	
		
			
			| 29 |  | -	// Clone or pull the repo
 | 
	
		
			
			| 30 |  | -	repo, _, err := git.NewRepository(cfg.Git)
 | 
	
		
			
			| 31 |  | -	if err != nil {
 | 
	
		
			
			| 32 |  | -		return err
 | 
	
		
			
			| 33 |  | -	}
 | 
	
		
			
			|  | 28 | +func PullGrafanaAndCommit(client *grafana.Client, cfg *config.Config) (err error) {
 | 
	
		
			
			|  | 29 | +	var repo *git.Repository
 | 
	
		
			
			|  | 30 | +	var w *gogit.Worktree
 | 
	
		
			
			|  | 31 | +	var syncPath string
 | 
	
		
			
			|  | 32 | +
 | 
	
		
			
			|  | 33 | +	// Only do Git stuff if there's a configuration for that. On "simple sync"
 | 
	
		
			
			|  | 34 | +	// mode, we don't need do do any versioning.
 | 
	
		
			
			|  | 35 | +	// We need to set syncPath accordingly, though, because we use it later.
 | 
	
		
			
			|  | 36 | +	if cfg.Git != nil {
 | 
	
		
			
			|  | 37 | +		syncPath = cfg.Git.ClonePath
 | 
	
		
			
			|  | 38 | +
 | 
	
		
			
			|  | 39 | +		// Clone or pull the repo
 | 
	
		
			
			|  | 40 | +		repo, _, err = git.NewRepository(cfg.Git)
 | 
	
		
			
			|  | 41 | +		if err != nil {
 | 
	
		
			
			|  | 42 | +			return err
 | 
	
		
			
			|  | 43 | +		}
 | 
	
		
			
			| 34 | 44 |  
 | 
	
		
			
			| 35 |  | -	if err = repo.Sync(false); err != nil {
 | 
	
		
			
			| 36 |  | -		return err
 | 
	
		
			
			| 37 |  | -	}
 | 
	
		
			
			|  | 45 | +		if err = repo.Sync(false); err != nil {
 | 
	
		
			
			|  | 46 | +			return err
 | 
	
		
			
			|  | 47 | +		}
 | 
	
		
			
			| 38 | 48 |  
 | 
	
		
			
			| 39 |  | -	w, err := repo.Repo.Worktree()
 | 
	
		
			
			| 40 |  | -	if err != nil {
 | 
	
		
			
			| 41 |  | -		return err
 | 
	
		
			
			|  | 49 | +		w, err = repo.Repo.Worktree()
 | 
	
		
			
			|  | 50 | +		if err != nil {
 | 
	
		
			
			|  | 51 | +			return err
 | 
	
		
			
			|  | 52 | +		}
 | 
	
		
			
			|  | 53 | +	} else {
 | 
	
		
			
			|  | 54 | +		syncPath = cfg.SimpleSync.SyncPath
 | 
	
		
			
			| 42 | 55 |  	}
 | 
	
		
			
			| 43 | 56 |  
 | 
	
		
			
			| 44 | 57 |  	// Get URIs for all known dashboards
 | 
	
	
		
			
			|  | @@ -52,7 +65,7 @@ func PullGrafanaAndCommit(client *grafana.Client, cfg *config.Config) error {
 | 
	
		
			
			| 52 | 65 |  
 | 
	
		
			
			| 53 | 66 |  	// Load versions
 | 
	
		
			
			| 54 | 67 |  	logrus.Info("Getting local dashboard versions")
 | 
	
		
			
			| 55 |  | -	dbVersions, err := getDashboardsVersions(cfg.Git.ClonePath)
 | 
	
		
			
			|  | 68 | +	dbVersions, err := getDashboardsVersions(syncPath)
 | 
	
		
			
			| 56 | 69 |  	if err != nil {
 | 
	
		
			
			| 57 | 70 |  		return err
 | 
	
		
			
			| 58 | 71 |  	}
 | 
	
	
		
			
			|  | @@ -96,7 +109,7 @@ func PullGrafanaAndCommit(client *grafana.Client, cfg *config.Config) error {
 | 
	
		
			
			| 96 | 109 |  			}).Info("Grafana has a newer version, updating")
 | 
	
		
			
			| 97 | 110 |  
 | 
	
		
			
			| 98 | 111 |  			if err = addDashboardChangesToRepo(
 | 
	
		
			
			| 99 |  | -				dashboard, cfg.Git.ClonePath, w,
 | 
	
		
			
			|  | 112 | +				dashboard, syncPath, w,
 | 
	
		
			
			| 100 | 113 |  			); err != nil {
 | 
	
		
			
			| 101 | 114 |  				return err
 | 
	
		
			
			| 102 | 115 |  			}
 | 
	
	
		
			
			|  | @@ -112,24 +125,36 @@ func PullGrafanaAndCommit(client *grafana.Client, cfg *config.Config) error {
 | 
	
		
			
			| 112 | 125 |  		}
 | 
	
		
			
			| 113 | 126 |  	}
 | 
	
		
			
			| 114 | 127 |  
 | 
	
		
			
			| 115 |  | -	status, err := w.Status()
 | 
	
		
			
			| 116 |  | -	if err != nil {
 | 
	
		
			
			| 117 |  | -		return err
 | 
	
		
			
			| 118 |  | -	}
 | 
	
		
			
			|  | 128 | +	// Only do Git stuff if there's a configuration for that. On "simple sync"
 | 
	
		
			
			|  | 129 | +	// mode, we don't need do do any versioning.
 | 
	
		
			
			|  | 130 | +	if cfg.Git != nil {
 | 
	
		
			
			|  | 131 | +		var status gogit.Status
 | 
	
		
			
			|  | 132 | +		status, err = w.Status()
 | 
	
		
			
			|  | 133 | +		if err != nil {
 | 
	
		
			
			|  | 134 | +			return err
 | 
	
		
			
			|  | 135 | +		}
 | 
	
		
			
			| 119 | 136 |  
 | 
	
		
			
			| 120 |  | -	// Check if there's uncommited changes, and if that's the case, commit them.
 | 
	
		
			
			| 121 |  | -	if !status.IsClean() {
 | 
	
		
			
			| 122 |  | -		logrus.Info("Comitting changes")
 | 
	
		
			
			|  | 137 | +		// Check if there's uncommited changes, and if that's the case, commit
 | 
	
		
			
			|  | 138 | +		// them.
 | 
	
		
			
			|  | 139 | +		if !status.IsClean() {
 | 
	
		
			
			|  | 140 | +			logrus.Info("Comitting changes")
 | 
	
		
			
			| 123 | 141 |  
 | 
	
		
			
			| 124 |  | -		if err = commitNewVersions(dbVersions, dv, w, cfg); err != nil {
 | 
	
		
			
			| 125 |  | -			return err
 | 
	
		
			
			|  | 142 | +			if err = commitNewVersions(dbVersions, dv, w, cfg); err != nil {
 | 
	
		
			
			|  | 143 | +				return err
 | 
	
		
			
			|  | 144 | +			}
 | 
	
		
			
			| 126 | 145 |  		}
 | 
	
		
			
			| 127 |  | -	}
 | 
	
		
			
			| 128 | 146 |  
 | 
	
		
			
			| 129 |  | -	// Push the changes (we don't do it in the if clause above in case there are
 | 
	
		
			
			| 130 |  | -	// pending commits in the local repo that haven't been pushed yet).
 | 
	
		
			
			| 131 |  | -	if err = repo.Push(); err != nil {
 | 
	
		
			
			| 132 |  | -		return err
 | 
	
		
			
			|  | 147 | +		// Push the changes (we don't do it in the if clause above in case there
 | 
	
		
			
			|  | 148 | +		// are pending commits in the local repo that haven't been pushed yet).
 | 
	
		
			
			|  | 149 | +		if err = repo.Push(); err != nil {
 | 
	
		
			
			|  | 150 | +			return err
 | 
	
		
			
			|  | 151 | +		}
 | 
	
		
			
			|  | 152 | +	} else {
 | 
	
		
			
			|  | 153 | +		// If we're on simple sync mode, write versions and don't do anything
 | 
	
		
			
			|  | 154 | +		// else.
 | 
	
		
			
			|  | 155 | +		if err = writeVersions(dbVersions, dv, syncPath); err != nil {
 | 
	
		
			
			|  | 156 | +			return err
 | 
	
		
			
			|  | 157 | +		}
 | 
	
		
			
			| 133 | 158 |  	}
 | 
	
		
			
			| 134 | 159 |  
 | 
	
		
			
			| 135 | 160 |  	return nil
 | 
	
	
		
			
			|  | @@ -146,8 +171,12 @@ func addDashboardChangesToRepo(
 | 
	
		
			
			| 146 | 171 |  		return err
 | 
	
		
			
			| 147 | 172 |  	}
 | 
	
		
			
			| 148 | 173 |  
 | 
	
		
			
			| 149 |  | -	if _, err := worktree.Add(slugExt); err != nil {
 | 
	
		
			
			| 150 |  | -		return err
 | 
	
		
			
			|  | 174 | +	// If worktree is nil, it means that it hasn't been initialised, which means
 | 
	
		
			
			|  | 175 | +	// the sync mode is "simple sync" and not Git.
 | 
	
		
			
			|  | 176 | +	if worktree != nil {
 | 
	
		
			
			|  | 177 | +		if _, err := worktree.Add(slugExt); err != nil {
 | 
	
		
			
			|  | 178 | +			return err
 | 
	
		
			
			|  | 179 | +		}
 | 
	
		
			
			| 151 | 180 |  	}
 | 
	
		
			
			| 152 | 181 |  
 | 
	
		
			
			| 153 | 182 |  	return nil
 |