cscli: avoid global usage
This is required to make it possible to split the package
This commit is contained in:
parent
05b54687b6
commit
830e85033e
7 changed files with 29 additions and 25 deletions
|
@ -47,7 +47,9 @@ cscli hub upgrade`,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHub) list(all bool) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ cscli appsec-configs list crowdsecurity/vpatch`,
|
||||||
func NewCLIAppsecRule(cfg configGetter) *cliItem {
|
func NewCLIAppsecRule(cfg configGetter) *cliItem {
|
||||||
inspectDetail := func(item *cwhub.Item) error {
|
inspectDetail := func(item *cwhub.Item) error {
|
||||||
// Only show the converted rules in human mode
|
// Only show the converted rules in human mode
|
||||||
if csConfig.Cscli.Output != "human" {
|
if cfg().Cscli.Output != "human" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,22 +18,22 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ShowMetrics(hubItem *cwhub.Item) error {
|
func ShowMetrics(prometheusURL string, hubItem *cwhub.Item) error {
|
||||||
switch hubItem.Type {
|
switch hubItem.Type {
|
||||||
case cwhub.PARSERS:
|
case cwhub.PARSERS:
|
||||||
metrics := GetParserMetric(csConfig.Cscli.PrometheusUrl, hubItem.Name)
|
metrics := GetParserMetric(prometheusURL, hubItem.Name)
|
||||||
parserMetricsTable(color.Output, hubItem.Name, metrics)
|
parserMetricsTable(color.Output, hubItem.Name, metrics)
|
||||||
case cwhub.SCENARIOS:
|
case cwhub.SCENARIOS:
|
||||||
metrics := GetScenarioMetric(csConfig.Cscli.PrometheusUrl, hubItem.Name)
|
metrics := GetScenarioMetric(prometheusURL, hubItem.Name)
|
||||||
scenarioMetricsTable(color.Output, hubItem.Name, metrics)
|
scenarioMetricsTable(color.Output, hubItem.Name, metrics)
|
||||||
case cwhub.COLLECTIONS:
|
case cwhub.COLLECTIONS:
|
||||||
for _, sub := range hubItem.SubItems() {
|
for _, sub := range hubItem.SubItems() {
|
||||||
if err := ShowMetrics(sub); err != nil {
|
if err := ShowMetrics(prometheusURL, sub); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case cwhub.APPSEC_RULES:
|
case cwhub.APPSEC_RULES:
|
||||||
metrics := GetAppsecRuleMetric(csConfig.Cscli.PrometheusUrl, hubItem.Name)
|
metrics := GetAppsecRuleMetric(prometheusURL, hubItem.Name)
|
||||||
appsecMetricsTable(color.Output, hubItem.Name, metrics)
|
appsecMetricsTable(color.Output, hubItem.Name, metrics)
|
||||||
default: // no metrics for this item type
|
default: // no metrics for this item type
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,8 +36,8 @@ func suggestNearestMessage(hub *cwhub.Hub, itemType string, itemName string) str
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func compAllItems(itemType string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
func compAllItems(itemType string, args []string, toComplete string, cfg configGetter) ([]string, cobra.ShellCompDirective) {
|
||||||
hub, err := require.Hub(csConfig, nil, nil)
|
hub, err := require.Hub(cfg(), nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, cobra.ShellCompDirectiveDefault
|
return nil, cobra.ShellCompDirectiveDefault
|
||||||
}
|
}
|
||||||
|
@ -55,8 +55,8 @@ func compAllItems(itemType string, args []string, toComplete string) ([]string,
|
||||||
return comp, cobra.ShellCompDirectiveNoFileComp
|
return comp, cobra.ShellCompDirectiveNoFileComp
|
||||||
}
|
}
|
||||||
|
|
||||||
func compInstalledItems(itemType string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
func compInstalledItems(itemType string, args []string, toComplete string, cfg configGetter) ([]string, cobra.ShellCompDirective) {
|
||||||
hub, err := require.Hub(csConfig, nil, nil)
|
hub, err := require.Hub(cfg(), nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, cobra.ShellCompDirectiveDefault
|
return nil, cobra.ShellCompDirectiveDefault
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,7 +112,7 @@ func (cli cliItem) newInstallCmd() *cobra.Command {
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
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 {
|
RunE: func(_ *cobra.Command, args []string) error {
|
||||||
return cli.install(args, downloadOnly, force, ignoreError)
|
return cli.install(args, downloadOnly, force, ignoreError)
|
||||||
|
@ -238,7 +238,7 @@ func (cli cliItem) newRemoveCmd() *cobra.Command {
|
||||||
Aliases: []string{"delete"},
|
Aliases: []string{"delete"},
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
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 {
|
RunE: func(_ *cobra.Command, args []string) error {
|
||||||
return cli.remove(args, purge, force, all)
|
return cli.remove(args, purge, force, all)
|
||||||
|
@ -333,7 +333,7 @@ func (cli cliItem) newUpgradeCmd() *cobra.Command {
|
||||||
Example: cli.upgradeHelp.example,
|
Example: cli.upgradeHelp.example,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
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 {
|
RunE: func(_ *cobra.Command, args []string) error {
|
||||||
return cli.upgrade(args, force, all)
|
return cli.upgrade(args, force, all)
|
||||||
|
@ -381,7 +381,7 @@ func (cli cliItem) inspect(args []string, url string, diff bool, rev bool, noMet
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = inspectItem(item, !noMetrics); err != nil {
|
if err = inspectItem(item, !noMetrics, cfg.Cscli.Output, cfg.Cscli.PrometheusUrl); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,7 +411,7 @@ func (cli cliItem) newInspectCmd() *cobra.Command {
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
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 {
|
RunE: func(_ *cobra.Command, args []string) error {
|
||||||
return cli.inspect(args, url, diff, rev, noMetrics)
|
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 {
|
func (cli cliItem) list(args []string, all bool) error {
|
||||||
|
cfg := cli.cfg()
|
||||||
|
|
||||||
hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
|
hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -440,7 +442,7 @@ func (cli cliItem) list(args []string, all bool) error {
|
||||||
return err
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,8 +54,8 @@ func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly b
|
||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item, omitIfEmpty bool) error {
|
func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item, omitIfEmpty bool, output string) error {
|
||||||
switch csConfig.Cscli.Output {
|
switch output {
|
||||||
case "human":
|
case "human":
|
||||||
nothingToDisplay := true
|
nothingToDisplay := true
|
||||||
|
|
||||||
|
@ -143,8 +143,8 @@ func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func inspectItem(item *cwhub.Item, showMetrics bool) error {
|
func inspectItem(item *cwhub.Item, showMetrics bool, output string, prometheusURL string) error {
|
||||||
switch csConfig.Cscli.Output {
|
switch output {
|
||||||
case "human", "raw":
|
case "human", "raw":
|
||||||
enc := yaml.NewEncoder(os.Stdout)
|
enc := yaml.NewEncoder(os.Stdout)
|
||||||
enc.SetIndent(2)
|
enc.SetIndent(2)
|
||||||
|
@ -161,7 +161,7 @@ func inspectItem(item *cwhub.Item, showMetrics bool) error {
|
||||||
fmt.Print(string(b))
|
fmt.Print(string(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
if csConfig.Cscli.Output != "human" {
|
if output != "human" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +174,7 @@ func inspectItem(item *cwhub.Item, showMetrics bool) error {
|
||||||
if showMetrics {
|
if showMetrics {
|
||||||
fmt.Printf("\nCurrent metrics: \n")
|
fmt.Printf("\nCurrent metrics: \n")
|
||||||
|
|
||||||
if err := ShowMetrics(item); err != nil {
|
if err := ShowMetrics(prometheusURL, item); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -154,7 +154,7 @@ func collectHubItems(hub *cwhub.Hub, itemType string) []byte {
|
||||||
log.Warnf("could not collect %s list: %s", itemType, err)
|
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)
|
log.Warnf("could not collect %s list: %s", itemType, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue