remove.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/spf13/cobra"
  7. )
  8. var purge_remove, remove_all bool
  9. func RemoveMany(ttype string, name string) {
  10. var err error
  11. var disabled int
  12. for _, v := range cwhub.HubIdx[ttype] {
  13. if name != "" && v.Name == name {
  14. v, err = cwhub.DisableItem(v, cwhub.Installdir, cwhub.Hubdir, purge_remove)
  15. if err != nil {
  16. log.Fatalf("unable to disable %s : %v", v.Name, err)
  17. }
  18. cwhub.HubIdx[ttype][v.Name] = v
  19. return
  20. } else if name == "" && remove_all {
  21. v, err = cwhub.DisableItem(v, cwhub.Installdir, cwhub.Hubdir, purge_remove)
  22. if err != nil {
  23. log.Fatalf("unable to disable %s : %v", v.Name, err)
  24. }
  25. cwhub.HubIdx[ttype][v.Name] = v
  26. disabled += 1
  27. }
  28. }
  29. if name != "" && !remove_all {
  30. log.Errorf("%s not found", name)
  31. return
  32. }
  33. log.Infof("Disabled %d items", disabled)
  34. }
  35. func NewRemoveCmd() *cobra.Command {
  36. var cmdRemove = &cobra.Command{
  37. Use: "remove [type] <config>",
  38. Short: "Remove/disable configuration(s)",
  39. Long: `
  40. Remove local configuration.
  41. [type] must be parser, scenario, postoverflow, collection
  42. [config_name] must be a valid config name from [Crowdsec Hub](https://hub.crowdsec.net) or locally installed.
  43. `,
  44. Example: `cscli remove [type] [config_name]`,
  45. Args: cobra.MinimumNArgs(1),
  46. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  47. if !config.configured {
  48. return fmt.Errorf("you must configure cli before interacting with hub")
  49. }
  50. return nil
  51. },
  52. }
  53. cmdRemove.PersistentFlags().BoolVar(&purge_remove, "purge", false, "Delete source file in ~/.cscli/hub/ too")
  54. cmdRemove.PersistentFlags().BoolVar(&remove_all, "all", false, "Delete all the files in selected scope")
  55. var cmdRemoveParser = &cobra.Command{
  56. Use: "parser <config>",
  57. Short: "Remove/disable parser",
  58. Long: `<config> must be a valid parser.`,
  59. Args: cobra.MinimumNArgs(0),
  60. Run: func(cmd *cobra.Command, args []string) {
  61. if err := cwhub.GetHubIdx(); err != nil {
  62. log.Fatalf("Failed to get Hub index : %v", err)
  63. }
  64. if remove_all && len(args) == 0 {
  65. RemoveMany(cwhub.PARSERS, "")
  66. } else if len(args) == 1 {
  67. RemoveMany(cwhub.PARSERS, args[0])
  68. } else {
  69. _ = cmd.Help()
  70. return
  71. }
  72. //fmt.Println("remove/disable parser: " + strings.Join(args, " "))
  73. },
  74. }
  75. cmdRemove.AddCommand(cmdRemoveParser)
  76. var cmdRemoveScenario = &cobra.Command{
  77. Use: "scenario [config]",
  78. Short: "Remove/disable scenario",
  79. Long: `<config> must be a valid scenario.`,
  80. Args: cobra.MinimumNArgs(0),
  81. Run: func(cmd *cobra.Command, args []string) {
  82. if err := cwhub.GetHubIdx(); err != nil {
  83. log.Fatalf("Failed to get Hub index : %v", err)
  84. }
  85. if remove_all && len(args) == 0 {
  86. RemoveMany(cwhub.SCENARIOS, "")
  87. } else if len(args) == 1 {
  88. RemoveMany(cwhub.SCENARIOS, args[0])
  89. } else {
  90. _ = cmd.Help()
  91. return
  92. }
  93. },
  94. }
  95. cmdRemove.AddCommand(cmdRemoveScenario)
  96. var cmdRemoveCollection = &cobra.Command{
  97. Use: "collection [config]",
  98. Short: "Remove/disable collection",
  99. Long: `<config> must be a valid collection.`,
  100. Args: cobra.MinimumNArgs(0),
  101. Run: func(cmd *cobra.Command, args []string) {
  102. if err := cwhub.GetHubIdx(); err != nil {
  103. log.Fatalf("Failed to get Hub index : %v", err)
  104. }
  105. if remove_all && len(args) == 0 {
  106. RemoveMany(cwhub.COLLECTIONS, "")
  107. } else if len(args) == 1 {
  108. RemoveMany(cwhub.COLLECTIONS, args[0])
  109. } else {
  110. _ = cmd.Help()
  111. return
  112. }
  113. },
  114. }
  115. cmdRemove.AddCommand(cmdRemoveCollection)
  116. var cmdRemovePostoverflow = &cobra.Command{
  117. Use: "postoverflow [config]",
  118. Short: "Remove/disable postoverflow parser",
  119. Long: `<config> must be a valid collection.`,
  120. Args: cobra.MinimumNArgs(0),
  121. Run: func(cmd *cobra.Command, args []string) {
  122. if err := cwhub.GetHubIdx(); err != nil {
  123. log.Fatalf("Failed to get Hub index : %v", err)
  124. }
  125. if remove_all && len(args) == 0 {
  126. RemoveMany(cwhub.PARSERS_OVFLW, "")
  127. } else if len(args) == 1 {
  128. RemoveMany(cwhub.PARSERS_OVFLW, args[0])
  129. } else {
  130. _ = cmd.Help()
  131. return
  132. }
  133. },
  134. }
  135. cmdRemove.AddCommand(cmdRemovePostoverflow)
  136. return cmdRemove
  137. }