Skip to content

Commit

Permalink
cscli refact: package clibouncer, climachine
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Aug 28, 2024
1 parent 3dcc311 commit e02b0dc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clibouncer

import (
"encoding/csv"
Expand All @@ -21,19 +21,22 @@ import (
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/database"
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/bouncer"
"github.com/crowdsecurity/crowdsec/pkg/emoji"
"github.com/crowdsecurity/crowdsec/pkg/types"
)

type configGetter = func() *csconfig.Config

type cliBouncers struct {
db *database.Client
cfg configGetter
}

func NewCLIBouncers(cfg configGetter) *cliBouncers {
func New(cfg configGetter) *cliBouncers {
return &cliBouncers{
cfg: cfg,
}
Expand Down Expand Up @@ -156,8 +159,11 @@ func (cli *cliBouncers) listCSV(out io.Writer, bouncers ent.Bouncers) error {
return nil
}

func (cli *cliBouncers) list(out io.Writer) error {
bouncers, err := cli.db.ListBouncers()
func (cli *cliBouncers) List(out io.Writer, db *database.Client) error {
// XXX: must use the provided db object, the one in the struct might be nil
// (calling List directly skips the PersistentPreRunE)

bouncers, err := db.ListBouncers()
if err != nil {
return fmt.Errorf("unable to list bouncers: %w", err)
}
Expand Down Expand Up @@ -194,7 +200,7 @@ func (cli *cliBouncers) newListCmd() *cobra.Command {
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.list(color.Output)
return cli.List(color.Output, cli.db)
},
}

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

// Custom types for flag validation and conversion.

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

import (
"encoding/csv"
Expand Down Expand Up @@ -49,12 +49,14 @@ func getLastHeartbeat(m *ent.Machine) (string, bool) {
return hb, true
}

type configGetter = func() *csconfig.Config

type cliMachines struct {
db *database.Client
cfg configGetter
}

func NewCLIMachines(cfg configGetter) *cliMachines {
func New(cfg configGetter) *cliMachines {
return &cliMachines{
cfg: cfg,
}
Expand Down Expand Up @@ -208,8 +210,11 @@ func (cli *cliMachines) listCSV(out io.Writer, machines ent.Machines) error {
return nil
}

func (cli *cliMachines) list(out io.Writer) error {
machines, err := cli.db.ListMachines()
func (cli *cliMachines) List(out io.Writer, db *database.Client) error {
// XXX: must use the provided db object, the one in the struct might be nil
// (calling List directly skips the PersistentPreRunE)

machines, err := db.ListMachines()
if err != nil {
return fmt.Errorf("unable to list machines: %w", err)
}
Expand Down Expand Up @@ -247,7 +252,7 @@ func (cli *cliMachines) newListCmd() *cobra.Command {
Args: cobra.NoArgs,
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.list(color.Output)
return cli.List(color.Output, cli.db)
},
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (

"github.com/crowdsecurity/go-cs-lib/trace"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clibouncer"
"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/climachine"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/climetrics"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clinotifications"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clipapi"
Expand Down Expand Up @@ -256,8 +258,8 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
cmd.AddCommand(NewCLIDecisions(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIAlerts(cli.cfg).NewCommand())
cmd.AddCommand(clisimulation.New(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIBouncers(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIMachines(cli.cfg).NewCommand())
cmd.AddCommand(clibouncer.New(cli.cfg).NewCommand())
cmd.AddCommand(climachine.New(cli.cfg).NewCommand())
cmd.AddCommand(clicapi.New(cli.cfg).NewCommand())
cmd.AddCommand(clilapi.New(cli.cfg).NewCommand())
cmd.AddCommand(NewCompletionCmd())
Expand Down
10 changes: 6 additions & 4 deletions cmd/crowdsec-cli/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (

"github.com/crowdsecurity/go-cs-lib/trace"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clibouncer"
"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/climachine"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/climetrics"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
Expand Down Expand Up @@ -193,9 +195,9 @@ func (cli *cliSupport) dumpBouncers(zw *zip.Writer, db *database.Client) error {
}

out := new(bytes.Buffer)
cm := cliBouncers{db: db, cfg: cli.cfg}
cm := clibouncer.New(cli.cfg)

if err := cm.list(out); err != nil {
if err := cm.List(out, db); err != nil {
return err
}

Expand All @@ -214,9 +216,9 @@ func (cli *cliSupport) dumpAgents(zw *zip.Writer, db *database.Client) error {
}

out := new(bytes.Buffer)
cm := cliMachines{db: db, cfg: cli.cfg}
cm := climachine.New(cli.cfg)

if err := cm.list(out); err != nil {
if err := cm.List(out, db); err != nil {
return err
}

Expand Down

0 comments on commit e02b0dc

Please sign in to comment.