Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the 'repo already exist' bug and improve error logging for terrascan init #552

Merged
merged 1 commit into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}