Skip to content

Commit

Permalink
🧹 do not use log.Fatal and os.Exit in config gathering
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Mar 11, 2024
1 parent a5f86d3 commit 13b09bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 3 additions & 9 deletions apps/cnquery/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"
"strings"

"github.com/cockroachdb/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand All @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions apps/cnquery/cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 13b09bd

Please sign in to comment.