From d4955cded758d68b872a100c9fcb225c4aac5afd Mon Sep 17 00:00:00 2001 From: marco Date: Fri, 26 Jan 2024 09:35:43 +0100 Subject: [PATCH] check outputformat in a single place --- cmd/crowdsec-cli/items.go | 2 -- cmd/crowdsec-cli/machines.go | 2 -- cmd/crowdsec-cli/main.go | 18 ++++++++++-------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/cmd/crowdsec-cli/items.go b/cmd/crowdsec-cli/items.go index a1d079747fa..851be553f15 100644 --- a/cmd/crowdsec-cli/items.go +++ b/cmd/crowdsec-cli/items.go @@ -138,8 +138,6 @@ func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item } csvwriter.Flush() - default: - return fmt.Errorf("unknown output format '%s'", csConfig.Cscli.Output) } return nil diff --git a/cmd/crowdsec-cli/machines.go b/cmd/crowdsec-cli/machines.go index 339b9339044..472da32031d 100644 --- a/cmd/crowdsec-cli/machines.go +++ b/cmd/crowdsec-cli/machines.go @@ -185,8 +185,6 @@ func (cli *cliMachines) list() error { } csvwriter.Flush() - default: - return fmt.Errorf("unknown output '%s'", cli.cfg().Cscli.Output) } return nil diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index c61df50de92..e083b2aeb9a 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -21,7 +21,7 @@ var ConfigFilePath string var csConfig *csconfig.Config var dbClient *database.Client -var OutputFormat string +var outputFormat string var OutputColor string var mergedConfig string @@ -64,16 +64,18 @@ func initConfig() { csConfig.Cscli.HubBranch = flagBranch } - if OutputFormat != "" { - csConfig.Cscli.Output = OutputFormat - - if OutputFormat != "json" && OutputFormat != "raw" && OutputFormat != "human" { - log.Fatalf("output format %s unknown", OutputFormat) - } + if outputFormat != "" { + csConfig.Cscli.Output = outputFormat } + if csConfig.Cscli.Output == "" { csConfig.Cscli.Output = "human" } + + if csConfig.Cscli.Output != "human" && csConfig.Cscli.Output != "json" && csConfig.Cscli.Output != "raw" { + log.Fatalf("output format '%s' not supported: must be one of human, json, raw", csConfig.Cscli.Output) + } + if csConfig.Cscli.Output == "json" { log.SetFormatter(&log.JSONFormatter{}) log.SetLevel(log.ErrorLevel) @@ -146,7 +148,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall cmd.SetOut(color.Output) cmd.PersistentFlags().StringVarP(&ConfigFilePath, "config", "c", csconfig.DefaultConfigPath("config.yaml"), "path to crowdsec config file") - cmd.PersistentFlags().StringVarP(&OutputFormat, "output", "o", "", "Output format: human, json, raw") + cmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", "", "Output format: human, json, raw") cmd.PersistentFlags().StringVarP(&OutputColor, "color", "", "auto", "Output color: yes, no, auto") cmd.PersistentFlags().BoolVar(&dbg_lvl, "debug", false, "Set logging to debug") cmd.PersistentFlags().BoolVar(&nfo_lvl, "info", false, "Set logging to info")