浏览代码

[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:
Laurence Jones 1 年之前
父节点
当前提交
702da0f59a
共有 2 个文件被更改,包括 16 次插入4 次删除
  1. 10 0
      cmd/crowdsec-cli/explain.go
  2. 6 4
      cmd/crowdsec/main.go

+ 10 - 0
cmd/crowdsec-cli/explain.go

@@ -74,6 +74,11 @@ func runExplain(cmd *cobra.Command, args []string) error {
 		return err
 		return err
 	}
 	}
 
 
+	labels, err := flags.GetString("labels")
+	if err != nil {
+		return err
+	}
+
 	fileInfo, _ := os.Stdin.Stat()
 	fileInfo, _ := os.Stdin.Stat()
 
 
 	if logType == "" || (logLine == "" && logFile == "" && dsn == "") {
 	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"}
 	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...)
 	crowdsecCmd := exec.Command(crowdsec, cmdArgs...)
 	output, err := crowdsecCmd.CombinedOutput()
 	output, err := crowdsecCmd.CombinedOutput()
 	if err != nil {
 	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("dsn", "d", "", "DSN to test")
 	flags.StringP("log", "l", "", "Log line to test")
 	flags.StringP("log", "l", "", "Log line to test")
 	flags.StringP("type", "t", "", "Type of the acquisition 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.BoolP("verbose", "v", false, "Display individual changes")
 	flags.Bool("failures", false, "Only show failed lines")
 	flags.Bool("failures", false, "Only show failed lines")
 	flags.Bool("only-successful-parsers", false, "Only show successful parsers")
 	flags.Bool("only-successful-parsers", false, "Only show successful parsers")

+ 6 - 4
cmd/crowdsec/main.go

@@ -138,11 +138,13 @@ func (l *labelsMap) String() string {
 }
 }
 
 
 func (l labelsMap) Set(label string) error {
 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
 	return nil
 }
 }