suggestNearestMessage returns a message with the most similar item name, if one is found
(hub *cwhub.Hub, itemType string, itemName string)
| 23 | |
| 24 | // suggestNearestMessage returns a message with the most similar item name, if one is found |
| 25 | func 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 | |
| 48 | func (cli *cliItem) install(ctx context.Context, args []string, interactive bool, dryRun bool, downloadOnly bool, force bool, ignoreError bool) error { |
| 49 | cfg := cli.cfg() |
no test coverage detected
searching dependent graphs…