|
@@ -12,11 +12,15 @@ import (
|
|
)
|
|
)
|
|
|
|
|
|
func NewCollectionsCmd() *cobra.Command {
|
|
func NewCollectionsCmd() *cobra.Command {
|
|
- var cmdCollections = &cobra.Command{
|
|
|
|
|
|
+ cmdCollections := &cobra.Command{
|
|
Use: "collections [action]",
|
|
Use: "collections [action]",
|
|
- Short: "Manage collections from hub",
|
|
|
|
- Long: `Install/Remove/Upgrade/Inspect collections from the CrowdSec Hub.`,
|
|
|
|
- /*TBD fix help*/
|
|
|
|
|
|
+ Short: "Install/Remove/Upgrade/Inspect collections from the CrowdSec Hub.",
|
|
|
|
+ Example: `cscli collections install crowdsec/xxx crowdsec/xyz
|
|
|
|
+cscli collections inspect crowdsec/xxx crowdsec/xyz
|
|
|
|
+cscli collections upgrade crowdsec/xxx crowdsec/xyz
|
|
|
|
+cscli collections list
|
|
|
|
+cscli collections remove crowdsec/xxx crowdsec/xyz
|
|
|
|
+`,
|
|
Args: cobra.MinimumNArgs(1),
|
|
Args: cobra.MinimumNArgs(1),
|
|
Aliases: []string{"collection"},
|
|
Aliases: []string{"collection"},
|
|
DisableAutoGenTag: true,
|
|
DisableAutoGenTag: true,
|
|
@@ -35,42 +39,130 @@ func NewCollectionsCmd() *cobra.Command {
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
|
|
- var ignoreError bool
|
|
|
|
|
|
+ cmdCollections.AddCommand(NewCollectionsInstallCmd())
|
|
|
|
+ cmdCollections.AddCommand(NewCollectionsRemoveCmd())
|
|
|
|
+ cmdCollections.AddCommand(NewCollectionsUpgradeCmd())
|
|
|
|
+ cmdCollections.AddCommand(NewCollectionsInspectCmd())
|
|
|
|
+ cmdCollections.AddCommand(NewCollectionsListCmd())
|
|
|
|
|
|
- var cmdCollectionsInstall = &cobra.Command{
|
|
|
|
- Use: "install collection",
|
|
|
|
- Short: "Install given collection(s)",
|
|
|
|
- Long: `Fetch and install given collection(s) from hub`,
|
|
|
|
- Example: `cscli collections install crowdsec/xxx crowdsec/xyz`,
|
|
|
|
- Args: cobra.MinimumNArgs(1),
|
|
|
|
|
|
+ return cmdCollections
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func runCollectionsInstall(cmd *cobra.Command, args []string) error {
|
|
|
|
+ flags := cmd.Flags()
|
|
|
|
+
|
|
|
|
+ downloadOnly, err := flags.GetBool("download-only")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ force, err := flags.GetBool("force")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ignoreError, err := flags.GetBool("ignore")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, name := range args {
|
|
|
|
+ t := cwhub.GetItem(cwhub.COLLECTIONS, name)
|
|
|
|
+ if t == nil {
|
|
|
|
+ nearestItem, score := GetDistance(cwhub.COLLECTIONS, name)
|
|
|
|
+ Suggest(cwhub.COLLECTIONS, name, nearestItem.Name, score, ignoreError)
|
|
|
|
+
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if err := cwhub.InstallItem(csConfig, name, cwhub.COLLECTIONS, force, downloadOnly); err != nil {
|
|
|
|
+ if !ignoreError {
|
|
|
|
+ return fmt.Errorf("error while installing '%s': %w", name, err)
|
|
|
|
+ }
|
|
|
|
+ log.Errorf("Error while installing '%s': %s", name, err)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func NewCollectionsInstallCmd() *cobra.Command {
|
|
|
|
+ cmdCollectionsInstall := &cobra.Command{
|
|
|
|
+ Use: "install collection",
|
|
|
|
+ Short: "Install given collection(s)",
|
|
|
|
+ Long: `Fetch and install given collection(s) from hub`,
|
|
|
|
+ Example: `cscli collections install crowdsec/xxx crowdsec/xyz`,
|
|
|
|
+ Args: cobra.MinimumNArgs(1),
|
|
|
|
+ DisableAutoGenTag: true,
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
return compAllItems(cwhub.COLLECTIONS, args, toComplete)
|
|
return compAllItems(cwhub.COLLECTIONS, args, toComplete)
|
|
},
|
|
},
|
|
- DisableAutoGenTag: true,
|
|
|
|
- RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
- for _, name := range args {
|
|
|
|
- t := cwhub.GetItem(cwhub.COLLECTIONS, name)
|
|
|
|
- if t == nil {
|
|
|
|
- nearestItem, score := GetDistance(cwhub.COLLECTIONS, name)
|
|
|
|
- Suggest(cwhub.COLLECTIONS, name, nearestItem.Name, score, ignoreError)
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- if err := cwhub.InstallItem(csConfig, name, cwhub.COLLECTIONS, forceAction, downloadOnly); err != nil {
|
|
|
|
- if !ignoreError {
|
|
|
|
- return fmt.Errorf("error while installing '%s': %w", name, err)
|
|
|
|
- }
|
|
|
|
- log.Errorf("Error while installing '%s': %s", name, err)
|
|
|
|
- }
|
|
|
|
|
|
+ RunE: runCollectionsInstall,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ flags := cmdCollectionsInstall.Flags()
|
|
|
|
+ flags.BoolP("download-only", "d", false, "Only download packages, don't enable")
|
|
|
|
+ flags.Bool("force", false, "Force install : Overwrite tainted and outdated files")
|
|
|
|
+ flags.Bool("ignore", false, "Ignore errors when installing multiple collections")
|
|
|
|
+
|
|
|
|
+ return cmdCollectionsInstall
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func runCollectionsRemove(cmd *cobra.Command, args []string) error {
|
|
|
|
+ flags := cmd.Flags()
|
|
|
|
+
|
|
|
|
+ purge, err := flags.GetBool("purge")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ force, err := flags.GetBool("force")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ all, err := flags.GetBool("all")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if all {
|
|
|
|
+ err := cwhub.RemoveMany(csConfig, cwhub.COLLECTIONS, "", all, purge, force)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(args) == 0 {
|
|
|
|
+ return fmt.Errorf("specify at least one collection to remove or '--all'")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, name := range args {
|
|
|
|
+ if !force {
|
|
|
|
+ item := cwhub.GetItem(cwhub.COLLECTIONS, name)
|
|
|
|
+ if item == nil {
|
|
|
|
+ return fmt.Errorf("unable to retrieve: %s", name)
|
|
}
|
|
}
|
|
- return nil
|
|
|
|
- },
|
|
|
|
|
|
+ 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
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ err := cwhub.RemoveMany(csConfig, cwhub.COLLECTIONS, name, all, purge, force)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return 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{
|
|
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func NewCollectionsRemoveCmd() *cobra.Command {
|
|
|
|
+ cmdCollectionsRemove := &cobra.Command{
|
|
Use: "remove collection",
|
|
Use: "remove collection",
|
|
Short: "Remove given collection(s)",
|
|
Short: "Remove given collection(s)",
|
|
Long: `Remove given collection(s) from hub`,
|
|
Long: `Remove given collection(s) from hub`,
|
|
@@ -80,39 +172,48 @@ func NewCollectionsCmd() *cobra.Command {
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
return compInstalledItems(cwhub.COLLECTIONS, args, toComplete)
|
|
return compInstalledItems(cwhub.COLLECTIONS, args, toComplete)
|
|
},
|
|
},
|
|
- RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
- if all {
|
|
|
|
- cwhub.RemoveMany(csConfig, cwhub.COLLECTIONS, "", all, purge, forceAction)
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
|
|
+ RunE: runCollectionsRemove,
|
|
|
|
+ }
|
|
|
|
|
|
- if len(args) == 0 {
|
|
|
|
- return fmt.Errorf("specify at least one collection to remove or '--all'")
|
|
|
|
- }
|
|
|
|
|
|
+ flags := cmdCollectionsRemove.Flags()
|
|
|
|
+ flags.Bool("purge", false, "Delete source file too")
|
|
|
|
+ flags.Bool("force", false, "Force remove : Remove tainted and outdated files")
|
|
|
|
+ flags.Bool("all", false, "Delete all the collections")
|
|
|
|
|
|
- for _, name := range args {
|
|
|
|
- if !forceAction {
|
|
|
|
- item := cwhub.GetItem(cwhub.COLLECTIONS, name)
|
|
|
|
- if item == nil {
|
|
|
|
- return fmt.Errorf("unable to retrieve: %s", 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
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- cwhub.RemoveMany(csConfig, cwhub.COLLECTIONS, name, all, purge, forceAction)
|
|
|
|
- }
|
|
|
|
- return nil
|
|
|
|
- },
|
|
|
|
|
|
+ return cmdCollectionsRemove
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func runCollectionsUpgrade(cmd *cobra.Command, args []string) error {
|
|
|
|
+ flags := cmd.Flags()
|
|
|
|
+
|
|
|
|
+ force, err := flags.GetBool("force")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ all, err := flags.GetBool("all")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if all {
|
|
|
|
+ cwhub.UpgradeConfig(csConfig, cwhub.COLLECTIONS, "", force)
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
- cmdCollectionsRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
|
|
|
|
- cmdCollectionsRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
|
|
|
|
- cmdCollectionsRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the collections")
|
|
|
|
- cmdCollections.AddCommand(cmdCollectionsRemove)
|
|
|
|
|
|
|
|
- var cmdCollectionsUpgrade = &cobra.Command{
|
|
|
|
|
|
+ if len(args) == 0 {
|
|
|
|
+ return fmt.Errorf("specify at least one collection to upgrade or '--all'")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, name := range args {
|
|
|
|
+ cwhub.UpgradeConfig(csConfig, cwhub.COLLECTIONS, name, force)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func NewCollectionsUpgradeCmd() *cobra.Command {
|
|
|
|
+ cmdCollectionsUpgrade := &cobra.Command{
|
|
Use: "upgrade collection",
|
|
Use: "upgrade collection",
|
|
Short: "Upgrade given collection(s)",
|
|
Short: "Upgrade given collection(s)",
|
|
Long: `Fetch and upgrade given collection(s) from hub`,
|
|
Long: `Fetch and upgrade given collection(s) from hub`,
|
|
@@ -121,25 +222,35 @@ func NewCollectionsCmd() *cobra.Command {
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
return compInstalledItems(cwhub.COLLECTIONS, args, toComplete)
|
|
return compInstalledItems(cwhub.COLLECTIONS, args, toComplete)
|
|
},
|
|
},
|
|
- RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
- if all {
|
|
|
|
- cwhub.UpgradeConfig(csConfig, cwhub.COLLECTIONS, "", forceAction)
|
|
|
|
- } else {
|
|
|
|
- if len(args) == 0 {
|
|
|
|
- return fmt.Errorf("specify at least one collection to upgrade or '--all'")
|
|
|
|
- }
|
|
|
|
- for _, name := range args {
|
|
|
|
- cwhub.UpgradeConfig(csConfig, cwhub.COLLECTIONS, name, forceAction)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return nil
|
|
|
|
- },
|
|
|
|
|
|
+ RunE: runCollectionsUpgrade,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ flags := cmdCollectionsUpgrade.Flags()
|
|
|
|
+ flags.BoolP("all", "a", false, "Upgrade all the collections")
|
|
|
|
+ flags.Bool("force", false, "Force upgrade : Overwrite tainted and outdated files")
|
|
|
|
+
|
|
|
|
+ return cmdCollectionsUpgrade
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func runCollectionsInspect(cmd *cobra.Command, args []string) error {
|
|
|
|
+ flags := cmd.Flags()
|
|
|
|
+
|
|
|
|
+ var err error
|
|
|
|
+ // XXX: set global
|
|
|
|
+ prometheusURL, err = flags.GetString("url")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, name := range args {
|
|
|
|
+ InspectItem(name, cwhub.COLLECTIONS)
|
|
}
|
|
}
|
|
- cmdCollectionsUpgrade.PersistentFlags().BoolVarP(&all, "all", "a", false, "Upgrade all the collections")
|
|
|
|
- cmdCollectionsUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
|
|
|
|
- cmdCollections.AddCommand(cmdCollectionsUpgrade)
|
|
|
|
|
|
|
|
- var cmdCollectionsInspect = &cobra.Command{
|
|
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func NewCollectionsInspectCmd() *cobra.Command {
|
|
|
|
+ cmdCollectionsInspect := &cobra.Command{
|
|
Use: "inspect collection",
|
|
Use: "inspect collection",
|
|
Short: "Inspect given collection",
|
|
Short: "Inspect given collection",
|
|
Long: `Inspect given collection`,
|
|
Long: `Inspect given collection`,
|
|
@@ -149,28 +260,42 @@ func NewCollectionsCmd() *cobra.Command {
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
return compInstalledItems(cwhub.COLLECTIONS, args, toComplete)
|
|
return compInstalledItems(cwhub.COLLECTIONS, args, toComplete)
|
|
},
|
|
},
|
|
- Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
- for _, name := range args {
|
|
|
|
- InspectItem(name, cwhub.COLLECTIONS)
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
|
|
+ RunE: runCollectionsInspect,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ flags := cmdCollectionsInspect.Flags()
|
|
|
|
+ flags.StringP("url", "u", "", "Prometheus url")
|
|
|
|
+
|
|
|
|
+ return cmdCollectionsInspect
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func runCollectionsList(cmd *cobra.Command, args []string) error {
|
|
|
|
+ flags := cmd.Flags()
|
|
|
|
+
|
|
|
|
+ all, err := flags.GetBool("all")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
}
|
|
}
|
|
- cmdCollectionsInspect.PersistentFlags().StringVarP(&prometheusURL, "url", "u", "", "Prometheus url")
|
|
|
|
- cmdCollections.AddCommand(cmdCollectionsInspect)
|
|
|
|
|
|
|
|
- var cmdCollectionsList = &cobra.Command{
|
|
|
|
|
|
+ // XXX: will happily ignore missing collections
|
|
|
|
+ ListItems(color.Output, []string{cwhub.COLLECTIONS}, args, false, true, all)
|
|
|
|
+
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func NewCollectionsListCmd() *cobra.Command {
|
|
|
|
+ cmdCollectionsList := &cobra.Command{
|
|
Use: "list collection [-a]",
|
|
Use: "list collection [-a]",
|
|
Short: "List all collections",
|
|
Short: "List all collections",
|
|
Long: `List all collections`,
|
|
Long: `List all collections`,
|
|
Example: `cscli collections list`,
|
|
Example: `cscli collections list`,
|
|
Args: cobra.ExactArgs(0),
|
|
Args: cobra.ExactArgs(0),
|
|
DisableAutoGenTag: true,
|
|
DisableAutoGenTag: true,
|
|
- Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
- ListItems(color.Output, []string{cwhub.COLLECTIONS}, args, false, true, all)
|
|
|
|
- },
|
|
|
|
|
|
+ RunE: runCollectionsList,
|
|
}
|
|
}
|
|
- cmdCollectionsList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
|
|
|
|
- cmdCollections.AddCommand(cmdCollectionsList)
|
|
|
|
|
|
|
|
- return cmdCollections
|
|
|
|
|
|
+ flags := cmdCollectionsList.Flags()
|
|
|
|
+ flags.BoolP("all", "a", false, "List disabled items as well")
|
|
|
|
+
|
|
|
|
+ return cmdCollectionsList
|
|
}
|
|
}
|