Skip to content

Commit

Permalink
1. update fix for exit code
Browse files Browse the repository at this point in the history
2. remove default command code
  • Loading branch information
patilpankaj212 committed Jan 7, 2021
1 parent b1c8f99 commit c4b9aa7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 34 deletions.
9 changes: 6 additions & 3 deletions pkg/cli/init.go
Expand Up @@ -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() {
Expand Down
24 changes: 0 additions & 24 deletions pkg/cli/register.go
Expand Up @@ -18,7 +18,6 @@ package cli

import (
"flag"
"fmt"
"os"

"github.com/accurics/terrascan/pkg/config"
Expand All @@ -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)")
Expand Down Expand Up @@ -81,9 +59,7 @@ func Execute() {
}
}

setDefaultCommandIfNonePresent()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
13 changes: 6 additions & 7 deletions pkg/cli/scan.go
Expand Up @@ -18,7 +18,6 @@ package cli

import (
"fmt"
"os"
"strings"

iacProvider "github.com/accurics/terrascan/pkg/iac-providers"
Expand All @@ -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() {
Expand Down

0 comments on commit c4b9aa7

Please sign in to comment.