refact cscli: hub item - pointer receiver for consistency (#3595)

This commit is contained in:
mmetc 2025-05-01 23:04:42 +02:00 committed by GitHub
parent dafc9c3076
commit 54571d1688
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 26 additions and 25 deletions

View file

@ -22,7 +22,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
)
func (cli cliItem) inspect(ctx context.Context, args []string, url string, diff bool, rev bool, noMetrics bool) error {
func (cli *cliItem) inspect(ctx context.Context, args []string, url string, diff bool, rev bool, noMetrics bool) error {
cfg := cli.cfg()
if rev && !diff {
@ -51,7 +51,7 @@ func (cli cliItem) inspect(ctx context.Context, args []string, url string, diff
}
if diff {
fmt.Println(cli.whyTainted(ctx, hub, contentProvider, item, rev))
fmt.Fprintln(os.Stdout, cli.whyTainted(ctx, hub, contentProvider, item, rev))
continue
}
@ -71,7 +71,7 @@ func (cli cliItem) inspect(ctx context.Context, args []string, url string, diff
}
// return the diff between the installed version and the latest version
func (cli cliItem) itemDiff(ctx context.Context, item *cwhub.Item, contentProvider cwhub.ContentProvider, reverse bool) (string, error) {
func (cli *cliItem) itemDiff(ctx context.Context, item *cwhub.Item, contentProvider cwhub.ContentProvider, reverse bool) (string, error) {
if !item.State.IsInstalled() {
return "", fmt.Errorf("'%s' is not installed", item.FQName())
}
@ -113,7 +113,7 @@ func (cli cliItem) itemDiff(ctx context.Context, item *cwhub.Item, contentProvid
return fmt.Sprintf("%s", diff), nil
}
func (cli cliItem) whyTainted(ctx context.Context, hub *cwhub.Hub, contentProvider cwhub.ContentProvider, item *cwhub.Item, reverse bool) string {
func (cli *cliItem) whyTainted(ctx context.Context, hub *cwhub.Hub, contentProvider cwhub.ContentProvider, item *cwhub.Item, reverse bool) string {
if !item.State.IsInstalled() {
return fmt.Sprintf("# %s is not installed", item.FQName())
}
@ -159,7 +159,7 @@ func (cli cliItem) whyTainted(ctx context.Context, hub *cwhub.Hub, contentProvid
return strings.Join(ret, "\n")
}
func (cli cliItem) newInspectCmd() *cobra.Command {
func (cli *cliItem) newInspectCmd() *cobra.Command {
var (
url string
diff bool
@ -212,7 +212,7 @@ func inspectItem(hub *cwhub.Hub, item *cwhub.Item, wantMetrics bool, output stri
return fmt.Errorf("unable to serialize item: %w", err)
}
fmt.Print(string(b))
fmt.Fprintln(os.Stdout, string(b))
}
if output != "human" {
@ -220,13 +220,11 @@ func inspectItem(hub *cwhub.Hub, item *cwhub.Item, wantMetrics bool, output stri
}
if item.State.Tainted {
fmt.Println()
fmt.Printf(`This item is tainted. Use "%s %s inspect --diff %s" to see why.`, filepath.Base(os.Args[0]), item.Type, item.Name)
fmt.Println()
fmt.Fprintf(os.Stdout, "\nThis item is tainted. Use '%s %s inspect --diff %s' to see why.\n", filepath.Base(os.Args[0]), item.Type, item.Name)
}
if wantMetrics {
fmt.Printf("\nCurrent metrics: \n")
fmt.Fprintf(os.Stdout, "\nCurrent metrics: \n")
if err := showMetrics(prometheusURL, hub, item, wantColor); err != nil {
return err

View file

@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"os"
"slices"
"strings"
@ -43,7 +44,7 @@ func suggestNearestMessage(hub *cwhub.Hub, itemType string, itemName string) str
return msg
}
func (cli cliItem) install(ctx context.Context, args []string, interactive bool, dryRun bool, downloadOnly bool, force bool, ignoreError bool) error {
func (cli *cliItem) install(ctx context.Context, args []string, interactive bool, dryRun bool, downloadOnly bool, force bool, ignoreError bool) error {
cfg := cli.cfg()
hub, err := require.Hub(cfg, log.StandardLogger())
@ -91,7 +92,7 @@ func (cli cliItem) install(ctx context.Context, args []string, interactive bool,
}
if msg := reload.UserMessage(); msg != "" && plan.ReloadNeeded {
fmt.Println("\n" + msg)
fmt.Fprintln(os.Stdout, "\n"+msg)
}
return nil
@ -116,7 +117,7 @@ func compAllItems(itemType string, args []string, toComplete string, cfg configG
return comp, cobra.ShellCompDirectiveNoFileComp
}
func (cli cliItem) newInstallCmd() *cobra.Command {
func (cli *cliItem) newInstallCmd() *cobra.Command {
var (
interactive bool
dryRun bool

View file

@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -15,7 +16,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/hubops"
)
func (cli cliItem) removePlan(hub *cwhub.Hub, args []string, purge bool, force bool, all bool) (*hubops.ActionPlan, error) {
func (cli *cliItem) removePlan(hub *cwhub.Hub, args []string, purge bool, force bool, all bool) (*hubops.ActionPlan, error) {
plan := hubops.NewActionPlan(hub)
if all {
@ -85,7 +86,7 @@ func installedParentNames(item *cwhub.Item) []string {
return ret
}
func (cli cliItem) remove(ctx context.Context, args []string, interactive bool, dryRun bool, purge bool, force bool, all bool) error {
func (cli *cliItem) remove(ctx context.Context, args []string, interactive bool, dryRun bool, purge bool, force bool, all bool) error {
cfg := cli.cfg()
hub, err := require.Hub(cli.cfg(), log.StandardLogger())
@ -106,13 +107,13 @@ func (cli cliItem) remove(ctx context.Context, args []string, interactive bool,
}
if msg := reload.UserMessage(); msg != "" && plan.ReloadNeeded {
fmt.Println("\n" + msg)
fmt.Fprintln(os.Stdout, "\n"+msg)
}
return nil
}
func (cli cliItem) newRemoveCmd() *cobra.Command {
func (cli *cliItem) newRemoveCmd() *cobra.Command {
var (
interactive bool
dryRun bool

View file

@ -4,6 +4,7 @@ import (
"cmp"
"context"
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -14,7 +15,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/hubops"
)
func (cli cliItem) upgradePlan(hub *cwhub.Hub, contentProvider cwhub.ContentProvider, args []string, force bool, all bool) (*hubops.ActionPlan, error) {
func (cli *cliItem) upgradePlan(hub *cwhub.Hub, contentProvider cwhub.ContentProvider, args []string, force bool, all bool) (*hubops.ActionPlan, error) {
plan := hubops.NewActionPlan(hub)
if all {
@ -45,7 +46,7 @@ func (cli cliItem) upgradePlan(hub *cwhub.Hub, contentProvider cwhub.ContentProv
return plan, nil
}
func (cli cliItem) upgrade(ctx context.Context, args []string, interactive bool, dryRun bool, force bool, all bool) error {
func (cli *cliItem) upgrade(ctx context.Context, args []string, interactive bool, dryRun bool, force bool, all bool) error {
cfg := cli.cfg()
hub, err := require.Hub(cfg, log.StandardLogger())
@ -68,13 +69,13 @@ func (cli cliItem) upgrade(ctx context.Context, args []string, interactive bool,
}
if msg := reload.UserMessage(); msg != "" && plan.ReloadNeeded {
fmt.Println("\n" + msg)
fmt.Fprintln(os.Stdout, "\n"+msg)
}
return nil
}
func (cli cliItem) newUpgradeCmd() *cobra.Command {
func (cli *cliItem) newUpgradeCmd() *cobra.Command {
var (
interactive bool
dryRun bool

View file

@ -40,7 +40,7 @@ type cliItem struct {
listHelp cliHelp
}
func (cli cliItem) NewCommand() *cobra.Command {
func (cli *cliItem) NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: cmp.Or(cli.help.use, cli.name+" <action> [item]..."),
Short: cmp.Or(cli.help.short, "Manage hub "+cli.name),
@ -59,7 +59,7 @@ func (cli cliItem) NewCommand() *cobra.Command {
return cmd
}
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(), log.StandardLogger())
@ -77,7 +77,7 @@ func (cli cliItem) list(args []string, all bool) error {
return clihub.ListItems(color.Output, cfg.Cscli.Color, []string{cli.name}, items, false, cfg.Cscli.Output)
}
func (cli cliItem) newListCmd() *cobra.Command {
func (cli *cliItem) newListCmd() *cobra.Command {
var all bool
cmd := &cobra.Command{
@ -97,7 +97,7 @@ func (cli cliItem) newListCmd() *cobra.Command {
return cmd
}
func compInstalledItems(itemType string, args []string, toComplete string, cfg configGetter) ([]string, cobra.ShellCompDirective) {
func compInstalledItems(itemType string, _ []string, toComplete string, cfg configGetter) ([]string, cobra.ShellCompDirective) {
hub, err := require.Hub(cfg(), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveDefault