diff --git a/cli/command/completion/functions.go b/cli/command/completion/functions.go index 917c84ff1f07..978615bfb3d3 100644 --- a/cli/command/completion/functions.go +++ b/cli/command/completion/functions.go @@ -223,6 +223,7 @@ type Image struct { Creator int `json:"creator"` Repository int `json:"repository"` } + type ImageTags struct { Count int `json:"count"` Next string `json:"next"` @@ -230,7 +231,7 @@ type ImageTags struct { Results []Image `json:"results"` } -func Images(cmd *cobra.Command, arg []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func RemoteImages(cmd *cobra.Command, arg []string, toComplete string) ([]string, cobra.ShellCompDirective) { ctx := cmd.Context() c := &http.Client{ Timeout: 2 * time.Second, @@ -259,7 +260,6 @@ func Images(cmd *cobra.Command, arg []string, toComplete string) ([]string, cobr logrus.Errorf("Error sending hub image tags request: %v", err) return nil, cobra.ShellCompDirectiveError } - defer resp.Body.Close() var tags *ImageTags @@ -278,7 +278,7 @@ func Images(cmd *cobra.Command, arg []string, toComplete string) ([]string, cobr u, err := url.Parse("https://hub.docker.com/api/search/v3/catalog/search") if err != nil { logrus.Errorf("Error parsing hub image search URL: %v", err) - return nil, cobra.ShellCompDirectiveNoFileComp + return nil, cobra.ShellCompDirectiveError } q := u.Query() q.Set("query", toComplete) @@ -290,20 +290,20 @@ func Images(cmd *cobra.Command, arg []string, toComplete string) ([]string, cobr req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) if err != nil { logrus.Errorf("Error creating hub image search request: %v", err) - return nil, cobra.ShellCompDirectiveNoFileComp + return nil, cobra.ShellCompDirectiveError } resp, err := c.Do(req) if err != nil { logrus.Errorf("Error sending hub image search request: %v", err) - return nil, cobra.ShellCompDirectiveNoFileComp + return nil, cobra.ShellCompDirectiveError } defer resp.Body.Close() var images *ImageSearch if err := json.NewDecoder(resp.Body).Decode(&images); err != nil { logrus.Errorf("Error decoding hub image search response: %v", err) - return nil, cobra.ShellCompDirectiveNoFileComp + return nil, cobra.ShellCompDirectiveError } names := make([]string, 0, len(images.Results)) diff --git a/cli/command/container/run.go b/cli/command/container/run.go index dc115df2ee8e..7583a33562b0 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -25,6 +25,7 @@ type runOptions struct { detach bool sigProxy bool detachKeys string + tui bool } // NewRunCommand create a new `docker run` command @@ -37,13 +38,18 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command { Short: "Create and run a new container from an image", Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - copts.Image = args[0] + replacer := strings.NewReplacer("(local)", "", "(remote)", "") + copts.Image = replacer.Replace(args[0]) if len(args) > 1 { copts.Args = args[1:] } return runRun(cmd.Context(), dockerCli, cmd.Flags(), &options, copts) }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) > 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + unique := map[string]struct{}{} localImages, shellComp := completion.ImageNames(dockerCli)(cmd, args, toComplete) @@ -52,23 +58,23 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command { all = make([]string, 0, len(localImages)) for _, img := range localImages { unique[img] = struct{}{} - all = append(all, fmt.Sprintf("%s\tlocal", img)) + all = append(all, img+"\tlocal") } } - remoteImages, shellCompRemote := completion.Images(cmd, args, toComplete) + remoteImages, shellCompRemote := completion.RemoteImages(cmd, args, toComplete) if shellCompRemote != cobra.ShellCompDirectiveError { if len(all) == 0 { all = make([]string, 0, len(remoteImages)) } for _, img := range remoteImages { if _, ok := unique[img]; !ok { - all = append(all, fmt.Sprintf("%s\tremote", img)) + all = append(all, img+"\tremote") } } } - return all, cobra.ShellCompDirectiveKeepOrder | cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveDefault + return all, cobra.ShellCompDirectiveKeepOrder | cobra.ShellCompDirectiveNoFileComp }, Annotations: map[string]string{ "category-top": "1", @@ -86,6 +92,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command { flags.StringVar(&options.detachKeys, "detach-keys", "", "Override the key sequence for detaching a container") flags.StringVar(&options.pull, "pull", PullImageMissing, `Pull image before running ("`+PullImageAlways+`", "`+PullImageMissing+`", "`+PullImageNever+`")`) flags.BoolVarP(&options.quiet, "quiet", "q", false, "Suppress the pull output") + flags.BoolVarP(&options.tui, "tui", "", false, "Enable TUI mode") // Add an explicit help that doesn't have a `-h` to prevent the conflict // with hostname diff --git a/cli/command/image/pull.go b/cli/command/image/pull.go index 928a3c0b81e0..2d81bfa7bc73 100644 --- a/cli/command/image/pull.go +++ b/cli/command/image/pull.go @@ -43,7 +43,7 @@ func NewPullCommand(dockerCli command.Cli) *cobra.Command { if len(args) > 0 { return nil, cobra.ShellCompDirectiveNoFileComp } - return completion.Images(cmd, args, toComplete) + return completion.RemoteImages(cmd, args, toComplete) }, } diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 8b15b76f8ec7..b7e185d10bd6 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -89,11 +89,6 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand { }, Version: fmt.Sprintf("%s, build %s", version.Version, version.GitCommit), DisableFlagsInUseLine: true, - CompletionOptions: cobra.CompletionOptions{ - DisableDefaultCmd: false, - HiddenDefaultCmd: true, - DisableDescriptions: true, - }, } cmd.SetIn(dockerCli.In()) cmd.SetOut(dockerCli.Out())