From 702da0f59acad1deaed2f7c7668769cc3dbced1c Mon Sep 17 00:00:00 2001 From: Laurence Jones Date: Mon, 11 Sep 2023 14:18:04 +0100 Subject: [PATCH] [enhancement] cscli explain --labels (#2461) * Add label support for explain and allow user to provide multiple labels * Change my mind about empty string * Add debug and im an idiot :smile: --- cmd/crowdsec-cli/explain.go | 10 ++++++++++ cmd/crowdsec/main.go | 10 ++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/crowdsec-cli/explain.go b/cmd/crowdsec-cli/explain.go index c130259f5..d73d9b7d1 100644 --- a/cmd/crowdsec-cli/explain.go +++ b/cmd/crowdsec-cli/explain.go @@ -74,6 +74,11 @@ func runExplain(cmd *cobra.Command, args []string) error { return err } + labels, err := flags.GetString("labels") + if err != nil { + return err + } + fileInfo, _ := os.Stdin.Stat() if logType == "" || (logLine == "" && logFile == "" && dsn == "") { @@ -150,6 +155,10 @@ func runExplain(cmd *cobra.Command, args []string) error { } cmdArgs := []string{"-c", ConfigFilePath, "-type", logType, "-dsn", dsn, "-dump-data", dir, "-no-api"} + if labels != "" { + log.Debugf("adding labels %s", labels) + cmdArgs = append(cmdArgs, "-label", labels) + } crowdsecCmd := exec.Command(crowdsec, cmdArgs...) output, err := crowdsecCmd.CombinedOutput() if err != nil { @@ -209,6 +218,7 @@ tail -n 5 myfile.log | cscli explain --type nginx -f - flags.StringP("dsn", "d", "", "DSN to test") flags.StringP("log", "l", "", "Log line to test") flags.StringP("type", "t", "", "Type of the acquisition to test") + flags.String("labels", "", "Additional labels to add to the acquisition format (key:value,key2:value2)") flags.BoolP("verbose", "v", false, "Display individual changes") flags.Bool("failures", false, "Only show failed lines") flags.Bool("only-successful-parsers", false, "Only show successful parsers") diff --git a/cmd/crowdsec/main.go b/cmd/crowdsec/main.go index 273ef6fb8..c604e670a 100644 --- a/cmd/crowdsec/main.go +++ b/cmd/crowdsec/main.go @@ -138,11 +138,13 @@ func (l *labelsMap) String() string { } func (l labelsMap) Set(label string) error { - split := strings.Split(label, ":") - if len(split) != 2 { - return errors.Wrapf(errors.New("Bad Format"), "for Label '%s'", label) + for _, pair := range strings.Split(label, ",") { + split := strings.Split(pair, ":") + if len(split) != 2 { + return fmt.Errorf("invalid format for label '%s', must be key:value", pair) + } + l[split[0]] = split[1] } - l[split[0]] = split[1] return nil }