mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-18 09:17:49 +08:00
Add builder as a global flag.
This allows all subcommands to use this flag. Additionally reads the default value for the flag from the `BUILDX_BUILDER` env var. Precedence is: CLI ARG > flag > env var > config file Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
parent
213d3af3b0
commit
b2ec1d331c
@ -99,7 +99,7 @@ func defaultFiles() ([]string, error) {
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func bakeCmd(dockerCli command.Cli) *cobra.Command {
|
func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
||||||
var options bakeOptions
|
var options bakeOptions
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
|
@ -63,13 +63,12 @@ type buildOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type commonOptions struct {
|
type commonOptions struct {
|
||||||
builderOptions
|
builder string
|
||||||
noCache *bool
|
noCache *bool
|
||||||
progress string
|
progress string
|
||||||
pull *bool
|
pull *bool
|
||||||
exportPush bool
|
exportPush bool
|
||||||
exportLoad bool
|
exportLoad bool
|
||||||
builder string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runBuild(dockerCli command.Cli, in buildOptions) error {
|
func runBuild(dockerCli command.Cli, in buildOptions) error {
|
||||||
@ -210,7 +209,7 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildCmd(dockerCli command.Cli) *cobra.Command {
|
func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
||||||
var options buildOptions
|
var options buildOptions
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@ -220,6 +219,7 @@ func buildCmd(dockerCli command.Cli) *cobra.Command {
|
|||||||
Args: cli.ExactArgs(1),
|
Args: cli.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
options.contextPath = args[0]
|
options.contextPath = args[0]
|
||||||
|
options.builder = rootOpts.builder
|
||||||
return runBuild(dockerCli, options)
|
return runBuild(dockerCli, options)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -305,7 +305,6 @@ func buildCmd(dockerCli command.Cli) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func commonBuildFlags(options *commonOptions, flags *pflag.FlagSet) {
|
func commonBuildFlags(options *commonOptions, flags *pflag.FlagSet) {
|
||||||
builderFlags(&options.builderOptions, flags)
|
|
||||||
flags.Var(flagutil.Tristate(options.noCache), "no-cache", "Do not use cache when building the image")
|
flags.Var(flagutil.Tristate(options.noCache), "no-cache", "Do not use cache when building the image")
|
||||||
flags.StringVar(&options.progress, "progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output")
|
flags.StringVar(&options.progress, "progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output")
|
||||||
flags.Var(flagutil.Tristate(options.pull), "pull", "Always attempt to pull a newer version of the image")
|
flags.Var(flagutil.Tristate(options.pull), "pull", "Always attempt to pull a newer version of the image")
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
package commands
|
|
||||||
|
|
||||||
import "github.com/spf13/pflag"
|
|
||||||
|
|
||||||
type builderOptions struct {
|
|
||||||
builder string
|
|
||||||
}
|
|
||||||
|
|
||||||
func builderFlags(options *builderOptions, flags *pflag.FlagSet) {
|
|
||||||
flags.StringVar(&options.builder, "builder", "", "Override the configured builder instance")
|
|
||||||
}
|
|
@ -18,7 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type duOptions struct {
|
type duOptions struct {
|
||||||
builderOptions
|
builder string
|
||||||
filter opts.FilterOpt
|
filter opts.FilterOpt
|
||||||
verbose bool
|
verbose bool
|
||||||
}
|
}
|
||||||
@ -98,7 +98,7 @@ func runDiskUsage(dockerCli command.Cli, opts duOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func duCmd(dockerCli command.Cli) *cobra.Command {
|
func duCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
||||||
options := duOptions{filter: opts.NewFilterOpt()}
|
options := duOptions{filter: opts.NewFilterOpt()}
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@ -106,13 +106,13 @@ func duCmd(dockerCli command.Cli) *cobra.Command {
|
|||||||
Short: "Disk usage",
|
Short: "Disk usage",
|
||||||
Args: cli.NoArgs,
|
Args: cli.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
options.builder = rootOpts.builder
|
||||||
return runDiskUsage(dockerCli, options)
|
return runDiskUsage(dockerCli, options)
|
||||||
},
|
},
|
||||||
Annotations: map[string]string{"version": "1.00"},
|
Annotations: map[string]string{"version": "1.00"},
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
builderFlags(&options.builderOptions, flags)
|
|
||||||
flags.Var(&options.filter, "filter", "Provide filter values")
|
flags.Var(&options.filter, "filter", "Provide filter values")
|
||||||
flags.BoolVar(&options.verbose, "verbose", false, "Provide a more verbose output")
|
flags.BoolVar(&options.verbose, "verbose", false, "Provide a more verbose output")
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
|
|
||||||
type inspectOptions struct {
|
type inspectOptions struct {
|
||||||
bootstrap bool
|
bootstrap bool
|
||||||
|
builder string
|
||||||
}
|
}
|
||||||
|
|
||||||
type dinfo struct {
|
type dinfo struct {
|
||||||
@ -38,7 +39,7 @@ type nginfo struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func runInspect(dockerCli command.Cli, in inspectOptions, args []string) error {
|
func runInspect(dockerCli command.Cli, in inspectOptions) error {
|
||||||
ctx := appcontext.Context()
|
ctx := appcontext.Context()
|
||||||
|
|
||||||
txn, release, err := getStore(dockerCli)
|
txn, release, err := getStore(dockerCli)
|
||||||
@ -49,8 +50,8 @@ func runInspect(dockerCli command.Cli, in inspectOptions, args []string) error {
|
|||||||
|
|
||||||
var ng *store.NodeGroup
|
var ng *store.NodeGroup
|
||||||
|
|
||||||
if len(args) > 0 {
|
if in.builder != "" {
|
||||||
ng, err = getNodeGroup(txn, dockerCli, args[0])
|
ng, err = getNodeGroup(txn, dockerCli, in.builder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -127,7 +128,7 @@ func runInspect(dockerCli command.Cli, in inspectOptions, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func inspectCmd(dockerCli command.Cli) *cobra.Command {
|
func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
||||||
var options inspectOptions
|
var options inspectOptions
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@ -135,7 +136,11 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command {
|
|||||||
Short: "Inspect current builder instance",
|
Short: "Inspect current builder instance",
|
||||||
Args: cli.RequiresMaxArgs(1),
|
Args: cli.RequiresMaxArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runInspect(dockerCli, options, args)
|
options.builder = rootOpts.builder
|
||||||
|
if len(args) > 0 {
|
||||||
|
options.builder = args[0]
|
||||||
|
}
|
||||||
|
return runInspect(dockerCli, options)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type pruneOptions struct {
|
type pruneOptions struct {
|
||||||
builderOptions
|
builder string
|
||||||
all bool
|
all bool
|
||||||
filter opts.FilterOpt
|
filter opts.FilterOpt
|
||||||
keepStorage opts.MemBytes
|
keepStorage opts.MemBytes
|
||||||
@ -124,7 +124,7 @@ func runPrune(dockerCli command.Cli, opts pruneOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pruneCmd(dockerCli command.Cli) *cobra.Command {
|
func pruneCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
||||||
options := pruneOptions{filter: opts.NewFilterOpt()}
|
options := pruneOptions{filter: opts.NewFilterOpt()}
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@ -132,6 +132,7 @@ func pruneCmd(dockerCli command.Cli) *cobra.Command {
|
|||||||
Short: "Remove build cache ",
|
Short: "Remove build cache ",
|
||||||
Args: cli.NoArgs,
|
Args: cli.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
options.builder = rootOpts.builder
|
||||||
return runPrune(dockerCli, options)
|
return runPrune(dockerCli, options)
|
||||||
},
|
},
|
||||||
Annotations: map[string]string{"version": "1.00"},
|
Annotations: map[string]string{"version": "1.00"},
|
||||||
|
@ -11,9 +11,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type rmOptions struct {
|
type rmOptions struct {
|
||||||
|
builder string
|
||||||
}
|
}
|
||||||
|
|
||||||
func runRm(dockerCli command.Cli, in rmOptions, args []string) error {
|
func runRm(dockerCli command.Cli, in rmOptions) error {
|
||||||
ctx := appcontext.Context()
|
ctx := appcontext.Context()
|
||||||
|
|
||||||
txn, release, err := getStore(dockerCli)
|
txn, release, err := getStore(dockerCli)
|
||||||
@ -22,8 +23,8 @@ func runRm(dockerCli command.Cli, in rmOptions, args []string) error {
|
|||||||
}
|
}
|
||||||
defer release()
|
defer release()
|
||||||
|
|
||||||
if len(args) > 0 {
|
if in.builder != "" {
|
||||||
ng, err := getNodeGroup(txn, dockerCli, args[0])
|
ng, err := getNodeGroup(txn, dockerCli, in.builder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -49,7 +50,7 @@ func runRm(dockerCli command.Cli, in rmOptions, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func rmCmd(dockerCli command.Cli) *cobra.Command {
|
func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
||||||
var options rmOptions
|
var options rmOptions
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@ -57,7 +58,11 @@ func rmCmd(dockerCli command.Cli) *cobra.Command {
|
|||||||
Short: "Remove a builder instance",
|
Short: "Remove a builder instance",
|
||||||
Args: cli.RequiresMaxArgs(1),
|
Args: cli.RequiresMaxArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runRm(dockerCli, options, args)
|
options.builder = rootOpts.builder
|
||||||
|
if len(args) > 0 {
|
||||||
|
options.builder = args[0]
|
||||||
|
}
|
||||||
|
return runRm(dockerCli, options)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
imagetoolscmd "github.com/docker/buildx/commands/imagetools"
|
imagetoolscmd "github.com/docker/buildx/commands/imagetools"
|
||||||
"github.com/docker/cli/cli-plugins/plugin"
|
"github.com/docker/cli/cli-plugins/plugin"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Command {
|
func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Command {
|
||||||
@ -22,21 +25,32 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
|
|||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type rootOptions struct {
|
||||||
|
builder string
|
||||||
|
}
|
||||||
|
|
||||||
func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
|
func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
|
||||||
|
opts := &rootOptions{}
|
||||||
|
rootFlags(opts, cmd.PersistentFlags())
|
||||||
|
|
||||||
cmd.AddCommand(
|
cmd.AddCommand(
|
||||||
buildCmd(dockerCli),
|
buildCmd(dockerCli, opts),
|
||||||
bakeCmd(dockerCli),
|
bakeCmd(dockerCli, opts),
|
||||||
createCmd(dockerCli),
|
createCmd(dockerCli),
|
||||||
rmCmd(dockerCli),
|
rmCmd(dockerCli, opts),
|
||||||
lsCmd(dockerCli),
|
lsCmd(dockerCli),
|
||||||
useCmd(dockerCli),
|
useCmd(dockerCli, opts),
|
||||||
inspectCmd(dockerCli),
|
inspectCmd(dockerCli, opts),
|
||||||
stopCmd(dockerCli),
|
stopCmd(dockerCli, opts),
|
||||||
installCmd(dockerCli),
|
installCmd(dockerCli),
|
||||||
uninstallCmd(dockerCli),
|
uninstallCmd(dockerCli),
|
||||||
versionCmd(dockerCli),
|
versionCmd(dockerCli),
|
||||||
pruneCmd(dockerCli),
|
pruneCmd(dockerCli, opts),
|
||||||
duCmd(dockerCli),
|
duCmd(dockerCli, opts),
|
||||||
imagetoolscmd.RootCmd(dockerCli),
|
imagetoolscmd.RootCmd(dockerCli),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func rootFlags(options *rootOptions, flags *pflag.FlagSet) {
|
||||||
|
flags.StringVar(&options.builder, "builder", os.Getenv("BUILDX_BUILDER"), "Override the configured builder instance")
|
||||||
|
}
|
||||||
|
@ -8,9 +8,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type stopOptions struct {
|
type stopOptions struct {
|
||||||
|
builder string
|
||||||
}
|
}
|
||||||
|
|
||||||
func runStop(dockerCli command.Cli, in stopOptions, args []string) error {
|
func runStop(dockerCli command.Cli, in stopOptions) error {
|
||||||
ctx := appcontext.Context()
|
ctx := appcontext.Context()
|
||||||
|
|
||||||
txn, release, err := getStore(dockerCli)
|
txn, release, err := getStore(dockerCli)
|
||||||
@ -19,8 +20,8 @@ func runStop(dockerCli command.Cli, in stopOptions, args []string) error {
|
|||||||
}
|
}
|
||||||
defer release()
|
defer release()
|
||||||
|
|
||||||
if len(args) > 0 {
|
if in.builder != "" {
|
||||||
ng, err := getNodeGroup(txn, dockerCli, args[0])
|
ng, err := getNodeGroup(txn, dockerCli, in.builder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -41,7 +42,7 @@ func runStop(dockerCli command.Cli, in stopOptions, args []string) error {
|
|||||||
return stopCurrent(ctx, dockerCli, false)
|
return stopCurrent(ctx, dockerCli, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stopCmd(dockerCli command.Cli) *cobra.Command {
|
func stopCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
||||||
var options stopOptions
|
var options stopOptions
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@ -49,7 +50,11 @@ func stopCmd(dockerCli command.Cli) *cobra.Command {
|
|||||||
Short: "Stop builder instance",
|
Short: "Stop builder instance",
|
||||||
Args: cli.RequiresMaxArgs(1),
|
Args: cli.RequiresMaxArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runStop(dockerCli, options, args)
|
options.builder = rootOpts.builder
|
||||||
|
if len(args) > 0 {
|
||||||
|
options.builder = args[0]
|
||||||
|
}
|
||||||
|
return runStop(dockerCli, options)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,21 +12,22 @@ import (
|
|||||||
type useOptions struct {
|
type useOptions struct {
|
||||||
isGlobal bool
|
isGlobal bool
|
||||||
isDefault bool
|
isDefault bool
|
||||||
|
builder string
|
||||||
}
|
}
|
||||||
|
|
||||||
func runUse(dockerCli command.Cli, in useOptions, name string) error {
|
func runUse(dockerCli command.Cli, in useOptions) error {
|
||||||
txn, release, err := getStore(dockerCli)
|
txn, release, err := getStore(dockerCli)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer release()
|
defer release()
|
||||||
|
|
||||||
if _, err := txn.NodeGroupByName(name); err != nil {
|
if _, err := txn.NodeGroupByName(in.builder); err != nil {
|
||||||
if os.IsNotExist(errors.Cause(err)) {
|
if os.IsNotExist(errors.Cause(err)) {
|
||||||
if name == "default" && name != dockerCli.CurrentContext() {
|
if in.builder == "default" && in.builder != dockerCli.CurrentContext() {
|
||||||
return errors.Errorf("run `docker context use default` to switch to default context")
|
return errors.Errorf("run `docker context use default` to switch to default context")
|
||||||
}
|
}
|
||||||
if name == "default" || name == dockerCli.CurrentContext() {
|
if in.builder == "default" || in.builder == dockerCli.CurrentContext() {
|
||||||
ep, err := getCurrentEndpoint(dockerCli)
|
ep, err := getCurrentEndpoint(dockerCli)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -41,35 +42,39 @@ func runUse(dockerCli command.Cli, in useOptions, name string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, l := range list {
|
for _, l := range list {
|
||||||
if l.Name == name {
|
if l.Name == in.builder {
|
||||||
return errors.Errorf("run `docker context use %s` to switch to context %s", name, name)
|
return errors.Errorf("run `docker context use %s` to switch to context %s", in.builder, in.builder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return errors.Wrapf(err, "failed to find instance %q", name)
|
return errors.Wrapf(err, "failed to find instance %q", in.builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
ep, err := getCurrentEndpoint(dockerCli)
|
ep, err := getCurrentEndpoint(dockerCli)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := txn.SetCurrent(ep, name, in.isGlobal, in.isDefault); err != nil {
|
if err := txn.SetCurrent(ep, in.builder, in.isGlobal, in.isDefault); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func useCmd(dockerCli command.Cli) *cobra.Command {
|
func useCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
||||||
var options useOptions
|
var options useOptions
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "use [OPTIONS] NAME",
|
Use: "use [OPTIONS] NAME",
|
||||||
Short: "Set the current builder instance",
|
Short: "Set the current builder instance",
|
||||||
Args: cli.ExactArgs(1),
|
Args: cli.RequiresMaxArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runUse(dockerCli, options, args[0])
|
options.builder = rootOpts.builder
|
||||||
|
if len(args) > 0 {
|
||||||
|
options.builder = args[0]
|
||||||
|
}
|
||||||
|
return runUse(dockerCli, options)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user