Skip to content

Commit 81555db

Browse files
committed
cmd: support cli environment variables in standalone mode
Signed-off-by: CrazyMax <[email protected]>
1 parent 8efc528 commit 81555db

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

cmd/buildx/main.go

-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/docker/cli/cli-plugins/plugin"
1616
"github.com/docker/cli/cli/command"
1717
"github.com/docker/cli/cli/debug"
18-
cliflags "github.com/docker/cli/cli/flags"
1918
"github.com/moby/buildkit/solver/errdefs"
2019
"github.com/moby/buildkit/util/stack"
2120
"github.com/pkg/errors"
@@ -37,11 +36,7 @@ func init() {
3736
}
3837

3938
func runStandalone(cmd *command.DockerCli) error {
40-
if err := cmd.Initialize(cliflags.NewClientOptions()); err != nil {
41-
return err
42-
}
4339
defer flushMetrics(cmd)
44-
4540
executable := os.Args[0]
4641
rootCmd := commands.NewRootCmd(filepath.Base(executable), false, cmd)
4742
return rootCmd.Execute()

commands/root.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import (
1616
"github.com/docker/cli/cli-plugins/plugin"
1717
"github.com/docker/cli/cli/command"
1818
"github.com/docker/cli/cli/debug"
19+
cliflags "github.com/docker/cli/cli/flags"
1920
"github.com/moby/buildkit/util/appcontext"
2021
"github.com/sirupsen/logrus"
2122
"github.com/spf13/cobra"
2223
"github.com/spf13/pflag"
2324
)
2425

25-
func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Command {
26+
func NewRootCmd(name string, isPlugin bool, dockerCli *command.DockerCli) *cobra.Command {
2627
var opt rootOptions
2728
cmd := &cobra.Command{
2829
Short: "Docker Buildx",
@@ -40,7 +41,17 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
4041
}
4142
cmd.SetContext(appcontext.Context())
4243
if !isPlugin {
43-
return nil
44+
// InstallFlags and SetDefaultOptions are necessary to match
45+
// the plugin mode behavior to handle env vars such as
46+
// DOCKER_TLS, DOCKER_TLS_VERIFY, ... and we also need to use a
47+
// new flagset to avoid conflict with the global debug flag
48+
// that we already handle in the root command otherwise it
49+
// would panic.
50+
nflags := pflag.NewFlagSet(cmd.DisplayName(), pflag.ContinueOnError)
51+
options := cliflags.NewClientOptions()
52+
options.InstallFlags(nflags)
53+
options.SetDefaultOptions(nflags)
54+
return dockerCli.Initialize(options)
4455
}
4556
return plugin.PersistentPreRunE(cmd, args)
4657
},

0 commit comments

Comments
 (0)