Skip to content

Commit

Permalink
check outputformat in a single place
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 26, 2024
1 parent 989f95f commit d4955cd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 0 additions & 2 deletions cmd/crowdsec-cli/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions cmd/crowdsec-cli/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ func (cli *cliMachines) list() error {
}

csvwriter.Flush()
default:
return fmt.Errorf("unknown output '%s'", cli.cfg().Cscli.Output)
}

return nil
Expand Down
18 changes: 10 additions & 8 deletions cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit d4955cd

Please sign in to comment.