Skip to content

Commit

Permalink
feat: run with hub completion
Browse files Browse the repository at this point in the history
Signed-off-by: Alano Terblanche <[email protected]>
  • Loading branch information
Benehiko committed Oct 11, 2024
1 parent f4c164d commit 8511401
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
12 changes: 6 additions & 6 deletions cli/command/completion/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,15 @@ type Image struct {
Creator int `json:"creator"`
Repository int `json:"repository"`
}

type ImageTags struct {
Count int `json:"count"`
Next string `json:"next"`
Prev string `json:"prev"`
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,
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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))
Expand Down
17 changes: 12 additions & 5 deletions cli/command/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type runOptions struct {
detach bool
sigProxy bool
detachKeys string
tui bool
}

// NewRunCommand create a new `docker run` command
Expand All @@ -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)

Expand All @@ -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",
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
5 changes: 0 additions & 5 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 8511401

Please sign in to comment.