|
| 1 | +package completion |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + |
| 6 | + "github.com/docker/cli/cli/command" |
| 7 | + "github.com/docker/cli/cli/command/formatter" |
| 8 | + "github.com/docker/docker/api/types" |
| 9 | + "github.com/docker/docker/api/types/filters" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +// ValidArgsFn a function to be used by cobra command as `ValidArgsFunction` to offer command line completion |
| 14 | +type ValidArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) |
| 15 | + |
| 16 | +// ImageNames offers completion for images present within the local store |
| 17 | +func ImageNames(dockerCli command.Cli) ValidArgsFn { |
| 18 | + return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 19 | + list, err := dockerCli.Client().ImageList(cmd.Context(), types.ImageListOptions{}) |
| 20 | + if err != nil { |
| 21 | + return nil, cobra.ShellCompDirectiveError |
| 22 | + } |
| 23 | + var names []string |
| 24 | + for _, image := range list { |
| 25 | + names = append(names, image.RepoTags...) |
| 26 | + } |
| 27 | + return names, cobra.ShellCompDirectiveNoFileComp |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// ContainerNames offers completion for container names and IDs |
| 32 | +// By default, only names are returned. |
| 33 | +// Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs. |
| 34 | +func ContainerNames(dockerCli command.Cli, all bool, filters ...func(types.Container) bool) ValidArgsFn { |
| 35 | + return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 36 | + list, err := dockerCli.Client().ContainerList(cmd.Context(), types.ContainerListOptions{ |
| 37 | + All: all, |
| 38 | + }) |
| 39 | + if err != nil { |
| 40 | + return nil, cobra.ShellCompDirectiveError |
| 41 | + } |
| 42 | + |
| 43 | + showContainerIDs := os.Getenv("DOCKER_COMPLETION_SHOW_CONTAINER_IDS") == "yes" |
| 44 | + |
| 45 | + var names []string |
| 46 | + for _, container := range list { |
| 47 | + skip := false |
| 48 | + for _, fn := range filters { |
| 49 | + if !fn(container) { |
| 50 | + skip = true |
| 51 | + break |
| 52 | + } |
| 53 | + } |
| 54 | + if skip { |
| 55 | + continue |
| 56 | + } |
| 57 | + if showContainerIDs { |
| 58 | + names = append(names, container.ID) |
| 59 | + } |
| 60 | + names = append(names, formatter.StripNamePrefix(container.Names)...) |
| 61 | + } |
| 62 | + return names, cobra.ShellCompDirectiveNoFileComp |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// VolumeNames offers completion for volumes |
| 67 | +func VolumeNames(dockerCli command.Cli) ValidArgsFn { |
| 68 | + return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 69 | + list, err := dockerCli.Client().VolumeList(cmd.Context(), filters.Args{}) |
| 70 | + if err != nil { |
| 71 | + return nil, cobra.ShellCompDirectiveError |
| 72 | + } |
| 73 | + var names []string |
| 74 | + for _, volume := range list.Volumes { |
| 75 | + names = append(names, volume.Name) |
| 76 | + } |
| 77 | + return names, cobra.ShellCompDirectiveNoFileComp |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// NetworkNames offers completion for networks |
| 82 | +func NetworkNames(dockerCli command.Cli) ValidArgsFn { |
| 83 | + return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 84 | + list, err := dockerCli.Client().NetworkList(cmd.Context(), types.NetworkListOptions{}) |
| 85 | + if err != nil { |
| 86 | + return nil, cobra.ShellCompDirectiveError |
| 87 | + } |
| 88 | + var names []string |
| 89 | + for _, network := range list { |
| 90 | + names = append(names, network.Name) |
| 91 | + } |
| 92 | + return names, cobra.ShellCompDirectiveNoFileComp |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// NoComplete is used for commands where there's no relevant completion |
| 97 | +func NoComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 98 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 99 | +} |
0 commit comments