From b880df9a683d249c2152408fb48d99b11dd1217f Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Wed, 28 Aug 2024 11:52:25 +0200 Subject: [PATCH] cscli refact: package 'clihub' (#3198) * cscli refact: package 'clihub' * check for errors --- .golangci.yml | 2 +- cmd/crowdsec-cli/{ => clihub}/hub.go | 28 ++++++++----- cmd/crowdsec-cli/{ => clihub}/item_metrics.go | 2 +- cmd/crowdsec-cli/{ => clihub}/items.go | 8 ++-- cmd/crowdsec-cli/{ => clihub}/utils_table.go | 2 +- cmd/crowdsec-cli/itemcli.go | 7 ++-- cmd/crowdsec-cli/main.go | 3 +- cmd/crowdsec-cli/support.go | 42 +++++++++---------- 8 files changed, 50 insertions(+), 44 deletions(-) rename cmd/crowdsec-cli/{ => clihub}/hub.go (90%) rename cmd/crowdsec-cli/{ => clihub}/item_metrics.go (99%) rename cmd/crowdsec-cli/{ => clihub}/items.go (95%) rename cmd/crowdsec-cli/{ => clihub}/utils_table.go (99%) diff --git a/.golangci.yml b/.golangci.yml index 62147b54101..c59ab372799 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -482,7 +482,7 @@ issues: - linters: - revive - path: "cmd/crowdsec-cli/item_metrics.go" + path: "cmd/crowdsec-cli/clihub/item_metrics.go" text: "deep-exit: .*" - linters: diff --git a/cmd/crowdsec-cli/hub.go b/cmd/crowdsec-cli/clihub/hub.go similarity index 90% rename from cmd/crowdsec-cli/hub.go rename to cmd/crowdsec-cli/clihub/hub.go index 34bd61b1277..22568355546 100644 --- a/cmd/crowdsec-cli/hub.go +++ b/cmd/crowdsec-cli/clihub/hub.go @@ -1,9 +1,10 @@ -package main +package clihub import ( "context" "encoding/json" "fmt" + "io" "github.com/fatih/color" log "github.com/sirupsen/logrus" @@ -11,14 +12,17 @@ import ( "gopkg.in/yaml.v3" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" + "github.com/crowdsecurity/crowdsec/pkg/csconfig" "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) +type configGetter = func() *csconfig.Config + type cliHub struct { cfg configGetter } -func NewCLIHub(cfg configGetter) *cliHub { +func New(cfg configGetter) *cliHub { return &cliHub{ cfg: cfg, } @@ -47,14 +51,9 @@ cscli hub upgrade`, return cmd } -func (cli *cliHub) list(all bool) error { +func (cli *cliHub) List(out io.Writer, hub *cwhub.Hub, all bool) error { cfg := cli.cfg() - hub, err := require.Hub(cfg, nil, log.StandardLogger()) - if err != nil { - return err - } - for _, v := range hub.Warnings { log.Info(v) } @@ -65,14 +64,16 @@ func (cli *cliHub) list(all bool) error { items := make(map[string][]*cwhub.Item) + var err error + for _, itemType := range cwhub.ItemTypes { - items[itemType], err = selectItems(hub, itemType, nil, !all) + items[itemType], err = SelectItems(hub, itemType, nil, !all) if err != nil { return err } } - err = listItems(color.Output, cfg.Cscli.Color, cwhub.ItemTypes, items, true, cfg.Cscli.Output) + err = ListItems(out, cfg.Cscli.Color, cwhub.ItemTypes, items, true, cfg.Cscli.Output) if err != nil { return err } @@ -89,7 +90,12 @@ func (cli *cliHub) newListCmd() *cobra.Command { Args: cobra.ExactArgs(0), DisableAutoGenTag: true, RunE: func(_ *cobra.Command, _ []string) error { - return cli.list(all) + hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger()) + if err != nil { + return err + } + + return cli.List(color.Output, hub, all) }, } diff --git a/cmd/crowdsec-cli/item_metrics.go b/cmd/crowdsec-cli/clihub/item_metrics.go similarity index 99% rename from cmd/crowdsec-cli/item_metrics.go rename to cmd/crowdsec-cli/clihub/item_metrics.go index f00ae08b00b..aaee63d1d38 100644 --- a/cmd/crowdsec-cli/item_metrics.go +++ b/cmd/crowdsec-cli/clihub/item_metrics.go @@ -1,4 +1,4 @@ -package main +package clihub import ( "net/http" diff --git a/cmd/crowdsec-cli/items.go b/cmd/crowdsec-cli/clihub/items.go similarity index 95% rename from cmd/crowdsec-cli/items.go rename to cmd/crowdsec-cli/clihub/items.go index 5a4fee4d582..4dd3c46e0f9 100644 --- a/cmd/crowdsec-cli/items.go +++ b/cmd/crowdsec-cli/clihub/items.go @@ -1,4 +1,4 @@ -package main +package clihub import ( "encoding/csv" @@ -16,7 +16,7 @@ import ( ) // selectItems returns a slice of items of a given type, selected by name and sorted by case-insensitive name -func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly bool) ([]*cwhub.Item, error) { +func SelectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly bool) ([]*cwhub.Item, error) { allItems := hub.GetItemsByType(itemType, true) itemNames := make([]string, len(allItems)) @@ -57,7 +57,7 @@ func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly b return wantedItems, nil } -func listItems(out io.Writer, wantColor string, itemTypes []string, items map[string][]*cwhub.Item, omitIfEmpty bool, output string) error { +func ListItems(out io.Writer, wantColor string, itemTypes []string, items map[string][]*cwhub.Item, omitIfEmpty bool, output string) error { switch output { case "human": nothingToDisplay := true @@ -146,7 +146,7 @@ func listItems(out io.Writer, wantColor string, itemTypes []string, items map[st return nil } -func inspectItem(item *cwhub.Item, showMetrics bool, output string, prometheusURL string, wantColor string) error { +func InspectItem(item *cwhub.Item, showMetrics bool, output string, prometheusURL string, wantColor string) error { switch output { case "human", "raw": enc := yaml.NewEncoder(os.Stdout) diff --git a/cmd/crowdsec-cli/utils_table.go b/cmd/crowdsec-cli/clihub/utils_table.go similarity index 99% rename from cmd/crowdsec-cli/utils_table.go rename to cmd/crowdsec-cli/clihub/utils_table.go index 6df16cd85f5..018071d91bb 100644 --- a/cmd/crowdsec-cli/utils_table.go +++ b/cmd/crowdsec-cli/clihub/utils_table.go @@ -1,4 +1,4 @@ -package main +package clihub import ( "fmt" diff --git a/cmd/crowdsec-cli/itemcli.go b/cmd/crowdsec-cli/itemcli.go index 11ac1232eae..a5629b425b9 100644 --- a/cmd/crowdsec-cli/itemcli.go +++ b/cmd/crowdsec-cli/itemcli.go @@ -15,6 +15,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihub" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/reload" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/cwhub" @@ -372,7 +373,7 @@ func (cli cliItem) inspect(ctx context.Context, args []string, url string, diff continue } - if err = inspectItem(item, !noMetrics, cfg.Cscli.Output, cfg.Cscli.PrometheusUrl, cfg.Cscli.Color); err != nil { + if err = clihub.InspectItem(item, !noMetrics, cfg.Cscli.Output, cfg.Cscli.PrometheusUrl, cfg.Cscli.Color); err != nil { return err } @@ -428,12 +429,12 @@ func (cli cliItem) list(args []string, all bool) error { items := make(map[string][]*cwhub.Item) - items[cli.name], err = selectItems(hub, cli.name, args, !all) + items[cli.name], err = clihub.SelectItems(hub, cli.name, args, !all) if err != nil { return err } - return listItems(color.Output, cfg.Cscli.Color, []string{cli.name}, items, false, cfg.Cscli.Output) + return clihub.ListItems(color.Output, cfg.Cscli.Color, []string{cli.name}, items, false, cfg.Cscli.Output) } func (cli cliItem) newListCmd() *cobra.Command { diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 12128f6091a..2153ebfb7bb 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -17,6 +17,7 @@ import ( "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clicapi" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliconsole" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliexplain" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihub" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihubtest" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clilapi" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/climetrics" @@ -249,7 +250,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall cmd.AddCommand(NewCLIDoc().NewCommand(cmd)) cmd.AddCommand(NewCLIVersion().NewCommand()) cmd.AddCommand(NewCLIConfig(cli.cfg).NewCommand()) - cmd.AddCommand(NewCLIHub(cli.cfg).NewCommand()) + cmd.AddCommand(clihub.New(cli.cfg).NewCommand()) cmd.AddCommand(climetrics.New(cli.cfg).NewCommand()) cmd.AddCommand(NewCLIDashboard(cli.cfg).NewCommand()) cmd.AddCommand(NewCLIDecisions(cli.cfg).NewCommand()) diff --git a/cmd/crowdsec-cli/support.go b/cmd/crowdsec-cli/support.go index 3ab1415f2ec..c48c84668ea 100644 --- a/cmd/crowdsec-cli/support.go +++ b/cmd/crowdsec-cli/support.go @@ -23,6 +23,7 @@ import ( "github.com/crowdsecurity/go-cs-lib/trace" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clicapi" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihub" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clilapi" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/climetrics" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" @@ -38,7 +39,7 @@ const ( SUPPORT_VERSION_PATH = "version.txt" SUPPORT_FEATURES_PATH = "features.txt" SUPPORT_OS_INFO_PATH = "osinfo.txt" - SUPPORT_HUB_DIR = "hub/" + SUPPORT_HUB = "hub.txt" SUPPORT_BOUNCERS_PATH = "lapi/bouncers.txt" SUPPORT_AGENTS_PATH = "lapi/agents.txt" SUPPORT_CROWDSEC_CONFIG_PATH = "config/crowdsec.yaml" @@ -163,26 +164,23 @@ func (cli *cliSupport) dumpOSInfo(zw *zip.Writer) error { return nil } -func (cli *cliSupport) dumpHubItems(zw *zip.Writer, hub *cwhub.Hub, itemType string) error { - var err error - - out := new(bytes.Buffer) - - log.Infof("Collecting hub: %s", itemType) +func (cli *cliSupport) dumpHubItems(zw *zip.Writer, hub *cwhub.Hub) error { + if hub == nil { + return errors.New("no hub connection") + } - items := make(map[string][]*cwhub.Item) + log.Infof("Collecting hub") - if items[itemType], err = selectItems(hub, itemType, nil, true); err != nil { - return fmt.Errorf("could not collect %s list: %w", itemType, err) - } + out := new(bytes.Buffer) - if err := listItems(out, cli.cfg().Cscli.Color, []string{itemType}, items, false, "human"); err != nil { - return fmt.Errorf("could not list %s: %w", itemType, err) + ch := clihub.New(cli.cfg) + if err := ch.List(out, hub, false); err != nil { + return err } stripped := stripAnsiString(out.String()) - cli.writeToZip(zw, SUPPORT_HUB_DIR+itemType+".txt", time.Now(), strings.NewReader(stripped)) + cli.writeToZip(zw, SUPPORT_HUB, time.Now(), strings.NewReader(stripped)) return nil } @@ -198,7 +196,9 @@ func (cli *cliSupport) dumpBouncers(zw *zip.Writer, db *database.Client) error { // call the "cscli bouncers list" command directly, skip any preRun cm := cliBouncers{db: db, cfg: cli.cfg} - cm.list(out) + if err := cm.list(out); err != nil { + return err + } stripped := stripAnsiString(out.String()) @@ -218,7 +218,9 @@ func (cli *cliSupport) dumpAgents(zw *zip.Writer, db *database.Client) error { // call the "cscli machines list" command directly, skip any preRun cm := cliMachines{db: db, cfg: cli.cfg} - cm.list(out) + if err := cm.list(out); err != nil { + return err + } stripped := stripAnsiString(out.String()) @@ -513,12 +515,8 @@ func (cli *cliSupport) dump(ctx context.Context, outFile string) error { log.Warnf("could not collect main config file: %s", err) } - if hub != nil { - for _, itemType := range cwhub.ItemTypes { - if err = cli.dumpHubItems(zipWriter, hub, itemType); err != nil { - log.Warnf("could not collect %s information: %s", itemType, err) - } - } + if err = cli.dumpHubItems(zipWriter, hub); err != nil { + log.Warnf("could not collect hub information: %s", err) } if err = cli.dumpBouncers(zipWriter, db); err != nil {