completeNodeNames offers completion for swarm node (host)names and optional IDs. By default, only names are returned. Set DOCKER_COMPLETION_SHOW_NODE_IDS=yes to also complete IDs. TODO(thaJeztah): add support for filters.
(dockerCLI completion.APIClientProvider)
| 14 | // |
| 15 | // TODO(thaJeztah): add support for filters. |
| 16 | func completeNodeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { |
| 17 | // https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43 |
| 18 | showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_NODE_IDS") == "yes" |
| 19 | return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 20 | res, err := dockerCLI.Client().NodeList(cmd.Context(), client.NodeListOptions{}) |
| 21 | if err != nil { |
| 22 | return nil, cobra.ShellCompDirectiveError |
| 23 | } |
| 24 | |
| 25 | names := make([]string, 0, len(res.Items)+1) |
| 26 | for _, node := range res.Items { |
| 27 | if showIDs { |
| 28 | names = append(names, node.Description.Hostname, node.ID) |
| 29 | } else { |
| 30 | names = append(names, node.Description.Hostname) |
| 31 | } |
| 32 | } |
| 33 | // Nodes allow "self" as magic word for the current node. |
| 34 | names = append(names, "self") |
| 35 | return names, cobra.ShellCompDirectiveNoFileComp |
| 36 | } |
| 37 | } |
no test coverage detected
searching dependent graphs…