From c4b9aa7227572867309db3df3ca6fe271a4374a4 Mon Sep 17 00:00:00 2001 From: Pankaj Patil Date: Thu, 7 Jan 2021 13:47:24 +0530 Subject: [PATCH] 1. update fix for exit code 2. remove default command code --- pkg/cli/init.go | 9 ++++++--- pkg/cli/register.go | 24 ------------------------ pkg/cli/scan.go | 13 ++++++------- 3 files changed, 12 insertions(+), 34 deletions(-) diff --git a/pkg/cli/init.go b/pkg/cli/init.go index e05a23fce..db97e1db9 100644 --- a/pkg/cli/init.go +++ b/pkg/cli/init.go @@ -29,15 +29,18 @@ var initCmd = &cobra.Command{ Initializes Terrascan and clones policies from the Terrascan GitHub repository. `, - Run: initial, + RunE: initial, + SilenceUsage: true, + SilenceErrors: true, } -func initial(cmd *cobra.Command, args []string) { +func initial(cmd *cobra.Command, args []string) error { // initialize terrascan if err := initialize.Run(); err != nil { zap.S().Error("failed to initialize terrascan") - return + return err } + return nil } func init() { diff --git a/pkg/cli/register.go b/pkg/cli/register.go index 81fd7fc3a..98da7e4b9 100644 --- a/pkg/cli/register.go +++ b/pkg/cli/register.go @@ -18,7 +18,6 @@ package cli import ( "flag" - "fmt" "os" "github.com/accurics/terrascan/pkg/config" @@ -31,27 +30,6 @@ func RegisterCommand(baseCommand *cobra.Command, command *cobra.Command) { baseCommand.AddCommand(command) } -func subCommands() (commandNames []string) { - for _, command := range rootCmd.Commands() { - commandNames = append(commandNames, append(command.Aliases, command.Name())...) - } - return -} - -// setDefaultCommand sets `scan` as default command if no other command is specified -func setDefaultCommandIfNonePresent() { - if len(os.Args) > 1 { - potentialCommand := os.Args[1] - for _, command := range subCommands() { - if command == potentialCommand { - return - } - } - os.Args = append([]string{os.Args[0], "scan"}, os.Args[1:]...) - } - -} - // Execute the entrypoint called by main func Execute() { rootCmd.PersistentFlags().StringVarP(&LogLevel, "log-level", "l", "info", "log level (debug, info, warn, error, panic, fatal)") @@ -81,9 +59,7 @@ func Execute() { } } - setDefaultCommandIfNonePresent() if err := rootCmd.Execute(); err != nil { - fmt.Println(err) os.Exit(1) } } diff --git a/pkg/cli/scan.go b/pkg/cli/scan.go index 04dd1a826..14461cc18 100644 --- a/pkg/cli/scan.go +++ b/pkg/cli/scan.go @@ -18,7 +18,6 @@ package cli import ( "fmt" - "os" "strings" iacProvider "github.com/accurics/terrascan/pkg/iac-providers" @@ -36,17 +35,17 @@ var scanCmd = &cobra.Command{ Detect compliance and security violations across Infrastructure as Code to mitigate risk before provisioning cloud native infrastructure. `, - PreRun: initial, - Run: scan, + PreRunE: initial, + RunE: scan, + SilenceUsage: true, + SilenceErrors: true, } -func scan(cmd *cobra.Command, args []string) { +func scan(cmd *cobra.Command, args []string) error { zap.S().Debug("running terrascan in cli mode") scanOptions.configFile = ConfigFile scanOptions.outputType = OutputType - if err := scanOptions.Scan(); err != nil { - os.Exit(1) - } + return scanOptions.Scan() } func init() {