소스 검색

allow deleting multiple machines (#930)

* allow deleting multiple machines

* allow multiple bouncers deletion

Co-authored-by: AlteredCoder <AlteredCoder>
AlteredCoder 3 년 전
부모
커밋
4aca9941cb
2개의 변경된 파일17개의 추가작업 그리고 17개의 파일을 삭제
  1. 9 11
      cmd/crowdsec-cli/bouncers.go
  2. 8 6
      cmd/crowdsec-cli/machines.go

+ 9 - 11
cmd/crowdsec-cli/bouncers.go

@@ -141,18 +141,16 @@ cscli bouncers add MyBouncerName -l 24`,
 	var cmdBouncersDelete = &cobra.Command{
 		Use:               "delete MyBouncerName",
 		Short:             "delete bouncer",
-		Args:              cobra.ExactArgs(1),
+		Args:              cobra.MinimumNArgs(1),
 		DisableAutoGenTag: true,
-		Run: func(cmd *cobra.Command, arg []string) {
-			keyName := arg[0]
-			if keyName == "" {
-				log.Errorf("Please provide a bouncer name")
-				return
-			}
-			err := dbClient.DeleteBouncer(keyName)
-			if err != nil {
-				log.Errorf("unable to delete bouncer: %s", err)
-				return
+		Run: func(cmd *cobra.Command, args []string) {
+			for _, bouncerID := range args {
+				err := dbClient.DeleteBouncer(bouncerID)
+				if err != nil {
+					log.Errorf("unable to delete bouncer: %s", err)
+					return
+				}
+				log.Infof("bouncer '%s' deleted successfully", bouncerID)
 			}
 		},
 	}

+ 8 - 6
cmd/crowdsec-cli/machines.go

@@ -266,7 +266,7 @@ cscli machines add MyTestMachine --password MyPassword
 		Use:               "delete --machine MyTestMachine",
 		Short:             "delete machines",
 		Example:           `cscli machines delete "machine_name"`,
-		Args:              cobra.ExactArgs(1),
+		Args:              cobra.MinimumNArgs(1),
 		DisableAutoGenTag: true,
 		PreRun: func(cmd *cobra.Command, args []string) {
 			var err error
@@ -277,12 +277,14 @@ cscli machines add MyTestMachine --password MyPassword
 		},
 		Run: func(cmd *cobra.Command, args []string) {
 			machineID = args[0]
-			err := dbClient.DeleteWatcher(machineID)
-			if err != nil {
-				log.Errorf("unable to delete machine: %s", err)
-				return
+			for _, machineID := range args {
+				err := dbClient.DeleteWatcher(machineID)
+				if err != nil {
+					log.Errorf("unable to delete machine: %s", err)
+					return
+				}
+				log.Infof("machine '%s' deleted successfully", machineID)
 			}
-			log.Infof("machine '%s' deleted successfully", machineID)
 		},
 	}
 	cmdMachinesDelete.Flags().StringVarP(&machineID, "machine", "m", "", "machine to delete")