From b24b89b3b9aa8e5017d62af2f88275db7308dec6 Mon Sep 17 00:00:00 2001 From: Devang Gaur Date: Wed, 17 Feb 2021 03:38:35 +0530 Subject: [PATCH] fix the 'repo already exist' bug and improve error logging for terrascan init --- pkg/cli/init.go | 2 +- pkg/initialize/run.go | 37 ++++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/pkg/cli/init.go b/pkg/cli/init.go index 6cf47da73..9fde902e5 100644 --- a/pkg/cli/init.go +++ b/pkg/cli/init.go @@ -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 diff --git a/pkg/initialize/run.go b/pkg/initialize/run.go index c49482aca..cec6f36f9 100644 --- a/pkg/initialize/run.go +++ b/pkg/initialize/run.go @@ -18,8 +18,8 @@ package initialize import ( "fmt" + "io/ioutil" "os" - "path/filepath" "github.com/accurics/terrascan/pkg/config" "go.uber.org/zap" @@ -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 @@ -58,23 +57,29 @@ 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 @@ -82,8 +87,7 @@ func DownloadPolicies() error { 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 @@ -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 }