Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bake: add completion to list targets #1674

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ func ReadLocalFiles(names []string) ([]File, error) {
return out, nil
}

func ListTargets(files []File) ([]string, error) {
c, err := ParseFiles(files, nil)
if err != nil {
return nil, err
}
var targets []string
for _, g := range c.Groups {
targets = append(targets, g.Name)
}
for _, t := range c.Targets {
targets = append(targets, t.Name)
}
return dedupSlice(targets), nil
}

func ReadTargets(ctx context.Context, files []File, targets, overrides []string, defaults map[string]string) (map[string]*Target, map[string]*Group, error) {
c, err := ParseFiles(files, defaults)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/docker/buildx/builder"
controllerapi "github.com/docker/buildx/controller/pb"
"github.com/docker/buildx/util/buildflags"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/confutil"
"github.com/docker/buildx/util/dockerutil"
"github.com/docker/buildx/util/progress"
Expand Down Expand Up @@ -214,6 +215,7 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
// Other common flags (noCache, pull and progress) are processed in runBake function.
return runBake(dockerCli, args, options, cFlags)
},
ValidArgsFunction: cobrautil.CompleteBakeTargets(options.files),
}

flags := cmd.Flags()
Expand Down
3 changes: 3 additions & 0 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
}
return runBuild(dockerCli, options)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveFilterDirs
},
}

var platformsDefault []string
Expand Down
1 change: 1 addition & 0 deletions commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ func createCmd(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runCreate(dockerCli, options, args)
},
ValidArgsFunction: cobrautil.NoCompletion,
}

flags := cmd.Flags()
Expand Down
2 changes: 2 additions & 0 deletions commands/diskusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/opts"
Expand Down Expand Up @@ -115,6 +116,7 @@ func duCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
options.builder = rootOpts.builder
return runDiskUsage(dockerCli, options)
},
ValidArgsFunction: cobrautil.NoCompletion,
}

flags := cmd.Flags()
Expand Down
2 changes: 2 additions & 0 deletions commands/imagetools/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/imagetools"
"github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -273,6 +274,7 @@ func createCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command {
options.builder = *opts.Builder
return runCreate(dockerCli, options, args)
},
ValidArgsFunction: cobrautil.NoCompletion,
}

flags := cmd.Flags()
Expand Down
2 changes: 2 additions & 0 deletions commands/imagetools/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/imagetools"
"github.com/docker/cli-docs-tool/annotation"
"github.com/docker/cli/cli"
Expand Down Expand Up @@ -52,6 +53,7 @@ func inspectCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
options.builder = *rootOpts.Builder
return runInspect(dockerCli, options, args[0])
},
ValidArgsFunction: cobrautil.NoCompletion,
}

flags := cmd.Flags()
Expand Down
6 changes: 4 additions & 2 deletions commands/imagetools/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
)
Expand All @@ -11,8 +12,9 @@ type RootOptions struct {

func RootCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "imagetools",
Short: "Commands to work on images in registry",
Use: "imagetools",
Short: "Commands to work on images in registry",
ValidArgsFunction: cobrautil.NoCompletion,
}

cmd.AddCommand(
Expand Down
2 changes: 2 additions & 0 deletions commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/platformutil"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -112,6 +113,7 @@ func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
}
return runInspect(dockerCli, options)
},
ValidArgsFunction: cobrautil.NoCompletion,
}

flags := cmd.Flags()
Expand Down
3 changes: 2 additions & 1 deletion commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func installCmd(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runInstall(dockerCli, options)
},
Hidden: true,
Hidden: true,
ValidArgsFunction: cobrautil.NoCompletion,
}

// hide builder persistent flag for this command
Expand Down
1 change: 1 addition & 0 deletions commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func lsCmd(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runLs(dockerCli, options)
},
ValidArgsFunction: cobrautil.NoCompletion,
}

// hide builder persistent flag for this command
Expand Down
2 changes: 2 additions & 0 deletions commands/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/opts"
Expand Down Expand Up @@ -139,6 +140,7 @@ func pruneCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
options.builder = rootOpts.builder
return runPrune(dockerCli, options)
},
ValidArgsFunction: cobrautil.NoCompletion,
}

flags := cmd.Flags()
Expand Down
2 changes: 2 additions & 0 deletions commands/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/buildx/builder"
"github.com/docker/buildx/store"
"github.com/docker/buildx/store/storeutil"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/buildkit/util/appcontext"
Expand Down Expand Up @@ -92,6 +93,7 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
}
return runRm(dockerCli, options)
},
ValidArgsFunction: cobrautil.NoCompletion,
}

flags := cmd.Flags()
Expand Down
3 changes: 3 additions & 0 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
Annotations: map[string]string{
annotation.CodeDelimiter: `"`,
},
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
}
if isPlugin {
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 2 additions & 0 deletions commands/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/buildkit/util/appcontext"
Expand Down Expand Up @@ -46,6 +47,7 @@ func stopCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
}
return runStop(dockerCli, options)
},
ValidArgsFunction: cobrautil.NoCompletion,
}

return cmd
Expand Down
3 changes: 2 additions & 1 deletion commands/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func uninstallCmd(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runUninstall(dockerCli, options)
},
Hidden: true,
Hidden: true,
ValidArgsFunction: cobrautil.NoCompletion,
}

// hide builder persistent flag for this command
Expand Down
2 changes: 2 additions & 0 deletions commands/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

"github.com/docker/buildx/store/storeutil"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/dockerutil"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -78,6 +79,7 @@ func useCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
}
return runUse(dockerCli, options)
},
ValidArgsFunction: cobrautil.NoCompletion,
}

flags := cmd.Flags()
Expand Down
1 change: 1 addition & 0 deletions commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func versionCmd(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runVersion(dockerCli)
},
ValidArgsFunction: cobrautil.NoCompletion,
}

// hide builder persistent flag for this command
Expand Down
38 changes: 38 additions & 0 deletions util/cobrautil/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cobrautil

import (
"strings"

"github.com/docker/buildx/bake"
"github.com/spf13/cobra"
)

// ValidArgsFn defines a completion func to be returned to fetch completion options
type ValidArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)

func NoCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
}

func CompleteBakeTargets(files []string) ValidArgsFn {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
f, err := bake.ReadLocalFiles(files)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
tgts, err := bake.ListTargets(f)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var filtered []string
if toComplete == "" {
return tgts, cobra.ShellCompDirectiveNoFileComp
}
for _, tgt := range tgts {
if strings.HasPrefix(tgt, toComplete) {
filtered = append(filtered, tgt)
}
}
return filtered, cobra.ShellCompDirectiveNoFileComp
}
}