completeServiceNames offers completion for swarm service names and optional IDs. By default, only names are returned. Set DOCKER_COMPLETION_SHOW_SERVICE_IDS=yes to also complete IDs.
(dockerCLI completion.APIClientProvider)
| 12 | // By default, only names are returned. |
| 13 | // Set DOCKER_COMPLETION_SHOW_SERVICE_IDS=yes to also complete IDs. |
| 14 | func completeServiceNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc { |
| 15 | // https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43 |
| 16 | showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_SERVICE_IDS") == "yes" |
| 17 | return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 18 | res, err := dockerCLI.Client().ServiceList(cmd.Context(), client.ServiceListOptions{}) |
| 19 | if err != nil { |
| 20 | return nil, cobra.ShellCompDirectiveError |
| 21 | } |
| 22 | |
| 23 | names := make([]string, 0, len(res.Items)) |
| 24 | for _, service := range res.Items { |
| 25 | if showIDs { |
| 26 | names = append(names, service.Spec.Name, service.ID) |
| 27 | } else { |
| 28 | names = append(names, service.Spec.Name) |
| 29 | } |
| 30 | } |
| 31 | return names, cobra.ShellCompDirectiveNoFileComp |
| 32 | } |
| 33 | } |
no test coverage detected
searching dependent graphs…