Skip to content

Commit

Permalink
cscli refact: package 'clihub' (#3198)
Browse files Browse the repository at this point in the history
* cscli refact: package 'clihub'

* check for errors
  • Loading branch information
mmetc authored Aug 28, 2024
1 parent eec32ad commit b880df9
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 17 additions & 11 deletions cmd/crowdsec-cli/hub.go → cmd/crowdsec-cli/clihub/hub.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package main
package clihub

import (
"context"
"encoding/json"
"fmt"
"io"

"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"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,
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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)
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clihub

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clihub

import (
"encoding/csv"
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clihub

import (
"fmt"
Expand Down
7 changes: 4 additions & 3 deletions cmd/crowdsec-cli/itemcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
42 changes: 20 additions & 22 deletions cmd/crowdsec-cli/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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())

Expand All @@ -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())

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b880df9

Please sign in to comment.