Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exit code on error #458

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pkg/cli/init.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
}
}
10 changes: 6 additions & 4 deletions pkg/cli/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +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
scanOptions.Scan()
return scanOptions.Scan()
}

func init() {
Expand Down