Skip to content

Commit

Permalink
cscli refact: package 'cliexplain' (#3151)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Aug 21, 2024
1 parent 3d27e83 commit 429418f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cliexplain

import (
"bufio"
Expand All @@ -12,6 +12,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/dumps"
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
)
Expand Down Expand Up @@ -40,9 +41,12 @@ func getLineCountForFile(filepath string) (int, error) {
return lc, nil
}

type configGetter func() *csconfig.Config

type cliExplain struct {
cfg configGetter
flags struct {
cfg configGetter
configFilePath string
flags struct {
logFile string
dsn string
logLine string
Expand All @@ -56,9 +60,10 @@ type cliExplain struct {
}
}

func NewCLIExplain(cfg configGetter) *cliExplain {
func New(cfg configGetter, configFilePath string) *cliExplain {
return &cliExplain{
cfg: cfg,
cfg: cfg,
configFilePath: configFilePath,
}
}

Expand Down Expand Up @@ -103,7 +108,7 @@ tail -n 5 myfile.log | cscli explain --type nginx -f -
flags.StringVar(&cli.flags.crowdsec, "crowdsec", "crowdsec", "Path to crowdsec")
flags.BoolVar(&cli.flags.noClean, "no-clean", false, "Don't clean runtime environment after tests")

cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("type")
cmd.MarkFlagsOneRequired("log", "file", "dsn")

return cmd
Expand Down Expand Up @@ -214,7 +219,7 @@ func (cli *cliExplain) run() error {
return errors.New("no acquisition (--file or --dsn) provided, can't run cscli test")
}

cmdArgs := []string{"-c", ConfigFilePath, "-type", logType, "-dsn", dsn, "-dump-data", dir, "-no-api"}
cmdArgs := []string{"-c", cli.configFilePath, "-type", logType, "-dsn", dsn, "-dump-data", dir, "-no-api"}

if labels != "" {
log.Debugf("adding labels %s", labels)
Expand Down
39 changes: 20 additions & 19 deletions cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/crowdsecurity/go-cs-lib/trace"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliconsole"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliexplain"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/climetrics"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/fflag"
Expand Down Expand Up @@ -152,14 +153,6 @@ func (cli *cliRoot) initialize() error {
return nil
}

// list of valid subcommands for the shell completion
var validArgs = []string{
"alerts", "appsec-configs", "appsec-rules", "bouncers", "capi", "collections",
"completion", "config", "console", "contexts", "dashboard", "decisions", "explain",
"hub", "hubtest", "lapi", "machines", "metrics", "notifications", "parsers",
"postoverflows", "scenarios", "simulation", "support", "version",
}

func (cli *cliRoot) colorize(cmd *cobra.Command) {
cc.Init(&cc.Config{
RootCmd: cmd,
Expand Down Expand Up @@ -191,6 +184,14 @@ func (cli *cliRoot) NewCommand() (*cobra.Command, error) {
return nil, fmt.Errorf("failed to set feature flags from env: %w", err)
}

// list of valid subcommands for the shell completion
validArgs := []string{
"alerts", "appsec-configs", "appsec-rules", "bouncers", "capi", "collections",
"completion", "config", "console", "contexts", "dashboard", "decisions", "explain",
"hub", "hubtest", "lapi", "machines", "metrics", "notifications", "parsers",
"postoverflows", "scenarios", "simulation", "support", "version",
}

cmd := &cobra.Command{
Use: "cscli",
Short: "cscli allows you to manage crowdsec",
Expand Down Expand Up @@ -238,16 +239,6 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
return nil, err
}

if len(os.Args) > 1 {
cobra.OnInitialize(
func() {
if err := cli.initialize(); err != nil {
log.Fatal(err)
}
},
)
}

cmd.AddCommand(NewCLIDoc().NewCommand(cmd))
cmd.AddCommand(NewCLIVersion().NewCommand())
cmd.AddCommand(NewCLIConfig(cli.cfg).NewCommand())
Expand All @@ -263,7 +254,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
cmd.AddCommand(NewCLILapi(cli.cfg).NewCommand())
cmd.AddCommand(NewCompletionCmd())
cmd.AddCommand(cliconsole.New(cli.cfg, reloadMessage).NewCommand())
cmd.AddCommand(NewCLIExplain(cli.cfg).NewCommand())
cmd.AddCommand(cliexplain.New(cli.cfg, ConfigFilePath).NewCommand())
cmd.AddCommand(NewCLIHubTest(cli.cfg).NewCommand())
cmd.AddCommand(NewCLINotifications(cli.cfg).NewCommand())
cmd.AddCommand(NewCLISupport(cli.cfg).NewCommand())
Expand All @@ -280,6 +271,16 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
cmd.AddCommand(NewCLISetup(cli.cfg).NewCommand())
}

if len(os.Args) > 1 {
cobra.OnInitialize(
func() {
if err := cli.initialize(); err != nil {
log.Fatal(err)
}
},
)
}

return cmd, nil
}

Expand Down

0 comments on commit 429418f

Please sign in to comment.