From 5760a3d201961e0452902e815ac839647f6825e6 Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Wed, 9 Oct 2024 16:19:09 +0200 Subject: [PATCH] feat: run with hub completion Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> --- cli/command/completion/functions.go | 12 ++++++------ cli/command/container/run.go | 15 ++++++++++----- cli/command/image/pull.go | 2 +- cmd/docker/docker.go | 2 +- scripts/build/binary | 2 +- 5 files changed, 19 insertions(+), 14 deletions(-) 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..aeaffe680eb9 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -37,13 +37,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 +57,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", 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..23c536dbaf9e 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -92,7 +92,7 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand { CompletionOptions: cobra.CompletionOptions{ DisableDefaultCmd: false, HiddenDefaultCmd: true, - DisableDescriptions: true, + DisableDescriptions: false, }, } cmd.SetIn(dockerCli.In()) diff --git a/scripts/build/binary b/scripts/build/binary index 33f37628c20d..44cd4f14fdd8 100755 --- a/scripts/build/binary +++ b/scripts/build/binary @@ -24,6 +24,6 @@ if [ "$(go env GOOS)" = "windows" ]; then fi fi -(set -x ; go build -o "${TARGET}" -tags "${GO_BUILDTAGS}" -ldflags "${GO_LDFLAGS}" ${GO_BUILDMODE} -buildvcs=false "${SOURCE}") +(set -x ; go build -o "${TARGET}" -tags "${GO_BUILDTAGS}" -ldflags "${GO_LDFLAGS}" ${GO_BUILDMODE} "${SOURCE}") ln -sf "$(basename "${TARGET}")" "$(dirname "${TARGET}")/docker"