diff --git a/cmd/crowdsec-cli/collections.go b/cmd/crowdsec-cli/collections.go index 9a6547337..7e38b6ad9 100644 --- a/cmd/crowdsec-cli/collections.go +++ b/cmd/crowdsec-cli/collections.go @@ -45,6 +45,7 @@ func NewCollectionsCmd() *cobra.Command { }, } + var ignoreError bool var cmdCollectionsInstall = &cobra.Command{ Use: "install collection", Short: "Install given collection(s)", @@ -54,12 +55,19 @@ func NewCollectionsCmd() *cobra.Command { DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { for _, name := range args { - InstallItem(name, cwhub.COLLECTIONS, forceAction) + if err := InstallItem(name, cwhub.COLLECTIONS, forceAction); err != nil { + if ignoreError { + log.Errorf("Error while installing '%s': %s", name, err) + } else { + log.Fatalf("Error while installing '%s': %s", name, err) + } + } } }, } cmdCollectionsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable") cmdCollectionsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files") + cmdCollectionsInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple collections") cmdCollections.AddCommand(cmdCollectionsInstall) var cmdCollectionsRemove = &cobra.Command{ diff --git a/cmd/crowdsec-cli/parsers.go b/cmd/crowdsec-cli/parsers.go index 7d2181d5f..2308313c0 100644 --- a/cmd/crowdsec-cli/parsers.go +++ b/cmd/crowdsec-cli/parsers.go @@ -48,6 +48,7 @@ cscli parsers remove crowdsecurity/sshd-logs }, } + var ignoreError bool var cmdParsersInstall = &cobra.Command{ Use: "install [config]", Short: "Install given parser(s)", @@ -57,12 +58,19 @@ cscli parsers remove crowdsecurity/sshd-logs DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { for _, name := range args { - InstallItem(name, cwhub.PARSERS, forceAction) + if err := InstallItem(name, cwhub.PARSERS, forceAction); err != nil { + if ignoreError { + log.Errorf("Error while installing '%s': %s", name, err) + } else { + log.Fatalf("Error while installing '%s': %s", name, err) + } + } } }, } cmdParsersInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable") cmdParsersInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files") + cmdParsersInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple parsers") cmdParsers.AddCommand(cmdParsersInstall) var cmdParsersRemove = &cobra.Command{ diff --git a/cmd/crowdsec-cli/postoverflows.go b/cmd/crowdsec-cli/postoverflows.go index 30a9c85e8..d0ec4aee0 100644 --- a/cmd/crowdsec-cli/postoverflows.go +++ b/cmd/crowdsec-cli/postoverflows.go @@ -47,6 +47,7 @@ func NewPostOverflowsCmd() *cobra.Command { }, } + var ignoreError bool var cmdPostOverflowsInstall = &cobra.Command{ Use: "install [config]", Short: "Install given postoverflow(s)", @@ -56,12 +57,19 @@ func NewPostOverflowsCmd() *cobra.Command { DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { for _, name := range args { - InstallItem(name, cwhub.PARSERS_OVFLW, forceAction) + if err := InstallItem(name, cwhub.PARSERS_OVFLW, forceAction); err != nil { + if ignoreError { + log.Errorf("Error while installing '%s': %s", name, err) + } else { + log.Fatalf("Error while installing '%s': %s", name, err) + } + } } }, } cmdPostOverflowsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable") cmdPostOverflowsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files") + cmdPostOverflowsInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple postoverflows") cmdPostOverflows.AddCommand(cmdPostOverflowsInstall) var cmdPostOverflowsRemove = &cobra.Command{ diff --git a/cmd/crowdsec-cli/scenarios.go b/cmd/crowdsec-cli/scenarios.go index d29e6ae80..9f52334ed 100644 --- a/cmd/crowdsec-cli/scenarios.go +++ b/cmd/crowdsec-cli/scenarios.go @@ -48,6 +48,7 @@ cscli scenarios remove crowdsecurity/ssh-bf }, } + var ignoreError bool var cmdScenariosInstall = &cobra.Command{ Use: "install [config]", Short: "Install given scenario(s)", @@ -57,12 +58,19 @@ cscli scenarios remove crowdsecurity/ssh-bf DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { for _, name := range args { - InstallItem(name, cwhub.SCENARIOS, forceAction) + if err := InstallItem(name, cwhub.SCENARIOS, forceAction); err != nil { + if ignoreError { + log.Errorf("Error while installing '%s': %s", name, err) + } else { + log.Fatalf("Error while installing '%s': %s", name, err) + } + } } }, } cmdScenariosInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable") cmdScenariosInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files") + cmdScenariosInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple scenarios") cmdScenarios.AddCommand(cmdScenariosInstall) var cmdScenariosRemove = &cobra.Command{ diff --git a/cmd/crowdsec-cli/utils.go b/cmd/crowdsec-cli/utils.go index dd0cd6588..a01fd0ef4 100644 --- a/cmd/crowdsec-cli/utils.go +++ b/cmd/crowdsec-cli/utils.go @@ -178,33 +178,35 @@ func ListItems(itemTypes []string, args []string, showType bool, showHeader bool } } -func InstallItem(name string, obtype string, force bool) { +func InstallItem(name string, obtype string, force bool) error { it := cwhub.GetItem(obtype, name) if it == nil { - log.Fatalf("unable to retrieve item : %s", name) + return fmt.Errorf("unable to retrieve item : %s", name) } item := *it if downloadOnly && item.Downloaded && item.UpToDate { log.Warningf("%s is already downloaded and up-to-date", item.Name) if !force { - return + return nil } } item, err := cwhub.DownloadLatest(csConfig.Hub, item, force, false) if err != nil { - log.Fatalf("error while downloading %s : %v", item.Name, err) + return fmt.Errorf("error while downloading %s : %v", item.Name, err) } cwhub.AddItem(obtype, item) if downloadOnly { log.Infof("Downloaded %s to %s", item.Name, csConfig.Hub.HubDir+"/"+item.RemotePath) - return + return nil } item, err = cwhub.EnableItem(csConfig.Hub, item) if err != nil { - log.Fatalf("error while enabling %s : %v.", item.Name, err) + return fmt.Errorf("error while enabling %s : %v.", item.Name, err) } cwhub.AddItem(obtype, item) log.Infof("Enabled %s", item.Name) + + return nil } func RemoveMany(itemType string, name string) {