| 51 | } |
| 52 | |
| 53 | func runStringSliceCompletion( |
| 54 | allowedMap map[string]string, |
| 55 | toComplete string, |
| 56 | prefixAllDescription string, |
| 57 | ) ([]string, cobra.ShellCompDirective) { |
| 58 | var results []string |
| 59 | var prefix string |
| 60 | |
| 61 | if idx := strings.LastIndexByte(toComplete, ','); idx >= 0 { |
| 62 | prefix = toComplete[:idx+1] |
| 63 | toComplete = toComplete[idx+1:] |
| 64 | } |
| 65 | toComplete = strings.ToLower(toComplete) |
| 66 | |
| 67 | for name, description := range allowedMap { |
| 68 | prefixSlice := utils.StringToSlice(prefix) |
| 69 | |
| 70 | // Build dynamic description with previous selected values |
| 71 | dynamicSliceDescriptions := []string{} |
| 72 | for _, prefixName := range prefixSlice { |
| 73 | prefixDescription := allowedMap[prefixName] |
| 74 | if prefixDescription != "" { |
| 75 | dynamicSliceDescriptions = append(dynamicSliceDescriptions, prefixDescription) |
| 76 | } |
| 77 | } |
| 78 | // If current value isn't already selected and if prefix matches |
| 79 | if !utils.Contains(prefixSlice, name) && |
| 80 | strings.HasPrefix(strings.ToLower(name), toComplete) { |
| 81 | // Add description of current value |
| 82 | dynamicSliceDescriptions = append(dynamicSliceDescriptions, description) |
| 83 | results = append(results, fmt.Sprintf( |
| 84 | "%s%s\t%s", |
| 85 | prefix, |
| 86 | name, |
| 87 | fmt.Sprintf( |
| 88 | "%s %s", |
| 89 | prefixAllDescription, |
| 90 | utils.SliceToReadableString(dynamicSliceDescriptions), |
| 91 | ), |
| 92 | )) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | sort.Strings(results) |
| 97 | return results, cobra.ShellCompDirectiveNoSpace |
| 98 | } |