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 17, 2021
1 parent efeed62 commit b24b89b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Initializes Terrascan and clones policies from the Terrascan GitHub repository.
func initial(cmd *cobra.Command, args []string, isScanCmd bool) error {
// initialize terrascan
if err := initialize.Run(isScanCmd); err != nil {
zap.S().Error("failed to initialize terrascan")
zap.S().Errorf("failed to initialize terrascan. error : %v", err)
return err
}
return nil
Expand Down
37 changes: 24 additions & 13 deletions pkg/initialize/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package initialize

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/accurics/terrascan/pkg/config"
"go.uber.org/zap"
Expand All @@ -37,7 +37,6 @@ var (

// Run initializes terrascan if not done already
func Run(isScanCmd bool) error {

zap.S().Debug("initializing terrascan")

// check if policy paths exist
Expand All @@ -58,32 +57,37 @@ func Run(isScanCmd bool) error {

// DownloadPolicies clones the policies to a local folder
func DownloadPolicies() error {
zap.S().Debug("downloading policies")

tempPath, err := ioutil.TempDir("", "terrascan-")
if err != nil {
return fmt.Errorf("failed to create temporary directory. error: '%v'", err)
}

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

zap.S().Debugf("cloning terrascan repo at %s", tempPath)

// clone the repo
r, err := git.PlainClone(tempPath, false, &git.CloneOptions{
URL: repoURL,
})
if err != nil {
zap.S().Errorf("failed to download policies. error: '%v'", err)
return err
return fmt.Errorf("failed to download policies. error: '%v'", err)
}

// create working tree
w, err := r.Worktree()
if err != nil {
zap.S().Errorf("failed to create working tree. error: '%v'", err)
return err
return fmt.Errorf("failed to create working tree. error: '%v'", err)
}

// fetch references
err = r.Fetch(&git.FetchOptions{
RefSpecs: []gitConfig.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
})
if err != nil {
zap.S().Errorf("failed to fetch references from repo. error: '%v'", err)
return err
return fmt.Errorf("failed to fetch references from git repo. error: '%v'", err)
}

// checkout policies branch
Expand All @@ -92,11 +96,18 @@ func DownloadPolicies() error {
Force: true,
})
if err != nil {
zap.S().Errorf("failed to checkout branch '%v'. error: '%v'", branch, err)
return err
return fmt.Errorf("failed to checkout git branch '%v'. error: '%v'", branch, err)
}

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

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

return os.Rename(tempPath, basePath)
return nil
}

0 comments on commit b24b89b

Please sign in to comment.