Skip to content

Commit

Permalink
refactor init command for robust policy download checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Devang Gaur committed Feb 3, 2021
1 parent 40a9ac6 commit 24bdc7a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
8 changes: 5 additions & 3 deletions pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ var initCmd = &cobra.Command{
Initializes Terrascan and clones policies from the Terrascan GitHub repository.
`,
RunE: initialInit,
RunE: func(cmd *cobra.Command, args []string) error {
return initial(cmd, args, false)
},
SilenceUsage: true,
SilenceErrors: true,
}

func initialInit(cmd *cobra.Command, args []string) error {
func initial(cmd *cobra.Command, args []string, isScanCmd bool) error {
// initialize terrascan
if err := initialize.Run(false); err != nil {
if err := initialize.Run(isScanCmd); err != nil {
zap.S().Error("failed to initialize terrascan")
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestMain(m *testing.M) {
func setup() {
// to download the policies for Run test
// downloads the policies at $HOME/.terrascan
initialScan(nil, nil)
initial(nil, nil, false)
}

func shutdown() {
Expand Down
14 changes: 3 additions & 11 deletions pkg/cli/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strings"

iacProvider "github.com/accurics/terrascan/pkg/iac-providers"
"github.com/accurics/terrascan/pkg/initialize"
"github.com/accurics/terrascan/pkg/policy"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand All @@ -36,21 +35,14 @@ var scanCmd = &cobra.Command{
Detect compliance and security violations across Infrastructure as Code to mitigate risk before provisioning cloud native infrastructure.
`,
PreRunE: initialScan,
PreRunE: func(cmd *cobra.Command, args []string) error {
return initial(cmd, args, true)
},
RunE: scan,
SilenceUsage: true,
SilenceErrors: true,
}

func initialScan(cmd *cobra.Command, args []string) error {
// initialize terrascan
if err := initialize.Run(true); err != nil {
zap.S().Error("failed to initialize terrascan")
return err
}
return nil
}

func scan(cmd *cobra.Command, args []string) error {
zap.S().Debug("running terrascan in cli mode")
scanOptions.configFile = ConfigFile
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var serverCmd = &cobra.Command{
Run Terrascan as an API server that inspects incoming IaC (Infrastructure-as-Code) files and returns the scan results.
`,
PreRun: func(cmd *cobra.Command, args []string) {
initialScan(cmd, args)
initial(cmd, args, false)
},
Run: server,
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/initialize/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package initialize
import (
"fmt"
"os"
"path/filepath"

"github.com/accurics/terrascan/pkg/config"
"go.uber.org/zap"
Expand All @@ -44,9 +45,6 @@ func Run(isScanCmd bool) error {
if isScanCmd {
return nil
}
if err := os.RemoveAll(basePath); err != nil {
return err
}
}

// download policies
Expand All @@ -61,8 +59,10 @@ func Run(isScanCmd bool) error {
// DownloadPolicies clones the policies to a local folder
func DownloadPolicies() error {

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

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

return nil
os.RemoveAll(basePath)

return os.Rename(tempPath, basePath)
}

0 comments on commit 24bdc7a

Please sign in to comment.