item_suggest.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/agext/levenshtein"
  6. log "github.com/sirupsen/logrus"
  7. "github.com/spf13/cobra"
  8. "slices"
  9. "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
  10. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  11. )
  12. const MaxDistance = 7
  13. func Suggest(itemType string, baseItem string, suggestItem string, score int, ignoreErr bool) {
  14. errMsg := ""
  15. if score < MaxDistance {
  16. errMsg = fmt.Sprintf("can't find '%s' in %s, did you mean %s?", baseItem, itemType, suggestItem)
  17. } else {
  18. errMsg = fmt.Sprintf("can't find '%s' in %s", baseItem, itemType)
  19. }
  20. if ignoreErr {
  21. log.Error(errMsg)
  22. } else {
  23. log.Fatalf(errMsg)
  24. }
  25. }
  26. func GetDistance(itemType string, itemName string) (*cwhub.Item, int) {
  27. allItems := make([]string, 0)
  28. nearestScore := 100
  29. nearestItem := &cwhub.Item{}
  30. // XXX: handle error
  31. hub, _ := cwhub.GetHub()
  32. hubItems := hub.GetItemMap(itemType)
  33. for _, item := range hubItems {
  34. allItems = append(allItems, item.Name)
  35. }
  36. for _, s := range allItems {
  37. d := levenshtein.Distance(itemName, s, nil)
  38. if d < nearestScore {
  39. nearestScore = d
  40. nearestItem = hub.GetItem(itemType, s)
  41. }
  42. }
  43. return nearestItem, nearestScore
  44. }
  45. func compAllItems(itemType string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  46. hub, err := require.Hub(csConfig)
  47. if err != nil {
  48. return nil, cobra.ShellCompDirectiveDefault
  49. }
  50. comp := make([]string, 0)
  51. hubItems := hub.GetItemMap(itemType)
  52. for _, item := range hubItems {
  53. if !slices.Contains(args, item.Name) && strings.Contains(item.Name, toComplete) {
  54. comp = append(comp, item.Name)
  55. }
  56. }
  57. cobra.CompDebugln(fmt.Sprintf("%s: %+v", itemType, comp), true)
  58. return comp, cobra.ShellCompDirectiveNoFileComp
  59. }
  60. func compInstalledItems(itemType string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
  61. hub, err := require.Hub(csConfig)
  62. if err != nil {
  63. return nil, cobra.ShellCompDirectiveDefault
  64. }
  65. items, err := hub.GetInstalledItemsAsString(itemType)
  66. if err != nil {
  67. cobra.CompDebugln(fmt.Sprintf("list installed %s err: %s", itemType, err), true)
  68. return nil, cobra.ShellCompDirectiveDefault
  69. }
  70. comp := make([]string, 0)
  71. if toComplete != "" {
  72. for _, item := range items {
  73. if strings.Contains(item, toComplete) {
  74. comp = append(comp, item)
  75. }
  76. }
  77. } else {
  78. comp = items
  79. }
  80. cobra.CompDebugln(fmt.Sprintf("%s: %+v", itemType, comp), true)
  81. return comp, cobra.ShellCompDirectiveNoFileComp
  82. }