crowdsec/cmd/crowdsec-cli/clihubtest/list.go
mmetc b12ade27f4
cscli: review/update argument number checking (#3490)
* cscsli: remove unused Command.Args setting

* cscli: review/update argument number checking

cscli will consistently print the help text if the number of arguments is
wrong for the command, but not for other types of errors.

* fix func tests

* lint
2025-03-04 12:21:27 +01:00

45 lines
939 B
Go

package clihubtest
import (
"encoding/json"
"errors"
"fmt"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/args"
)
func (cli *cliHubTest) newListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list",
Args: args.NoArgs,
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
cfg := cli.cfg()
if err := hubPtr.LoadAllTests(); err != nil {
return fmt.Errorf("unable to load all tests: %w", err)
}
switch cfg.Cscli.Output {
case "human":
hubTestListTable(color.Output, cfg.Cscli.Color, hubPtr.Tests)
case "json":
j, err := json.MarshalIndent(hubPtr.Tests, " ", " ")
if err != nil {
return err
}
fmt.Println(string(j))
default:
return errors.New("only human/json output modes are supported")
}
return nil
},
}
return cmd
}