cli/command: resolveContextName() move conflicting options check

There's no strict need to perform this validation inside this function;
validating flags should happen earlier, to allow faster detecting of
configuration issues (we may want to have a central config "validate"
function though).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-04 10:33:22 +01:00
parent 2f5698511a
commit 793f09705d
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 18 additions and 21 deletions

View File

@ -211,6 +211,9 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
if opts.Debug { if opts.Debug {
debug.Enable() debug.Enable()
} }
if opts.Context != "" && len(opts.Hosts) > 0 {
return errors.New("conflicting options: either specify --host or --context, not both")
}
cli.loadConfigFile() cli.loadConfigFile()
@ -221,10 +224,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
return ResolveDefaultContext(opts, cli.contextStoreConfig) return ResolveDefaultContext(opts, cli.contextStoreConfig)
}, },
} }
cli.currentContext, err = resolveContextName(opts, cli.configFile) cli.currentContext = resolveContextName(opts, cli.configFile)
if err != nil {
return err
}
cli.dockerEndpoint, err = resolveDockerEndpoint(cli.contextStore, cli.currentContext) cli.dockerEndpoint, err = resolveDockerEndpoint(cli.contextStore, cli.currentContext)
if err != nil { if err != nil {
return errors.Wrap(err, "unable to resolve docker endpoint") return errors.Wrap(err, "unable to resolve docker endpoint")
@ -242,6 +242,10 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
// NewAPIClientFromFlags creates a new APIClient from command line flags // NewAPIClientFromFlags creates a new APIClient from command line flags
func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile.ConfigFile) (client.APIClient, error) { func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
if opts.Context != "" && len(opts.Hosts) > 0 {
return nil, errors.New("conflicting options: either specify --host or --context, not both")
}
storeConfig := DefaultContextStoreConfig() storeConfig := DefaultContextStoreConfig()
contextStore := &ContextStoreWithDefault{ contextStore := &ContextStoreWithDefault{
Store: store.New(config.ContextStoreDir(), storeConfig), Store: store.New(config.ContextStoreDir(), storeConfig),
@ -249,11 +253,7 @@ func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile.
return ResolveDefaultContext(opts, storeConfig) return ResolveDefaultContext(opts, storeConfig)
}, },
} }
contextName, err := resolveContextName(opts, configFile) endpoint, err := resolveDockerEndpoint(contextStore, resolveContextName(opts, configFile))
if err != nil {
return nil, err
}
endpoint, err := resolveDockerEndpoint(contextStore, contextName)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "unable to resolve docker endpoint") return nil, errors.Wrap(err, "unable to resolve docker endpoint")
} }
@ -447,27 +447,24 @@ func UserAgent() string {
// //
// resolveContextName does not validate if the given context exists; errors may // resolveContextName does not validate if the given context exists; errors may
// occur when trying to use it. // occur when trying to use it.
func resolveContextName(opts *cliflags.ClientOptions, config *configfile.ConfigFile) (string, error) { func resolveContextName(opts *cliflags.ClientOptions, config *configfile.ConfigFile) string {
if opts.Context != "" && len(opts.Hosts) > 0 { if opts != nil && opts.Context != "" {
return "", errors.New("Conflicting options: either specify --host or --context, not both") return opts.Context
} }
if opts.Context != "" { if opts != nil && len(opts.Hosts) > 0 {
return opts.Context, nil return DefaultContextName
}
if len(opts.Hosts) > 0 {
return DefaultContextName, nil
} }
if os.Getenv(client.EnvOverrideHost) != "" { if os.Getenv(client.EnvOverrideHost) != "" {
return DefaultContextName, nil return DefaultContextName
} }
if ctxName := os.Getenv("DOCKER_CONTEXT"); ctxName != "" { if ctxName := os.Getenv("DOCKER_CONTEXT"); ctxName != "" {
return ctxName, nil return ctxName
} }
if config != nil && config.CurrentContext != "" { if config != nil && config.CurrentContext != "" {
// We don't validate if this context exists: errors may occur when trying to use it. // We don't validate if this context exists: errors may occur when trying to use it.
return config.CurrentContext, nil return config.CurrentContext
} }
return DefaultContextName, nil return DefaultContextName
} }
var defaultStoreEndpoints = []store.NamedTypeGetter{ var defaultStoreEndpoints = []store.NamedTypeGetter{