Skip to content

Commit d0526ed

Browse files
authored
Merge pull request #3876 from thaJeztah/merge_common_options
cli/flags: merge CommonOptions into ClientOptions
2 parents 64e0a6c + 3499669 commit d0526ed

10 files changed

Lines changed: 54 additions & 54 deletions

File tree

cli/cobra.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *p
2828
flags := rootCmd.Flags()
2929

3030
flags.StringVar(&opts.ConfigDir, "config", config.Dir(), "Location of client config files")
31-
opts.Common.InstallFlags(flags)
31+
opts.InstallFlags(flags)
3232

3333
cobra.AddTemplateFunc("add", func(a, b int) int { return a + b })
3434
cobra.AddTemplateFunc("hasAliases", hasAliases)
@@ -172,7 +172,7 @@ func (tcmd *TopLevelCommand) HandleGlobalFlags() (*cobra.Command, []string, erro
172172

173173
// Initialize finalises global option parsing and initializes the docker client.
174174
func (tcmd *TopLevelCommand) Initialize(ops ...command.InitializeOpt) error {
175-
tcmd.opts.Common.SetDefaultOptions(tcmd.flags)
175+
tcmd.opts.SetDefaultOptions(tcmd.flags)
176176
return tcmd.dockerCli.Initialize(tcmd.opts, ops...)
177177
}
178178

cli/command/cli.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
203203
return err
204204
}
205205
}
206-
cliflags.SetLogLevel(opts.Common.LogLevel)
206+
cliflags.SetLogLevel(opts.LogLevel)
207207

208208
if opts.ConfigDir != "" {
209209
config.SetDir(opts.ConfigDir)
210210
}
211211

212-
if opts.Common.Debug {
212+
if opts.Debug {
213213
debug.Enable()
214214
}
215215

@@ -219,10 +219,10 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
219219
cli.contextStore = &ContextStoreWithDefault{
220220
Store: baseContextStore,
221221
Resolver: func() (*DefaultContext, error) {
222-
return ResolveDefaultContext(opts.Common, cli.contextStoreConfig)
222+
return ResolveDefaultContext(opts, cli.contextStoreConfig)
223223
},
224224
}
225-
cli.currentContext, err = resolveContextName(opts.Common, cli.configFile, cli.contextStore)
225+
cli.currentContext, err = resolveContextName(opts, cli.configFile, cli.contextStore)
226226
if err != nil {
227227
return err
228228
}
@@ -242,7 +242,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
242242
}
243243

