Repository: tenable/terrascan (language: python)

Issue:
terrascan init should download new policies
* terrascan version: v1.3.1
* Operating System: all

### Description
`terrascan init` should download the latest policies from github repository. Currently, `terrascan init` downloads the policies only once, and subsequently invocations of `terrascan init` do not refresh policies.


Discussion / hints:
@kanchwala-yusuf Would like to pick this up!
Thanks @rahulchheda, welcome your contributions! Let me know if you need any help on this one?
Hey @rahulchheda , Did you get a chance to look at this issue?
Hey Yusuf, I have started with this task, and will create a work in progress PR soon.

Fix patch:
diff --git a/pkg/cli/init.go b/pkg/cli/init.go
index db97e1db9..b6dcf6534 100644
--- a/pkg/cli/init.go
+++ b/pkg/cli/init.go
@@ -29,14 +29,14 @@ var initCmd = &cobra.Command{
 
 Initializes Terrascan and clones policies from the Terrascan GitHub repository.
 `,
-	RunE:          initial,
+	RunE:          initialInit,
 	SilenceUsage:  true,
 	SilenceErrors: true,
 }
 
-func initial(cmd *cobra.Command, args []string) error {
+func initialInit(cmd *cobra.Command, args []string) error {
 	// initialize terrascan
-	if err := initialize.Run(); err != nil {
+	if err := initialize.Run(false); err != nil {
 		zap.S().Error("failed to initialize terrascan")
 		return err
 	}
diff --git a/pkg/cli/scan.go b/pkg/cli/scan.go
index 14461cc18..a4eac4eaf 100644
--- a/pkg/cli/scan.go
+++ b/pkg/cli/scan.go
@@ -21,6 +21,7 @@ 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"
@@ -35,12 +36,21 @@ var scanCmd = &cobra.Command{
 
 Detect compliance and security violations across Infrastructure as Code to mitigate risk before provisioning cloud native infrastructure.
 `,
-	PreRunE:       initial,
+	PreRunE:       initialScan,
 	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
diff --git a/pkg/cli/server.go b/pkg/cli/server.go
index 9f2868d6f..6031b4225 100644
--- a/pkg/cli/server.go
+++ b/pkg/cli/server.go
@@ -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) {
-		initial(cmd, args)
+		initialScan(cmd, args)
 	},
 	Run: server,
 }
diff --git a/pkg/initialize/run.go b/pkg/initialize/run.go
index 45943ed76..998a2b14a 100644
--- a/pkg/initialize/run.go
+++ b/pkg/initialize/run.go
@@ -35,17 +35,21 @@ var (
 )
 
 // Run initializes terrascan if not done already
-func Run() error {
+func Run(isScanCmd bool) error {
 
 	zap.S().Debug("initializing terrascan")
 
 	// check if policy paths exist
 	if path, err := os.Stat(basePolicyPath); err == nil && path.IsDir() {
-		return nil
+		if isScanCmd {
+			return nil
+		}
+		if err := os.RemoveAll(basePath); err != nil {
+			return err
+		}
 	}
 
 	// download policies
-	os.RemoveAll(basePath)
 	if err := DownloadPolicies(); err != nil {
 		return err
 	}


Test patch:
diff --git a/pkg/cli/run_test.go b/pkg/cli/run_test.go
index ca88eda42..8f231f703 100644
--- a/pkg/cli/run_test.go
+++ b/pkg/cli/run_test.go
@@ -38,7 +38,7 @@ func TestMain(m *testing.M) {
 func setup() {
 	// to download the policies for Run test
 	// downloads the policies at $HOME/.terrascan
-	initial(nil, nil)
+	initialScan(nil, nil)
 }
 
 func shutdown() {