瀏覽代碼

AddContext() -> cli.addContext()

marco 1 年之前
父節點
當前提交
357a5fee03
共有 1 個文件被更改,包括 9 次插入8 次删除
  1. 9 8
      cmd/crowdsec-cli/lapi.go

+ 9 - 8
cmd/crowdsec-cli/lapi.go

@@ -221,24 +221,25 @@ func (cli *cliLapi) NewCommand() *cobra.Command {
 	return cmd
 	return cmd
 }
 }
 
 
-func AddContext(key string, values []string) error {
+func (cli *cliLapi) addContext(key string, values []string) error {
+	cfg := cli.cfg()
 	if err := alertcontext.ValidateContextExpr(key, values); err != nil {
 	if err := alertcontext.ValidateContextExpr(key, values); err != nil {
 		return fmt.Errorf("invalid context configuration :%s", err)
 		return fmt.Errorf("invalid context configuration :%s", err)
 	}
 	}
-	if _, ok := csConfig.Crowdsec.ContextToSend[key]; !ok {
-		csConfig.Crowdsec.ContextToSend[key] = make([]string, 0)
+	if _, ok := cfg.Crowdsec.ContextToSend[key]; !ok {
+		cfg.Crowdsec.ContextToSend[key] = make([]string, 0)
 
 
 		log.Infof("key '%s' added", key)
 		log.Infof("key '%s' added", key)
 	}
 	}
-	data := csConfig.Crowdsec.ContextToSend[key]
+	data := cfg.Crowdsec.ContextToSend[key]
 	for _, val := range values {
 	for _, val := range values {
 		if !slices.Contains(data, val) {
 		if !slices.Contains(data, val) {
 			log.Infof("value '%s' added to key '%s'", val, key)
 			log.Infof("value '%s' added to key '%s'", val, key)
 			data = append(data, val)
 			data = append(data, val)
 		}
 		}
-		csConfig.Crowdsec.ContextToSend[key] = data
+		cfg.Crowdsec.ContextToSend[key] = data
 	}
 	}
-	if err := csConfig.Crowdsec.DumpContextConfigFile(); err != nil {
+	if err := cfg.Crowdsec.DumpContextConfigFile(); err != nil {
 		return err
 		return err
 	}
 	}
 
 
@@ -267,7 +268,7 @@ cscli lapi context add --value evt.Meta.source_ip --value evt.Meta.target_user
 			}
 			}
 
 
 			if keyToAdd != "" {
 			if keyToAdd != "" {
-				if err := AddContext(keyToAdd, valuesToAdd); err != nil {
+				if err := cli.addContext(keyToAdd, valuesToAdd); err != nil {
 					return err
 					return err
 				}
 				}
 				return nil
 				return nil
@@ -277,7 +278,7 @@ cscli lapi context add --value evt.Meta.source_ip --value evt.Meta.target_user
 				keySlice := strings.Split(v, ".")
 				keySlice := strings.Split(v, ".")
 				key := keySlice[len(keySlice)-1]
 				key := keySlice[len(keySlice)-1]
 				value := []string{v}
 				value := []string{v}
-				if err := AddContext(key, value); err != nil {
+				if err := cli.addContext(key, value); err != nil {
 					return err
 					return err
 				}
 				}
 			}
 			}