mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-18 15:24:25 +02:00
* refact "cscli decisions import" * cobra.ExactArgs(0) -> cobra.NoArgs * refact cscli bouncers * refact cscli machines * refact "cscli lapi" * lint
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package clibouncer
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
|
)
|
|
|
|
func (cli *cliBouncers) delete(ctx context.Context, bouncers []string, ignoreMissing bool) error {
|
|
for _, bouncerID := range bouncers {
|
|
if err := cli.db.DeleteBouncer(ctx, bouncerID); err != nil {
|
|
var notFoundErr *database.BouncerNotFoundError
|
|
if ignoreMissing && errors.As(err, ¬FoundErr) {
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("unable to delete bouncer: %w", err)
|
|
}
|
|
|
|
log.Infof("bouncer '%s' deleted successfully", bouncerID)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (cli *cliBouncers) newDeleteCmd() *cobra.Command {
|
|
var ignoreMissing bool
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "delete MyBouncerName",
|
|
Short: "delete bouncer(s) from the database",
|
|
Example: `cscli bouncers delete "bouncer1" "bouncer2"`,
|
|
Args: cobra.MinimumNArgs(1),
|
|
Aliases: []string{"remove"},
|
|
DisableAutoGenTag: true,
|
|
ValidArgsFunction: cli.validBouncerID,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return cli.delete(cmd.Context(), args, ignoreMissing)
|
|
},
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
flags.BoolVar(&ignoreMissing, "ignore-missing", false, "don't print errors if one or more bouncers don't exist")
|
|
|
|
return cmd
|
|
}
|