diff --git a/cmd/crowdsec-cli/hubtest.go b/cmd/crowdsec-cli/hubtest.go index 2a4635d39f1..744c9f12594 100644 --- a/cmd/crowdsec-cli/hubtest.go +++ b/cmd/crowdsec-cli/hubtest.go @@ -85,7 +85,7 @@ func (cli *cliHubTest) NewCommand() *cobra.Command { cmd.AddCommand(cli.NewListCmd()) cmd.AddCommand(cli.NewCoverageCmd()) cmd.AddCommand(cli.NewEvalCmd()) - cmd.AddCommand(cli.NewExplainCmd()) + cmd.AddCommand(NewCLiHubtestExplain(cli.cfg).NewCommand()) return cmd } @@ -234,7 +234,6 @@ cscli hubtest create my-scenario-test --parsers crowdsecurity/nginx --scenarios return cmd } - func (cli *cliHubTest) run(runAll bool, NucleiTargetHost string, AppSecHost string, args []string) error { cfg := cli.cfg() @@ -271,7 +270,6 @@ func (cli *cliHubTest) run(runAll bool, NucleiTargetHost string, AppSecHost stri return nil } - func (cli *cliHubTest) NewRunCmd() *cobra.Command { var ( noClean bool @@ -701,46 +699,74 @@ func (cli *cliHubTest) NewEvalCmd() *cobra.Command { return cmd } -func (cli *cliHubTest) NewExplainCmd() *cobra.Command { +type cliHubtestExplain struct { + cfg configGetter + flags struct { + details bool + skipOk bool + } +} + +func NewCLiHubtestExplain(cfg configGetter) *cliHubtestExplain { + return &cliHubtestExplain{ + cfg: cfg, + } +} + +func (cli *cliHubtestExplain) NewCommand() *cobra.Command { cmd := &cobra.Command{ Use: "explain", Short: "explain [test_name]", Args: cobra.ExactArgs(1), DisableAutoGenTag: true, RunE: func(_ *cobra.Command, args []string) error { - for _, testName := range args { - test, err := HubTest.LoadTestItem(testName) - if err != nil { - return fmt.Errorf("can't load test: %+v", err) - } - err = test.ParserAssert.LoadTest(test.ParserResultFile) - if err != nil { - if err = test.Run(); err != nil { - return fmt.Errorf("running test '%s' failed: %+v", test.Name, err) - } + return cli.run(args) + }, + } - if err = test.ParserAssert.LoadTest(test.ParserResultFile); err != nil { - return fmt.Errorf("unable to load parser result after run: %w", err) - } - } + flags := cmd.Flags() - err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile) - if err != nil { - if err = test.Run(); err != nil { - return fmt.Errorf("running test '%s' failed: %+v", test.Name, err) - } + flags.BoolVarP(&cli.flags.details, "verbose", "v", false, "Display individual changes") + flags.BoolVar(&cli.flags.skipOk, "failures", false, "Only show failed lines") - if err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile); err != nil { - return fmt.Errorf("unable to load scenario result after run: %w", err) - } - } - opts := dumps.DumpOpts{} - dumps.DumpTree(*test.ParserAssert.TestData, *test.ScenarioAssert.PourData, opts) + return cmd +} + +func (cli *cliHubtestExplain) run(args []string) error { + for _, testName := range args { + test, err := HubTest.LoadTestItem(testName) + if err != nil { + return fmt.Errorf("can't load test: %+v", err) + } + err = test.ParserAssert.LoadTest(test.ParserResultFile) + if err != nil { + if err = test.Run(); err != nil { + return fmt.Errorf("running test '%s' failed: %+v", test.Name, err) } - return nil - }, + if err = test.ParserAssert.LoadTest(test.ParserResultFile); err != nil { + return fmt.Errorf("unable to load parser result after run: %w", err) + } + } + + err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile) + if err != nil { + if err = test.Run(); err != nil { + return fmt.Errorf("running test '%s' failed: %+v", test.Name, err) + } + + if err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile); err != nil { + return fmt.Errorf("unable to load scenario result after run: %w", err) + } + } + + opts := dumps.DumpOpts{ + Details: cli.flags.details, + SkipOk: cli.flags.skipOk, + } + + dumps.DumpTree(*test.ParserAssert.TestData, *test.ScenarioAssert.PourData, opts) } - return cmd + return nil }