From 429418ffc6f1ef1bbae7c9f73c36abe4848c7151 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Wed, 21 Aug 2024 10:24:18 +0200 Subject: [PATCH] cscli refact: package 'cliexplain' (#3151) --- cmd/crowdsec-cli/{ => cliexplain}/explain.go | 19 ++++++---- cmd/crowdsec-cli/main.go | 39 ++++++++++---------- 2 files changed, 32 insertions(+), 26 deletions(-) rename cmd/crowdsec-cli/{ => cliexplain}/explain.go (93%) diff --git a/cmd/crowdsec-cli/explain.go b/cmd/crowdsec-cli/cliexplain/explain.go similarity index 93% rename from cmd/crowdsec-cli/explain.go rename to cmd/crowdsec-cli/cliexplain/explain.go index c322cce47fe..182e34a12a5 100644 --- a/cmd/crowdsec-cli/explain.go +++ b/cmd/crowdsec-cli/cliexplain/explain.go @@ -1,4 +1,4 @@ -package main +package cliexplain import ( "bufio" @@ -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" ) @@ -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 @@ -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, } } @@ -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 @@ -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) diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index da955923962..49140b160ad 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -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" @@ -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, @@ -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", @@ -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()) @@ -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()) @@ -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 }