From 7b8bf9f801db1d4811aa479b0ab57c96fb1ac8ff Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Fri, 4 Apr 2025 12:12:46 +0200 Subject: [PATCH] cmd: support cli environment variables in standalone mode Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- cmd/buildx/main.go | 5 ----- commands/root.go | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/cmd/buildx/main.go b/cmd/buildx/main.go index 8333ecc4..f7fb26e0 100644 --- a/cmd/buildx/main.go +++ b/cmd/buildx/main.go @@ -15,7 +15,6 @@ import ( "github.com/docker/cli/cli-plugins/plugin" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/debug" - cliflags "github.com/docker/cli/cli/flags" "github.com/moby/buildkit/solver/errdefs" "github.com/moby/buildkit/util/stack" "github.com/pkg/errors" @@ -37,11 +36,7 @@ func init() { } func runStandalone(cmd *command.DockerCli) error { - if err := cmd.Initialize(cliflags.NewClientOptions()); err != nil { - return err - } defer flushMetrics(cmd) - executable := os.Args[0] rootCmd := commands.NewRootCmd(filepath.Base(executable), false, cmd) return rootCmd.Execute() diff --git a/commands/root.go b/commands/root.go index 4ea29ebe..e71867c4 100644 --- a/commands/root.go +++ b/commands/root.go @@ -16,13 +16,14 @@ import ( "github.com/docker/cli/cli-plugins/plugin" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/debug" + cliflags "github.com/docker/cli/cli/flags" "github.com/moby/buildkit/util/appcontext" "github.com/sirupsen/logrus" "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.DockerCli) *cobra.Command { var opt rootOptions cmd := &cobra.Command{ Short: "Docker Buildx", @@ -40,7 +41,17 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman } cmd.SetContext(appcontext.Context()) if !isPlugin { - return nil + // InstallFlags and SetDefaultOptions are necessary to match + // the plugin mode behavior to handle env vars such as + // DOCKER_TLS, DOCKER_TLS_VERIFY, ... and we also need to use a + // new flagset to avoid conflict with the global debug flag + // that we already handle in the root command otherwise it + // would panic. + nflags := pflag.NewFlagSet(cmd.DisplayName(), pflag.ContinueOnError) + options := cliflags.NewClientOptions() + options.InstallFlags(nflags) + options.SetDefaultOptions(nflags) + return dockerCli.Initialize(options) } return plugin.PersistentPreRunE(cmd, args) },