From 24797c15343f1ef254505e5123a5786f2195e1ee Mon Sep 17 00:00:00 2001 From: AlteredCoder <64792091+AlteredCoder@users.noreply.github.com> Date: Wed, 16 Mar 2022 17:26:31 +0100 Subject: [PATCH] Allow cscli remove to remove with --all (#1360) --- cmd/crowdsec-cli/collections.go | 32 +++++++++++++++++-------------- cmd/crowdsec-cli/parsers.go | 14 +++++++++----- cmd/crowdsec-cli/postoverflows.go | 14 +++++++++----- cmd/crowdsec-cli/scenarios.go | 14 +++++++++----- 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/cmd/crowdsec-cli/collections.go b/cmd/crowdsec-cli/collections.go index 0a1298d5d..9a6547337 100644 --- a/cmd/crowdsec-cli/collections.go +++ b/cmd/crowdsec-cli/collections.go @@ -67,26 +67,30 @@ func NewCollectionsCmd() *cobra.Command { Short: "Remove given collection(s)", Long: `Remove given collection(s) from hub`, Example: `cscli collections remove crowdsec/xxx crowdsec/xyz`, - Args: cobra.MinimumNArgs(1), DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { if all { RemoveMany(cwhub.COLLECTIONS, "") - } else { - for _, name := range args { - if !forceAction { - item := cwhub.GetItem(cwhub.COLLECTIONS, name) - if item == nil { - log.Fatalf("unable to retrieve: %s\n", name) - } - if len(item.BelongsToCollections) > 0 { - log.Warningf("%s belongs to other collections :\n%s\n", name, item.BelongsToCollections) - log.Printf("Run 'sudo cscli collections remove %s --force' if you want to force remove this sub collection\n", name) - continue - } + return + } + + if len(args) == 0 { + log.Fatalf("Specify at least one collection to remove or '--all' flag.") + } + + for _, name := range args { + if !forceAction { + item := cwhub.GetItem(cwhub.COLLECTIONS, name) + if item == nil { + log.Fatalf("unable to retrieve: %s\n", name) + } + if len(item.BelongsToCollections) > 0 { + log.Warningf("%s belongs to other collections :\n%s\n", name, item.BelongsToCollections) + log.Printf("Run 'sudo cscli collections remove %s --force' if you want to force remove this sub collection\n", name) + continue } - RemoveMany(cwhub.COLLECTIONS, name) } + RemoveMany(cwhub.COLLECTIONS, name) } }, } diff --git a/cmd/crowdsec-cli/parsers.go b/cmd/crowdsec-cli/parsers.go index cbf06660d..7d2181d5f 100644 --- a/cmd/crowdsec-cli/parsers.go +++ b/cmd/crowdsec-cli/parsers.go @@ -70,15 +70,19 @@ cscli parsers remove crowdsecurity/sshd-logs Short: "Remove given parser(s)", Long: `Remove given parse(s) from hub`, Example: `cscli parsers remove crowdsec/xxx crowdsec/xyz`, - Args: cobra.MinimumNArgs(1), DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { if all { RemoveMany(cwhub.PARSERS, "") - } else { - for _, name := range args { - RemoveMany(cwhub.PARSERS, name) - } + return + } + + if len(args) == 0 { + log.Fatalf("Specify at least one parser to remove or '--all' flag.") + } + + for _, name := range args { + RemoveMany(cwhub.PARSERS, name) } }, } diff --git a/cmd/crowdsec-cli/postoverflows.go b/cmd/crowdsec-cli/postoverflows.go index ceda1ad00..30a9c85e8 100644 --- a/cmd/crowdsec-cli/postoverflows.go +++ b/cmd/crowdsec-cli/postoverflows.go @@ -70,14 +70,18 @@ func NewPostOverflowsCmd() *cobra.Command { Long: `remove given postoverflow(s)`, Example: `cscli postoverflows remove crowdsec/xxx crowdsec/xyz`, DisableAutoGenTag: true, - Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { if all { RemoveMany(cwhub.PARSERS_OVFLW, "") - } else { - for _, name := range args { - RemoveMany(cwhub.PARSERS_OVFLW, name) - } + return + } + + if len(args) == 0 { + log.Fatalf("Specify at least one postoverflow to remove or '--all' flag.") + } + + for _, name := range args { + RemoveMany(cwhub.PARSERS_OVFLW, name) } }, } diff --git a/cmd/crowdsec-cli/scenarios.go b/cmd/crowdsec-cli/scenarios.go index 807376c0f..d29e6ae80 100644 --- a/cmd/crowdsec-cli/scenarios.go +++ b/cmd/crowdsec-cli/scenarios.go @@ -70,15 +70,19 @@ cscli scenarios remove crowdsecurity/ssh-bf Short: "Remove given scenario(s)", Long: `remove given scenario(s)`, Example: `cscli scenarios remove crowdsec/xxx crowdsec/xyz`, - Args: cobra.MinimumNArgs(1), DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { if all { RemoveMany(cwhub.SCENARIOS, "") - } else { - for _, name := range args { - RemoveMany(cwhub.SCENARIOS, name) - } + return + } + + if len(args) == 0 { + log.Fatalf("Specify at least one scenario to remove or '--all' flag.") + } + + for _, name := range args { + RemoveMany(cwhub.SCENARIOS, name) } }, }