getPluginCompletions receives an executable's filepath, a slice of arguments, and a slice of environment variables to relay to the executable. The executable is responsible for printing the completions of the plugin for the current set of arguments.
(executablePath string, cmdArgs, environment []string)
| 222 | // The executable is responsible for printing the completions of the |
| 223 | // plugin for the current set of arguments. |
| 224 | func getPluginCompletions(executablePath string, cmdArgs, environment []string) ([]string, cobra.ShellCompDirective) { |
| 225 | buf := new(bytes.Buffer) |
| 226 | |
| 227 | prog := exec.Command(executablePath, cmdArgs...) |
| 228 | prog.Stdin = os.Stdin |
| 229 | prog.Stdout = buf |
| 230 | prog.Stderr = os.Stderr |
| 231 | prog.Env = environment |
| 232 | |
| 233 | var comps []string |
| 234 | directive := cobra.ShellCompDirectiveDefault |
| 235 | if err := prog.Run(); err == nil { |
| 236 | for _, comp := range strings.Split(buf.String(), "\n") { |
| 237 | // Remove any empty lines |
| 238 | if len(comp) > 0 { |
| 239 | comps = append(comps, comp) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // Check if the last line of output is of the form :<integer>, which |
| 244 | // indicates a Cobra ShellCompDirective. We do this for plugins |
| 245 | // that use Cobra or the ones that wish to use this directive to |
| 246 | // communicate a special behavior for the shell. |
| 247 | if len(comps) > 0 { |
| 248 | lastLine := comps[len(comps)-1] |
| 249 | if len(lastLine) > 1 && lastLine[0] == ':' { |
| 250 | if strInt, err := strconv.Atoi(lastLine[1:]); err == nil { |
| 251 | directive = cobra.ShellCompDirective(strInt) |
| 252 | comps = comps[:len(comps)-1] |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | return comps, directive |
| 258 | } |
no test coverage detected
searching dependent graphs…