MCPcopy
hub / github.com/crowdsecurity/crowdsec / removePlan

Method removePlan

cmd/crowdsec-cli/cliitem/cmdremove.go:19–74  ·  view source on GitHub ↗
(hub *cwhub.Hub, args []string, purge bool, force bool, all bool)

Source from the content-addressed store, hash-verified

17)
18
19func (cli *cliItem) removePlan(hub *cwhub.Hub, args []string, purge bool, force bool, all bool) (*hubops.ActionPlan, error) {
20 plan := hubops.NewActionPlan(hub)
21
22 if all {
23 itemGetter := hub.GetInstalledByType
24 if purge {
25 itemGetter = hub.GetItemsByType
26 }
27
28 for _, item := range itemGetter(cli.name, true) {
29 if err := plan.AddCommand(hubops.NewDisableCommand(item, force)); err != nil {
30 return nil, err
31 }
32
33 if purge {
34 if err := plan.AddCommand(hubops.NewPurgeCommand(item, force)); err != nil {
35 return nil, err
36 }
37 }
38 }
39
40 return plan, nil
41 }
42
43 if len(args) == 0 {
44 return nil, fmt.Errorf("specify at least one %s to remove or '--all'", cli.singular)
45 }
46
47 for _, itemName := range args {
48 item := hub.GetItem(cli.name, itemName)
49 if item == nil {
50 return nil, fmt.Errorf("can't find '%s' in %s", itemName, cli.name)
51 }
52
53 parents := installedParentNames(item)
54
55 if !force && len(parents) > 0 {
56 log.Warningf("%s belongs to collections: %s", item.Name, parents)
57 log.Warningf("Run 'sudo cscli %s remove %s --force' if you want to force remove this %s", item.Type, item.Name, cli.singular)
58
59 continue
60 }
61
62 if err := plan.AddCommand(hubops.NewDisableCommand(item, force)); err != nil {
63 return nil, err
64 }
65
66 if purge {
67 if err := plan.AddCommand(hubops.NewPurgeCommand(item, force)); err != nil {
68 return nil, err
69 }
70 }
71 }
72
73 return plan, nil
74}
75
76// return the names of the installed parents of an item, used to check if we can remove it

Callers 1

removeMethod · 0.95

Calls 6

AddCommandMethod · 0.95
NewActionPlanFunction · 0.92
NewDisableCommandFunction · 0.92
NewPurgeCommandFunction · 0.92
installedParentNamesFunction · 0.85
GetItemMethod · 0.80

Tested by

no test coverage detected