cscli: avoid global usage

This is required to make it possible to split the package
This commit is contained in:
marco 2024-04-29 16:25:03 +02:00
parent 05b54687b6
commit 830e85033e
7 changed files with 29 additions and 25 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}
}

View file

@ -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)
}