244244
// NewAPIClientFromFlags creates a new APIClient from command line flags
245-
func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
245+
func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
246246
storeConfig := DefaultContextStoreConfig()
247247
contextStore := &ContextStoreWithDefault{
248248
Store: store.New(config.ContextStoreDir(), storeConfig),
@@ -288,7 +288,7 @@ func resolveDockerEndpoint(s store.Reader, contextName string) (docker.Endpoint,
288288
}
289289

290290
// Resolve the Docker endpoint for the default context (based on config, env vars and CLI flags)
291-
func resolveDefaultDockerEndpoint(opts *cliflags.CommonOptions) (docker.Endpoint, error) {
291+
func resolveDefaultDockerEndpoint(opts *cliflags.ClientOptions) (docker.Endpoint, error) {
292292
host, err := getServerHost(opts.Hosts, opts.TLSOptions)
293293
if err != nil {
294294
return docker.Endpoint{}, err
@@ -445,7 +445,7 @@ func UserAgent() string {
445445
// - if DOCKER_CONTEXT is set, use this value
446446
// - if Config file has a globally set "CurrentContext", use this value
447447
// - fallbacks to default HOST, uses TLS config from flags/env vars
448-
func resolveContextName(opts *cliflags.CommonOptions, config *configfile.ConfigFile, contextstore store.Reader) (string, error) {
448+
func resolveContextName(opts *cliflags.ClientOptions, config *configfile.ConfigFile, contextstore store.Reader) (string, error) {
449449
if opts.Context != "" && len(opts.Hosts) > 0 {
450450
return "", errors.New("Conflicting options: either specify --host or --context, not both")
451451
}

cli/command/cli_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestNewAPIClientFromFlags(t *testing.T) {
3131
if runtime.GOOS == "windows" {
3232
host = "npipe://./"
3333
}
34-
opts := &flags.CommonOptions{Hosts: []string{host}}
34+
opts := &flags.ClientOptions{Hosts: []string{host}}
3535
apiClient, err := NewAPIClientFromFlags(opts, &configfile.ConfigFile{})
3636
assert.NilError(t, err)
3737
assert.Equal(t, apiClient.DaemonHost(), host)
@@ -44,7 +44,7 @@ func TestNewAPIClientFromFlagsForDefaultSchema(t *testing.T) {
4444
if runtime.GOOS == "windows" {
4545
slug = "tcp://127.0.0.1"
4646
}
47-
opts := &flags.CommonOptions{Hosts: []string{host}}
47+
opts := &flags.ClientOptions{Hosts: []string{host}}
4848
apiClient, err := NewAPIClientFromFlags(opts, &configfile.ConfigFile{})
4949
assert.NilError(t, err)
5050
assert.Equal(t, apiClient.DaemonHost(), slug+host)
@@ -62,7 +62,7 @@ func TestNewAPIClientFromFlagsWithCustomHeaders(t *testing.T) {
6262
}))
6363
defer ts.Close()
6464
host := strings.Replace(ts.URL, "http://", "tcp://", 1)
65-
opts := &flags.CommonOptions{Hosts: []string{host}}
65+
opts := &flags.ClientOptions{Hosts: []string{host}}
6666
configFile := &configfile.ConfigFile{
6767
HTTPHeaders: map[string]string{
6868
"My-Header": "Custom-Value",
@@ -91,7 +91,7 @@ func TestNewAPIClientFromFlagsWithAPIVersionFromEnv(t *testing.T) {
9191
t.Setenv("DOCKER_API_VERSION", customVersion)
9292
t.Setenv("DOCKER_HOST", ":2375")
9393

94-
opts := &flags.CommonOptions{}
94+
opts := &flags.ClientOptions{}
9595
configFile := &configfile.ConfigFile{}
9696
apiclient, err := NewAPIClientFromFlags(opts, configFile)
9797
assert.NilError(t, err)
@@ -191,7 +191,7 @@ func TestInitializeFromClientHangs(t *testing.T) {
191191
ts.Start()
192192
defer ts.Close()
193193

194-
opts := &flags.CommonOptions{Hosts: []string{fmt.Sprintf("unix://%s", socket)}}
194+
opts := &flags.ClientOptions{Hosts: []string{fmt.Sprintf("unix://%s", socket)}}
195195
configFile := &configfile.ConfigFile{}
196196
apiClient, err := NewAPIClientFromFlags(opts, configFile)
197197
assert.NilError(t, err)

cli/command/defaultcontextstore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type EndpointDefaultResolver interface {
4242
}
4343

4444
// ResolveDefaultContext creates a Metadata for the current CLI invocation parameters
45-
func ResolveDefaultContext(opts *cliflags.CommonOptions, config store.Config) (*DefaultContext, error) {
45+
func ResolveDefaultContext(opts *cliflags.ClientOptions, config store.Config) (*DefaultContext, error) {
4646
contextTLSData := store.ContextTLSData{
4747
Endpoints: make(map[string]store.EndpointTLSData),
4848
}

cli/command/defaultcontextstore_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestDefaultContextInitializer(t *testing.T) {
5656
assert.NilError(t, err)
5757
t.Setenv("DOCKER_HOST", "ssh://someswarmserver")
5858
cli.configFile = &configfile.ConfigFile{}
59-
ctx, err := ResolveDefaultContext(&cliflags.CommonOptions{
59+
ctx, err := ResolveDefaultContext(&cliflags.ClientOptions{
6060
TLS: true,
6161
TLSOptions: &tlsconfig.Options{
6262
CAFile: "./testdata/ca.pem",

cli/flags/client.go

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,68 +43,69 @@ var (
4343
dockerTLS = os.Getenv("DOCKER_TLS") != ""
4444
)
4545

46-
// CommonOptions are options common to both the client and the daemon.
47-
type CommonOptions struct {
46+
// ClientOptions are the options used to configure the client cli.
47+
type ClientOptions struct {
4848
Debug bool
4949
Hosts []string
5050
LogLevel string
5151
TLS bool
5252
TLSVerify bool
5353
TLSOptions *tlsconfig.Options
5454
Context string
55+
ConfigDir string
5556
}
5657

57-
// NewCommonOptions returns a new CommonOptions
58-
func NewCommonOptions() *CommonOptions {
59-
return &CommonOptions{}
58+
// NewClientOptions returns a new ClientOptions.
59+
func NewClientOptions() *ClientOptions {
60+
return &ClientOptions{}
6061
}
6162

6263
// InstallFlags adds flags for the common options on the FlagSet
63-
func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) {
64+
func (o *ClientOptions) InstallFlags(flags *pflag.FlagSet) {
6465
if dockerCertPath == "" {
6566
dockerCertPath = config.Dir()
6667
}
6768

68-
flags.BoolVarP(&commonOpts.Debug, "debug", "D", false, "Enable debug mode")
69-
flags.StringVarP(&commonOpts.LogLevel, "log-level", "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
70-
flags.BoolVar(&commonOpts.TLS, "tls", dockerTLS, "Use TLS; implied by --tlsverify")
71-
flags.BoolVar(&commonOpts.TLSVerify, FlagTLSVerify, dockerTLSVerify, "Use TLS and verify the remote")
69+
flags.BoolVarP(&o.Debug, "debug", "D", false, "Enable debug mode")
70+
flags.StringVarP(&o.LogLevel, "log-level", "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
71+
flags.BoolVar(&o.TLS, "tls", dockerTLS, "Use TLS; implied by --tlsverify")
72+
flags.BoolVar(&o.TLSVerify, FlagTLSVerify, dockerTLSVerify, "Use TLS and verify the remote")
7273

7374
// TODO use flag flags.String("identity"}, "i", "", "Path to libtrust key file")
7475

75-
commonOpts.TLSOptions = &tlsconfig.Options{
76+
o.TLSOptions = &tlsconfig.Options{
7677
CAFile: filepath.Join(dockerCertPath, DefaultCaFile),
7778
CertFile: filepath.Join(dockerCertPath, DefaultCertFile),
7879
KeyFile: filepath.Join(dockerCertPath, DefaultKeyFile),
7980
}
80-
tlsOptions := commonOpts.TLSOptions
81+
tlsOptions := o.TLSOptions
8182
flags.Var(opts.NewQuotedString(&tlsOptions.CAFile), "tlscacert", "Trust certs signed only by this CA")
8283
flags.Var(opts.NewQuotedString(&tlsOptions.CertFile), "tlscert", "Path to TLS certificate file")
8384
flags.Var(opts.NewQuotedString(&tlsOptions.KeyFile), "tlskey", "Path to TLS key file")
8485

8586
// opts.ValidateHost is not used here, so as to allow connection helpers
86-
hostOpt := opts.NewNamedListOptsRef("hosts", &commonOpts.Hosts, nil)
87+
hostOpt := opts.NewNamedListOptsRef("hosts", &o.Hosts, nil)
8788
flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")
88-
flags.StringVarP(&commonOpts.Context, "context", "c", "",
89+
flags.StringVarP(&o.Context, "context", "c", "",
8990
`Name of the context to use to connect to the daemon (overrides `+client.EnvOverrideHost+` env var and default context set with "docker context use")`)
9091
}
9192

9293
// SetDefaultOptions sets default values for options after flag parsing is
9394
// complete
94-
func (commonOpts *CommonOptions) SetDefaultOptions(flags *pflag.FlagSet) {
95+
func (o *ClientOptions) SetDefaultOptions(flags *pflag.FlagSet) {
9596
// Regardless of whether the user sets it to true or false, if they
9697
// specify --tlsverify at all then we need to turn on TLS
9798
// TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
9899
// to check that here as well
99-
if flags.Changed(FlagTLSVerify) || commonOpts.TLSVerify {
100-
commonOpts.TLS = true
100+
if flags.Changed(FlagTLSVerify) || o.TLSVerify {
101+
o.TLS = true
101102
}
102103

103-
if !commonOpts.TLS {
104-
commonOpts.TLSOptions = nil
104+
if !o.TLS {
105+
o.TLSOptions = nil
105106
} else {
106-
tlsOptions := commonOpts.TLSOptions
107-
tlsOptions.InsecureSkipVerify = !commonOpts.TLSVerify
107+
tlsOptions := o.TLSOptions
108+
tlsOptions.InsecureSkipVerify = !o.TLSVerify
108109

109110
// Reset CertFile and KeyFile to empty string if the user did not specify
110111
// the respective flags and the respective default files were not found.

cli/flags/options_deprecated.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package flags
2+
3+
// CommonOptions are options common to both the client and the daemon.
4+
//
5+
// Deprecated: use [ClientOptions].
6+
type CommonOptions = ClientOptions
7+
8+
// NewCommonOptions returns a new CommonOptions
9+
//
10+
// Deprecated: use [NewClientOptions].
11+
var NewCommonOptions = NewClientOptions
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
is "gotest.tools/v3/assert/cmp"
1111
)
1212

13-
func TestCommonOptionsInstallFlags(t *testing.T) {
13+
func TestClientOptionsInstallFlags(t *testing.T) {
1414
flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
15-
opts := NewCommonOptions()
15+
opts := NewClientOptions()
1616
opts.InstallFlags(flags)
1717

1818
err := flags.Parse([]string{
@@ -30,9 +30,9 @@ func defaultPath(filename string) string {
3030
return filepath.Join(config.Dir(), filename)
3131
}
3232

33-
func TestCommonOptionsInstallFlagsWithDefaults(t *testing.T) {
33+
func TestClientOptionsInstallFlagsWithDefaults(t *testing.T) {
3434
flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
35-
opts := NewCommonOptions()
35+
opts := NewClientOptions()
3636
opts.InstallFlags(flags)
3737

3838
err := flags.Parse([]string{})

cmd/docker/builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ echo '{"SchemaVersion":"0.1.0","Vendor":"Docker Inc.","Version":"v0.6.3","ShortD
7979
}))
8080
}
8181
opts := flags.NewClientOptions()
82-
opts.Common.Context = tt.context
82+
opts.Context = tt.context
8383
assert.NilError(t, dockerCli.Initialize(opts))
8484
}
8585

0 commit comments

Comments
 (0)