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 5760a3d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 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
15 changes: 10 additions & 5 deletions cli/command/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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",
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
2 changes: 1 addition & 1 deletion cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/binary
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 5760a3d

Please sign in to comment.