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

Function suggestNearestMessage

cmd/crowdsec-cli/cliitem/cmdinstall.go:25–46  ·  view source on GitHub ↗

suggestNearestMessage returns a message with the most similar item name, if one is found

(hub *cwhub.Hub, itemType string, itemName string)

Source from the content-addressed store, hash-verified

23
24// suggestNearestMessage returns a message with the most similar item name, if one is found
25func suggestNearestMessage(hub *cwhub.Hub, itemType string, itemName string) string {
26 const maxDistance = 7
27
28 score := 100
29 nearest := ""
30
31 for _, item := range hub.GetItemsByType(itemType, false) {
32 d := levenshtein.Distance(itemName, item.Name, nil)
33 if d < score {
34 score = d
35 nearest = item.Name
36 }
37 }
38
39 msg := fmt.Sprintf("can't find '%s' in %s", itemName, itemType)
40
41 if score < maxDistance {
42 msg += fmt.Sprintf(", did you mean '%s'?", nearest)
43 }
44
45 return msg
46}
47
48func (cli *cliItem) install(ctx context.Context, args []string, interactive bool, dryRun bool, downloadOnly bool, force bool, ignoreError bool) error {
49 cfg := cli.cfg()

Callers 1

installMethod · 0.85

Calls 1

GetItemsByTypeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…