diff --git a/cmd/crowdsec-cli/hub.go b/cmd/crowdsec-cli/hub.go index 71347a5a5..62eb894d8 100644 --- a/cmd/crowdsec-cli/hub.go +++ b/cmd/crowdsec-cli/hub.go @@ -47,7 +47,9 @@ cscli hub upgrade`, } func (cli *cliHub) list(all bool) error { - hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger()) + cfg := cli.cfg() + + hub, err := require.Hub(cfg, nil, log.StandardLogger()) if err != nil { return err } @@ -69,7 +71,7 @@ func (cli *cliHub) list(all bool) error { } } - err = listItems(color.Output, cwhub.ItemTypes, items, true) + err = listItems(color.Output, cwhub.ItemTypes, items, true, cfg.Cscli.Output) if err != nil { return err } diff --git a/cmd/crowdsec-cli/hubappsec.go b/cmd/crowdsec-cli/hubappsec.go index 7ee578edc..1df3212f9 100644 --- a/cmd/crowdsec-cli/hubappsec.go +++ b/cmd/crowdsec-cli/hubappsec.go @@ -50,7 +50,7 @@ cscli appsec-configs list crowdsecurity/vpatch`, func NewCLIAppsecRule(cfg configGetter) *cliItem { inspectDetail := func(item *cwhub.Item) error { // Only show the converted rules in human mode - if csConfig.Cscli.Output != "human" { + if cfg().Cscli.Output != "human" { return nil } diff --git a/cmd/crowdsec-cli/item_metrics.go b/cmd/crowdsec-cli/item_metrics.go index b571fb1c5..945996879 100644 --- a/cmd/crowdsec-cli/item_metrics.go +++ b/cmd/crowdsec-cli/item_metrics.go @@ -18,22 +18,22 @@ import ( "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) -func ShowMetrics(hubItem *cwhub.Item) error { +func ShowMetrics(prometheusURL string, hubItem *cwhub.Item) error { switch hubItem.Type { case cwhub.PARSERS: - metrics := GetParserMetric(csConfig.Cscli.PrometheusUrl, hubItem.Name) + metrics := GetParserMetric(prometheusURL, hubItem.Name) parserMetricsTable(color.Output, hubItem.Name, metrics) case cwhub.SCENARIOS: - metrics := GetScenarioMetric(csConfig.Cscli.PrometheusUrl, hubItem.Name) + metrics := GetScenarioMetric(prometheusURL, hubItem.Name) scenarioMetricsTable(color.Output, hubItem.Name, metrics) case cwhub.COLLECTIONS: for _, sub := range hubItem.SubItems() { - if err := ShowMetrics(sub); err != nil { + if err := ShowMetrics(prometheusURL, sub); err != nil { return err } } case cwhub.APPSEC_RULES: - metrics := GetAppsecRuleMetric(csConfig.Cscli.PrometheusUrl, hubItem.Name) + metrics := GetAppsecRuleMetric(prometheusURL, hubItem.Name) appsecMetricsTable(color.Output, hubItem.Name, metrics) default: // no metrics for this item type } diff --git a/cmd/crowdsec-cli/item_suggest.go b/cmd/crowdsec-cli/item_suggest.go index 9272abedd..0ea656549 100644 --- a/cmd/crowdsec-cli/item_suggest.go +++ b/cmd/crowdsec-cli/item_suggest.go @@ -36,8 +36,8 @@ func suggestNearestMessage(hub *cwhub.Hub, itemType string, itemName string) str return msg } -func compAllItems(itemType string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - hub, err := require.Hub(csConfig, nil, nil) +func compAllItems(itemType string, args []string, toComplete string, cfg configGetter) ([]string, cobra.ShellCompDirective) { + hub, err := require.Hub(cfg(), nil, nil) if err != nil { return nil, cobra.ShellCompDirectiveDefault } @@ -55,8 +55,8 @@ func compAllItems(itemType string, args []string, toComplete string) ([]string, return comp, cobra.ShellCompDirectiveNoFileComp } -func compInstalledItems(itemType string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - hub, err := require.Hub(csConfig, nil, nil) +func compInstalledItems(itemType string, args []string, toComplete string, cfg configGetter) ([]string, cobra.ShellCompDirective) { + hub, err := require.Hub(cfg(), nil, nil) if err != nil { return nil, cobra.ShellCompDirectiveDefault } diff --git a/cmd/crowdsec-cli/itemcli.go b/cmd/crowdsec-cli/itemcli.go index cdaf78770..44a734d5e 100644 --- a/cmd/crowdsec-cli/itemcli.go +++ b/cmd/crowdsec-cli/itemcli.go @@ -112,7 +112,7 @@ func (cli cliItem) newInstallCmd() *cobra.Command { Args: cobra.MinimumNArgs(1), DisableAutoGenTag: true, ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return compAllItems(cli.name, args, toComplete) + return compAllItems(cli.name, args, toComplete, cli.cfg) }, RunE: func(_ *cobra.Command, args []string) error { return cli.install(args, downloadOnly, force, ignoreError) @@ -238,7 +238,7 @@ func (cli cliItem) newRemoveCmd() *cobra.Command { Aliases: []string{"delete"}, DisableAutoGenTag: true, ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return compInstalledItems(cli.name, args, toComplete) + return compInstalledItems(cli.name, args, toComplete, cli.cfg) }, RunE: func(_ *cobra.Command, args []string) error { return cli.remove(args, purge, force, all) @@ -333,7 +333,7 @@ func (cli cliItem) newUpgradeCmd() *cobra.Command { Example: cli.upgradeHelp.example, DisableAutoGenTag: true, ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return compInstalledItems(cli.name, args, toComplete) + return compInstalledItems(cli.name, args, toComplete, cli.cfg) }, RunE: func(_ *cobra.Command, args []string) error { return cli.upgrade(args, force, all) @@ -381,7 +381,7 @@ func (cli cliItem) inspect(args []string, url string, diff bool, rev bool, noMet continue } - if err = inspectItem(item, !noMetrics); err != nil { + if err = inspectItem(item, !noMetrics, cfg.Cscli.Output, cfg.Cscli.PrometheusUrl); err != nil { return err } @@ -411,7 +411,7 @@ func (cli cliItem) newInspectCmd() *cobra.Command { Args: cobra.MinimumNArgs(1), DisableAutoGenTag: true, ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return compInstalledItems(cli.name, args, toComplete) + return compInstalledItems(cli.name, args, toComplete, cli.cfg) }, RunE: func(_ *cobra.Command, args []string) error { return cli.inspect(args, url, diff, rev, noMetrics) @@ -428,6 +428,8 @@ func (cli cliItem) newInspectCmd() *cobra.Command { } func (cli cliItem) list(args []string, all bool) error { + cfg := cli.cfg() + hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger()) if err != nil { return err @@ -440,7 +442,7 @@ func (cli cliItem) list(args []string, all bool) error { return err } - if err = listItems(color.Output, []string{cli.name}, items, false); err != nil { + if err = listItems(color.Output, []string{cli.name}, items, false, cfg.Cscli.Output); err != nil { return err } diff --git a/cmd/crowdsec-cli/items.go b/cmd/crowdsec-cli/items.go index 4ae643151..9af432c32 100644 --- a/cmd/crowdsec-cli/items.go +++ b/cmd/crowdsec-cli/items.go @@ -54,8 +54,8 @@ func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly b return items, nil } -func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item, omitIfEmpty bool) error { - switch csConfig.Cscli.Output { +func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item, omitIfEmpty bool, output string) error { + switch output { case "human": nothingToDisplay := true @@ -143,8 +143,8 @@ func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item return nil } -func inspectItem(item *cwhub.Item, showMetrics bool) error { - switch csConfig.Cscli.Output { +func inspectItem(item *cwhub.Item, showMetrics bool, output string, prometheusURL string) error { + switch output { case "human", "raw": enc := yaml.NewEncoder(os.Stdout) enc.SetIndent(2) @@ -161,7 +161,7 @@ func inspectItem(item *cwhub.Item, showMetrics bool) error { fmt.Print(string(b)) } - if csConfig.Cscli.Output != "human" { + if output != "human" { return nil } @@ -174,7 +174,7 @@ func inspectItem(item *cwhub.Item, showMetrics bool) error { if showMetrics { fmt.Printf("\nCurrent metrics: \n") - if err := ShowMetrics(item); err != nil { + if err := ShowMetrics(prometheusURL, item); err != nil { return err } } diff --git a/cmd/crowdsec-cli/support.go b/cmd/crowdsec-cli/support.go index 737411e28..418a981ad 100644 --- a/cmd/crowdsec-cli/support.go +++ b/cmd/crowdsec-cli/support.go @@ -154,7 +154,7 @@ func collectHubItems(hub *cwhub.Hub, itemType string) []byte { log.Warnf("could not collect %s list: %s", itemType, err) } - if err := listItems(out, []string{itemType}, items, false); err != nil { + if err := listItems(out, []string{itemType}, items, false, "human"); err != nil { log.Warnf("could not collect %s list: %s", itemType, err) }