mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-10 05:27:07 +08:00
cli: add completion to list builder names
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@ -113,7 +113,7 @@ func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
|||||||
}
|
}
|
||||||
return runInspect(dockerCli, options)
|
return runInspect(dockerCli, options)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.Disable,
|
ValidArgsFunction: completion.BuilderNames(dockerCli),
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
@ -93,7 +93,7 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
|||||||
}
|
}
|
||||||
return runRm(dockerCli, options)
|
return runRm(dockerCli, options)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.Disable,
|
ValidArgsFunction: completion.BuilderNames(dockerCli),
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
imagetoolscmd "github.com/docker/buildx/commands/imagetools"
|
imagetoolscmd "github.com/docker/buildx/commands/imagetools"
|
||||||
"github.com/docker/buildx/controller/remote"
|
"github.com/docker/buildx/controller/remote"
|
||||||
|
"github.com/docker/buildx/util/cobrautil/completion"
|
||||||
"github.com/docker/buildx/util/logutil"
|
"github.com/docker/buildx/util/logutil"
|
||||||
"github.com/docker/cli-docs-tool/annotation"
|
"github.com/docker/cli-docs-tool/annotation"
|
||||||
"github.com/docker/cli/cli"
|
"github.com/docker/cli/cli"
|
||||||
@ -93,6 +94,11 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
|
|||||||
if isExperimental() {
|
if isExperimental() {
|
||||||
remote.AddControllerCommands(cmd, dockerCli)
|
remote.AddControllerCommands(cmd, dockerCli)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmd.RegisterFlagCompletionFunc( //nolint:errcheck
|
||||||
|
"builder",
|
||||||
|
completion.BuilderNames(dockerCli),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func rootFlags(options *rootOptions, flags *pflag.FlagSet) {
|
func rootFlags(options *rootOptions, flags *pflag.FlagSet) {
|
||||||
|
@ -47,7 +47,7 @@ func stopCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
|||||||
}
|
}
|
||||||
return runStop(dockerCli, options)
|
return runStop(dockerCli, options)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.Disable,
|
ValidArgsFunction: completion.BuilderNames(dockerCli),
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
@ -79,7 +79,7 @@ func useCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
|||||||
}
|
}
|
||||||
return runUse(dockerCli, options)
|
return runUse(dockerCli, options)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.Disable,
|
ValidArgsFunction: completion.BuilderNames(dockerCli),
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
@ -4,6 +4,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/buildx/bake"
|
"github.com/docker/buildx/bake"
|
||||||
|
"github.com/docker/buildx/builder"
|
||||||
|
"github.com/docker/buildx/store/storeutil"
|
||||||
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,3 +39,24 @@ func BakeTargets(files []string) ValidArgsFn {
|
|||||||
return filtered, cobra.ShellCompDirectiveNoFileComp
|
return filtered, cobra.ShellCompDirectiveNoFileComp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuilderNames(dockerCli command.Cli) ValidArgsFn {
|
||||||
|
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
txn, release, err := storeutil.GetStore(dockerCli)
|
||||||
|
if err != nil {
|
||||||
|
return nil, cobra.ShellCompDirectiveError
|
||||||
|
}
|
||||||
|
defer release()
|
||||||
|
builders, err := builder.GetBuilders(dockerCli, txn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, cobra.ShellCompDirectiveError
|
||||||
|
}
|
||||||
|
var filtered []string
|
||||||
|
for _, b := range builders {
|
||||||
|
if toComplete == "" || strings.HasPrefix(b.Name, toComplete) {
|
||||||
|
filtered = append(filtered, b.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filtered, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user