From d232ae0fb9de0ec67a1213a681033c9d4b3da129 Mon Sep 17 00:00:00 2001 From: Huang Huang Date: Sun, 23 Jan 2022 15:40:59 +0800 Subject: [PATCH] Fix the `--exit-code` flag doesn't work when run with subcommand (#1084) --- cmd/master.go | 2 ++ cmd/node.go | 2 ++ cmd/root.go | 3 +-- cmd/run.go | 6 ++++-- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/master.go b/cmd/master.go index 99d9cb3..590d978 100644 --- a/cmd/master.go +++ b/cmd/master.go @@ -16,6 +16,7 @@ package cmd import ( "fmt" + "os" "github.com/aquasecurity/kube-bench/check" "github.com/spf13/cobra" @@ -36,6 +37,7 @@ var masterCmd = &cobra.Command{ filename := loadConfig(check.MASTER, bv) runChecks(check.MASTER, filename, detecetedKubeVersion) writeOutput(controlsCollection) + os.Exit(exitCodeSelection(controlsCollection)) }, Deprecated: "this command will be retired soon. Please use the `run` command with `--targets=master` instead.", } diff --git a/cmd/node.go b/cmd/node.go index 5672297..63e8e28 100644 --- a/cmd/node.go +++ b/cmd/node.go @@ -16,6 +16,7 @@ package cmd import ( "fmt" + "os" "github.com/aquasecurity/kube-bench/check" "github.com/spf13/cobra" @@ -36,6 +37,7 @@ var nodeCmd = &cobra.Command{ filename := loadConfig(check.NODE, bv) runChecks(check.NODE, filename, detecetedKubeVersion) writeOutput(controlsCollection) + os.Exit(exitCodeSelection(controlsCollection)) }, Deprecated: "this command will be retired soon. Please use the `run` command with `--targets=node` instead.", } diff --git a/cmd/root.go b/cmd/root.go index 785d021..ebfaa67 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -136,8 +136,7 @@ var RootCmd = &cobra.Command{ } writeOutput(controlsCollection) - exitCode := exitCodeSelection(controlsCollection) - os.Exit(exitCode) + os.Exit(exitCodeSelection(controlsCollection)) }, } diff --git a/cmd/run.go b/cmd/run.go index db92fe7..8dd3922 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -54,13 +54,15 @@ var runCmd = &cobra.Command{ path := filepath.Join(cfgDir, bv) err = mergeConfig(path) if err != nil { - fmt.Printf("Error in mergeConfig: %v\n", err) + exitWithError(fmt.Errorf("Error in mergeConfig: %v\n", err)) } err = run(targets, bv) if err != nil { - fmt.Printf("Error in run: %v\n", err) + exitWithError(fmt.Errorf("Error in run: %v\n", err)) } + + os.Exit(exitCodeSelection(controlsCollection)) }, }