Skip to content

Commit ff6c2a3

Browse files
committed
Log the bubbled up error instead of printing it
Signed-off-by: Richard Wall <[email protected]>
1 parent 7ff0b22 commit ff6c2a3

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

cmd/root.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"os"
66
"strings"
77

8-
"github.com/jetstack/preflight/pkg/logs"
98
"github.com/spf13/cobra"
109
"github.com/spf13/pflag"
10+
"k8s.io/klog/v2"
11+
12+
"github.com/jetstack/preflight/pkg/logs"
1113
)
1214

1315
// rootCmd represents the base command when called without any subcommands
@@ -31,13 +33,16 @@ func init() {
3133

3234
// Execute adds all child commands to the root command and sets flags appropriately.
3335
// This is called by main.main(). It only needs to happen once to the rootCmd.
36+
// If the root command or sub-command returns an error, the error message will
37+
// will be logged and the process will exit with status 1.
3438
func Execute() {
3539
logs.AddFlags(rootCmd.PersistentFlags())
36-
40+
var exitCode int
3741
if err := rootCmd.Execute(); err != nil {
38-
fmt.Println(err)
39-
os.Exit(1)
42+
exitCode = 1
43+
klog.ErrorS(err, "Exiting due to error", "exit-code", exitCode)
4044
}
45+
klog.FlushAndExit(klog.ExitFlushTimeout, exitCode)
4146
}
4247

4348
func setFlagsFromEnv(prefix string, fs *pflag.FlagSet) {

0 commit comments

Comments
 (0)