Skip to content

Commit

Permalink
fix the 'repo already exist' bug and improve error logging for terras…
Browse files Browse the repository at this point in the history
…can init
  • Loading branch information
Devang Gaur committed Feb 16, 2021
1 parent efeed62 commit 317328b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkg/initialize/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ func DownloadPolicies() error {

tempPath := filepath.Join(os.TempDir(), "terrascan")

// ensure a clear tempPath
if err := os.RemoveAll(tempPath); err != nil {
zap.S().Errorf("failed to clean the existing temporary directory at '%s'. error: '%v'", tempPath, err)
return err
}

// clone the repo
r, err := git.PlainClone(tempPath, false, &git.CloneOptions{
URL: repoURL,
Expand Down Expand Up @@ -96,7 +102,17 @@ func DownloadPolicies() error {
return err
}

os.RemoveAll(basePath)
// cleaning the existing cached policies at basePath
if err = os.RemoveAll(basePath); err != nil {
zap.S().Errorf("failed to clean the existing policy repository at '%s'. error: '%v'", basePath, err)
return err
}

return os.Rename(tempPath, basePath)
// move the freshly cloned repo from tempPath to basePath
if err = os.Rename(tempPath, basePath); err != nil {
zap.S().Errorf("failed to move the freshly cloned repository from '%s' to '%s'. error: '%v'", tempPath, basePath, err)
return err
}

return nil
}

0 comments on commit 317328b

Please sign in to comment.