cscli refact: package 'cliexplain' (#3151)

This commit is contained in:
mmetc 2024-08-21 10:24:18 +02:00 committed by GitHub
parent 3d27e83bf5
commit 429418ffc6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 26 deletions

View file

@ -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)

View file

@ -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
}