From 13b09bd4d326f71a8b2c769518b4420b370ffe81 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Mon, 11 Mar 2024 12:46:27 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20do=20not=20use=20log.Fatal=20and?= =?UTF-8?q?=20os.Exit=20in=20config=20gathering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/cnquery/cmd/root.go | 12 +++--------- apps/cnquery/cmd/scan.go | 8 ++++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/apps/cnquery/cmd/root.go b/apps/cnquery/cmd/root.go index ca08fd126c..d89e813418 100644 --- a/apps/cnquery/cmd/root.go +++ b/apps/cnquery/cmd/root.go @@ -9,6 +9,7 @@ import ( "regexp" "strings" + "github.com/cockroachdb/errors" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/cobra" @@ -19,7 +20,6 @@ import ( "go.mondoo.com/cnquery/v10/cli/execruntime" "go.mondoo.com/cnquery/v10/cli/inventoryloader" cliproviders "go.mondoo.com/cnquery/v10/cli/providers" - "go.mondoo.com/cnquery/v10/cli/reporter" "go.mondoo.com/cnquery/v10/cli/theme" "go.mondoo.com/cnquery/v10/logger" "go.mondoo.com/cnquery/v10/providers" @@ -232,7 +232,7 @@ func getCobraScanConfig(cmd *cobra.Command, runtime *providers.Runtime, cliRes * inv, err := inventoryloader.ParseOrUse(cliRes.Asset, viper.GetBool("insecure"), optAnnotations) if err != nil { - log.Fatal().Err(err).Msg("failed to parse inventory") + return nil, errors.Wrap(err, "failed to parse inventory") } // TODO: We currently deduplicate this here because it leads to errors down the line, @@ -249,14 +249,8 @@ func getCobraScanConfig(cmd *cobra.Command, runtime *providers.Runtime, cliRes * runtime: runtime, } - // if users want to get more information on available output options, - // print them before executing the scan + // determine the output format output := viper.GetString("output") - if output == "help" { - fmt.Println("Available output formats: " + reporter.AllFormats()) - os.Exit(0) - } - // --json takes precedence if ok := viper.GetBool("json"); ok { output = "json" diff --git a/apps/cnquery/cmd/scan.go b/apps/cnquery/cmd/scan.go index 21772852aa..e8334ccf5a 100644 --- a/apps/cnquery/cmd/scan.go +++ b/apps/cnquery/cmd/scan.go @@ -114,6 +114,14 @@ var scanCmdRun = func(cmd *cobra.Command, runtime *providers.Runtime, cliRes *pl log.Fatal().Err(err).Msg("failed to prepare config") } + // if users want to get more information on available output options, + // print them before executing the scan + output := viper.GetString("output") + if output == "help" { + fmt.Println("Available output formats: " + reporter.AllFormats()) + os.Exit(0) + } + err = conf.loadBundles() if err != nil { log.Fatal().Err(err).Msg("failed to resolve query packs")