Skip to content

Commit

Permalink
rename ScanCommand to ScanOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
patilpankaj212 committed Dec 21, 2020
1 parent 9d40d8e commit 71645d2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 61 deletions.
30 changes: 15 additions & 15 deletions pkg/cli/run.go
Expand Up @@ -35,8 +35,8 @@ const (
humanOutputFormat = "human"
)

// ScanCommand represents scan command and its optional flags
type ScanCommand struct {
// ScanOptions represents scan command and its optional flags
type ScanOptions struct {
// Policy path directory
policyPath []string

Expand Down Expand Up @@ -79,22 +79,22 @@ type ScanCommand struct {
Verbose bool
}

// StartScan starts the terrascan scan command
func (s *ScanCommand) StartScan() error {
err := s.Init()
if err != nil {
// Scan executes the terrascan scan command
func (s *ScanOptions) Scan() error {
if err := s.Init(); err != nil {
zap.S().Error("scan init failed", zap.Error(err))
return err
}

err = s.Run()
if err != nil {
if err := s.Run(); err != nil {
zap.S().Error("scan run failed", zap.Error(err))
return err
}
return nil
}

//Init initalises and validates ScanCommand
func (s *ScanCommand) Init() error {
//Init initalises and validates ScanOptions
func (s *ScanOptions) Init() error {
s.initColor()
err := s.validate()
if err != nil {
Expand All @@ -106,7 +106,7 @@ func (s *ScanCommand) Init() error {

// validate config only for human readable output
// rest command options are validated by the executor
func (s ScanCommand) validate() error {
func (s ScanOptions) validate() error {
// human readable output doesn't support --config-only flag
// if --config-only flag is set, then exit with an error
// asking the user to use yaml or json output format
Expand All @@ -117,7 +117,7 @@ func (s ScanCommand) validate() error {
}

// initialises use colors options
func (s *ScanCommand) initColor() {
func (s *ScanOptions) initColor() {
switch strings.ToLower(s.useColors) {
case "auto":
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
Expand All @@ -143,7 +143,7 @@ func (s *ScanCommand) initColor() {
}

// Run executes terrascan in CLI mode
func (s *ScanCommand) Run() error {
func (s *ScanOptions) Run() error {

// temp dir to download the remote repo
tempDir := filepath.Join(os.TempDir(), utils.GenRandomString(6))
Expand Down Expand Up @@ -182,7 +182,7 @@ func (s *ScanCommand) Run() error {
return nil
}

func (s *ScanCommand) downloadRemoteRepository(tempDir string) error {
func (s *ScanOptions) downloadRemoteRepository(tempDir string) error {
d := downloader.NewDownloader()
path, err := d.DownloadWithType(s.remoteType, s.remoteURL, tempDir)
if path != "" {
Expand All @@ -198,7 +198,7 @@ func (s *ScanCommand) downloadRemoteRepository(tempDir string) error {
return nil
}

func (s ScanCommand) writeResults(results runtime.Output) error {
func (s ScanOptions) writeResults(results runtime.Output) error {
// add verbose flag to the scan summary
results.Violations.ViolationStore.Summary.ShowViolationDetails = s.Verbose

Expand Down
62 changes: 31 additions & 31 deletions pkg/cli/run_test.go
Expand Up @@ -54,14 +54,14 @@ func TestRun(t *testing.T) {
table := []struct {
name string
configFile string
scanCommand *ScanCommand
scanOptions *ScanOptions
stdOut string
want string
wantErr bool
}{
{
name: "normal terraform run",
scanCommand: &ScanCommand{
scanOptions: &ScanOptions{
// policy type terraform is not supported, error expected
policyType: []string{"terraform"},
iacDirPath: testDirPath,
Expand All @@ -70,15 +70,15 @@ func TestRun(t *testing.T) {
},
{
name: "normal terraform run with successful output",
scanCommand: &ScanCommand{
scanOptions: &ScanOptions{
policyType: []string{"all"},
iacDirPath: testDirPath,
outputType: "json",
},
},
{
name: "normal k8s run",
scanCommand: &ScanCommand{
scanOptions: &ScanOptions{
policyType: []string{"k8s"},
// kustomization.y(a)ml file not present under the dir path, error expected
iacDirPath: testDirPath,
Expand All @@ -87,15 +87,15 @@ func TestRun(t *testing.T) {
},
{
name: "normal k8s run with successful output",
scanCommand: &ScanCommand{
scanOptions: &ScanOptions{
policyType: []string{"k8s"},
iacDirPath: kustomizeTestDirPath,
outputType: "human",
},
},
{
name: "config-only flag terraform",
scanCommand: &ScanCommand{
scanOptions: &ScanOptions{
policyType: []string{"all"},
iacFilePath: testTerraformFilePath,
configOnly: true,
Expand All @@ -104,7 +104,7 @@ func TestRun(t *testing.T) {
},
{
name: "config-only flag k8s",
scanCommand: &ScanCommand{
scanOptions: &ScanOptions{
policyType: []string{"k8s"},
iacDirPath: kustomizeTestDirPath,
configOnly: true,
Expand All @@ -115,7 +115,7 @@ func TestRun(t *testing.T) {
// xml doesn't support config-only, error expected
// modify the test results when xml supports config-only
name: "config-only flag true with xml output format",
scanCommand: &ScanCommand{
scanOptions: &ScanOptions{
policyType: []string{"all"},
iacFilePath: testTerraformFilePath,
configOnly: true,
Expand All @@ -125,7 +125,7 @@ func TestRun(t *testing.T) {
},
{
name: "fail to download remote repository",
scanCommand: &ScanCommand{
scanOptions: &ScanOptions{
policyType: []string{"all"},
iacFilePath: testTerraformFilePath,
remoteURL: "test",
Expand All @@ -137,16 +137,16 @@ func TestRun(t *testing.T) {

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
err := tt.scanCommand.Run()
err := tt.scanOptions.Run()
if (err != nil) != tt.wantErr {
t.Errorf("ScanCommand.Run() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("ScanOptions.Run() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}

func TestScanCommandDownloadRemoteRepository(t *testing.T) {
func TestScanOptionsDownloadRemoteRepository(t *testing.T) {
testTempdir := filepath.Join(os.TempDir(), utils.GenRandomString(6))
defer os.RemoveAll(testTempdir)

Expand Down Expand Up @@ -190,23 +190,23 @@ func TestScanCommandDownloadRemoteRepository(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := ScanCommand{
s := ScanOptions{
remoteType: tt.fields.RemoteType,
remoteURL: tt.fields.RemoteURL,
}
err := s.downloadRemoteRepository(tt.tempDir)
if (err != nil) != tt.wantErr {
t.Errorf("ScanCommand.downloadRemoteRepository() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("ScanOptions.downloadRemoteRepository() error = %v, wantErr %v", err, tt.wantErr)
return
}
if s.iacDirPath != tt.want {
t.Errorf("ScanCommand.downloadRemoteRepository() = %v, want %v", s.iacDirPath, tt.want)
t.Errorf("ScanOptions.downloadRemoteRepository() = %v, want %v", s.iacDirPath, tt.want)
}
})
}
}

func TestScanCommandWriteResults(t *testing.T) {
func TestScanOptionsWriteResults(t *testing.T) {
testInput := runtime.Output{
ResourceConfig: output.AllResourceConfigs{},
Violations: policy.EngineOutput{
Expand Down Expand Up @@ -253,18 +253,18 @@ func TestScanCommandWriteResults(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := ScanCommand{
s := ScanOptions{
configOnly: tt.fields.ConfigOnly,
outputType: tt.fields.OutputType,
}
if err := s.writeResults(tt.args); (err != nil) != tt.wantErr {
t.Errorf("ScanCommand.writeResults() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("ScanOptions.writeResults() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestScanCommandValidate(t *testing.T) {
func TestScanOptionsValidate(t *testing.T) {
type fields struct {
configOnly bool
outputType string
Expand Down Expand Up @@ -293,18 +293,18 @@ func TestScanCommandValidate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := ScanCommand{
s := ScanOptions{
configOnly: tt.fields.configOnly,
outputType: tt.fields.outputType,
}
if err := s.validate(); (err != nil) != tt.wantErr {
t.Errorf("ScanCommand.validate() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("ScanOptions.validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestScanCommandInitColor(t *testing.T) {
func TestScanOptionsInitColor(t *testing.T) {
type fields struct {
useColors string
}
Expand Down Expand Up @@ -342,20 +342,20 @@ func TestScanCommandInitColor(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &ScanCommand{
s := &ScanOptions{
useColors: tt.fields.useColors,
}
s.initColor()
if s.useColors != "auto" {
if s.UseColors != tt.want {
t.Errorf("ScanCommand.initColor() incorrect value for UseColors, got: %v, want %v", s.useColors, tt.want)
t.Errorf("ScanOptions.initColor() incorrect value for UseColors, got: %v, want %v", s.useColors, tt.want)
}
}
})
}
}

func TestScanCommandInit(t *testing.T) {
func TestScanOptionsInit(t *testing.T) {
type fields struct {
configOnly bool
outputType string
Expand Down Expand Up @@ -386,19 +386,19 @@ func TestScanCommandInit(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &ScanCommand{
s := &ScanOptions{
configOnly: tt.fields.configOnly,
outputType: tt.fields.outputType,
useColors: tt.fields.useColors,
}
if err := s.Init(); (err != nil) != tt.wantErr {
t.Errorf("ScanCommand.Init() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("ScanOptions.Init() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestScanCommandStartScan(t *testing.T) {
func TestScanOptionsScan(t *testing.T) {
type fields struct {
policyType []string
iacDirPath string
Expand Down Expand Up @@ -437,14 +437,14 @@ func TestScanCommandStartScan(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &ScanCommand{
s := &ScanOptions{
policyType: tt.fields.policyType,
iacDirPath: tt.fields.iacDirPath,
configOnly: tt.fields.configOnly,
outputType: tt.fields.outputType,
}
if err := s.StartScan(); (err != nil) != tt.wantErr {
t.Errorf("ScanCommand.StartScan() error = %v, wantErr %v", err, tt.wantErr)
if err := s.Scan(); (err != nil) != tt.wantErr {
t.Errorf("ScanOptions.Scan() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
Expand Down
30 changes: 15 additions & 15 deletions pkg/cli/scan.go
Expand Up @@ -26,7 +26,7 @@ import (
"go.uber.org/zap"
)

var scanCommand = new(ScanCommand)
var scanOptions = new(ScanOptions)

var scanCmd = &cobra.Command{
Use: "scan",
Expand All @@ -41,23 +41,23 @@ Detect compliance and security violations across Infrastructure as Code to mitig

func scan(cmd *cobra.Command, args []string) {
zap.S().Debug("running terrascan in cli mode")
scanCommand.configFile = ConfigFile
scanCommand.outputType = OutputType
scanCommand.StartScan()
scanOptions.configFile = ConfigFile
scanOptions.outputType = OutputType
scanOptions.Scan()
}

func init() {
scanCmd.Flags().StringSliceVarP(&scanCommand.policyType, "policy-type", "t", []string{"all"}, fmt.Sprintf("policy type (%s)", strings.Join(policy.SupportedPolicyTypes(true), ", ")))
scanCmd.Flags().StringVarP(&scanCommand.iacType, "iac-type", "i", "", fmt.Sprintf("iac type (%v)", strings.Join(iacProvider.SupportedIacProviders(), ", ")))
scanCmd.Flags().StringVarP(&scanCommand.iacVersion, "iac-version", "", "", fmt.Sprintf("iac version (%v)", strings.Join(iacProvider.SupportedIacVersions(), ", ")))
scanCmd.Flags().StringVarP(&scanCommand.iacFilePath, "iac-file", "f", "", "path to a single IaC file")
scanCmd.Flags().StringVarP(&scanCommand.iacDirPath, "iac-dir", "d", ".", "path to a directory containing one or more IaC files")
scanCmd.Flags().StringArrayVarP(&scanCommand.policyPath, "policy-path", "p", []string{}, "policy path directory")
scanCmd.Flags().StringVarP(&scanCommand.remoteType, "remote-type", "r", "", "type of remote backend (git, s3, gcs, http)")
scanCmd.Flags().StringVarP(&scanCommand.remoteURL, "remote-url", "u", "", "url pointing to remote IaC repository")
scanCmd.Flags().BoolVarP(&scanCommand.configOnly, "config-only", "", false, "will output resource config (should only be used for debugging purposes)")
scanCmd.Flags().StringSliceVarP(&scanOptions.policyType, "policy-type", "t", []string{"all"}, fmt.Sprintf("policy type (%s)", strings.Join(policy.SupportedPolicyTypes(true), ", ")))
scanCmd.Flags().StringVarP(&scanOptions.iacType, "iac-type", "i", "", fmt.Sprintf("iac type (%v)", strings.Join(iacProvider.SupportedIacProviders(), ", ")))
scanCmd.Flags().StringVarP(&scanOptions.iacVersion, "iac-version", "", "", fmt.Sprintf("iac version (%v)", strings.Join(iacProvider.SupportedIacVersions(), ", ")))
scanCmd.Flags().StringVarP(&scanOptions.iacFilePath, "iac-file", "f", "", "path to a single IaC file")
scanCmd.Flags().StringVarP(&scanOptions.iacDirPath, "iac-dir", "d", ".", "path to a directory containing one or more IaC files")
scanCmd.Flags().StringArrayVarP(&scanOptions.policyPath, "policy-path", "p", []string{}, "policy path directory")
scanCmd.Flags().StringVarP(&scanOptions.remoteType, "remote-type", "r", "", "type of remote backend (git, s3, gcs, http)")
scanCmd.Flags().StringVarP(&scanOptions.remoteURL, "remote-url", "u", "", "url pointing to remote IaC repository")
scanCmd.Flags().BoolVarP(&scanOptions.configOnly, "config-only", "", false, "will output resource config (should only be used for debugging purposes)")
// flag passes a string, but we normalize to bool in PreRun
scanCmd.Flags().StringVar(&scanCommand.useColors, "use-colors", "auto", "color output (auto, t, f)")
scanCmd.Flags().BoolVarP(&scanCommand.Verbose, "verbose", "v", false, "will show violations with details (applicable for default output)")
scanCmd.Flags().StringVar(&scanOptions.useColors, "use-colors", "auto", "color output (auto, t, f)")
scanCmd.Flags().BoolVarP(&scanOptions.Verbose, "verbose", "v", false, "will show violations with details (applicable for default output)")
RegisterCommand(rootCmd, scanCmd)
}

0 comments on commit 71645d2

Please sign in to comment.