diff --git a/cli/command/cli.go b/cli/command/cli.go index dfc22b701f9d..78f010fd12f9 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -233,7 +233,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize cli.contextStore = &ContextStoreWithDefault{ Store: store.New(config.ContextStoreDir(), cli.contextStoreConfig), Resolver: func() (*DefaultContext, error) { - return ResolveDefaultContext(cli.options, cli.contextStoreConfig) + return ResolveDefaultContext(cli.options) }, } return nil @@ -244,12 +244,10 @@ func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile. if opts.Context != "" && len(opts.Hosts) > 0 { return nil, errors.New("conflicting options: either specify --host or --context, not both") } - - storeConfig := DefaultContextStoreConfig() contextStore := &ContextStoreWithDefault{ - Store: store.New(config.ContextStoreDir(), storeConfig), + Store: store.New(config.ContextStoreDir(), DefaultContextStoreConfig()), Resolver: func() (*DefaultContext, error) { - return ResolveDefaultContext(opts, storeConfig) + return ResolveDefaultContext(opts) }, } endpoint, err := resolveDockerEndpoint(contextStore, resolveContextName(opts, configFile)) diff --git a/cli/command/defaultcontextstore.go b/cli/command/defaultcontextstore.go index 203900e2fc02..66e011e79a9f 100644 --- a/cli/command/defaultcontextstore.go +++ b/cli/command/defaultcontextstore.go @@ -34,66 +34,31 @@ type ContextStoreWithDefault struct { Resolver DefaultContextResolver } -// EndpointDefaultResolver is implemented by any EndpointMeta object -// which wants to be able to populate the store with whatever their default is. -type EndpointDefaultResolver interface { - // ResolveDefault returns values suitable for storing in store.Metadata.Endpoints - // and store.ContextTLSData.Endpoints. - // - // An error is only returned for something fatal, not simply - // the lack of a default (e.g. because the config file which - // would contain it is missing). If there is no default then - // returns nil, nil, nil. - ResolveDefault() (interface{}, *store.EndpointTLSData, error) -} - // ResolveDefaultContext creates a Metadata for the current CLI invocation parameters -func ResolveDefaultContext(opts *cliflags.ClientOptions, config store.Config) (*DefaultContext, error) { - contextTLSData := store.ContextTLSData{ - Endpoints: make(map[string]store.EndpointTLSData), - } - contextMetadata := store.Metadata{ - Endpoints: make(map[string]interface{}), - Metadata: DockerContext{ - Description: "", - }, - Name: DefaultContextName, - } - +func ResolveDefaultContext(opts *cliflags.ClientOptions) (*DefaultContext, error) { dockerEP, err := resolveDefaultDockerEndpoint(opts) if err != nil { return nil, err } - contextMetadata.Endpoints[docker.DockerEndpoint] = dockerEP.EndpointMeta + contextTLSData := store.ContextTLSData{} if dockerEP.TLSData != nil { - contextTLSData.Endpoints[docker.DockerEndpoint] = *dockerEP.TLSData.ToStoreTLSData() - } - - if err := config.ForeachEndpointType(func(n string, get store.TypeGetter) error { - if n == docker.DockerEndpoint { // handled above - return nil + contextTLSData.Endpoints = map[string]store.EndpointTLSData{ + docker.DockerEndpoint: *dockerEP.TLSData.ToStoreTLSData(), } - ep := get() - if i, ok := ep.(EndpointDefaultResolver); ok { - meta, tls, err := i.ResolveDefault() - if err != nil { - return err - } - if meta == nil { - return nil - } - contextMetadata.Endpoints[n] = meta - if tls != nil { - contextTLSData.Endpoints[n] = *tls - } - } - // Nothing to be done - return nil - }); err != nil { - return nil, err } - return &DefaultContext{Meta: contextMetadata, TLS: contextTLSData}, nil + return &DefaultContext{ + Meta: store.Metadata{ + Endpoints: map[string]interface{}{ + docker.DockerEndpoint: dockerEP.EndpointMeta, + }, + Metadata: DockerContext{ + Description: "", + }, + Name: DefaultContextName, + }, + TLS: contextTLSData, + }, nil } // List implements store.Store's List diff --git a/cli/command/defaultcontextstore_test.go b/cli/command/defaultcontextstore_test.go index a368135ff7fc..c0f8e7927575 100644 --- a/cli/command/defaultcontextstore_test.go +++ b/cli/command/defaultcontextstore_test.go @@ -61,7 +61,7 @@ func TestDefaultContextInitializer(t *testing.T) { TLSOptions: &tlsconfig.Options{ CAFile: "./testdata/ca.pem", }, - }, DefaultContextStoreConfig()) + }) assert.NilError(t, err) assert.Equal(t, "default", ctx.Meta.Name) assert.DeepEqual(t, "ssh://someswarmserver", ctx.Meta.Endpoints[docker.DockerEndpoint].(docker.EndpointMeta).Host) diff --git a/cli/context/store/storeconfig.go b/cli/context/store/storeconfig.go index 9c93ecbab2b1..e1b54238f9ff 100644 --- a/cli/context/store/storeconfig.go +++ b/cli/context/store/storeconfig.go @@ -30,16 +30,6 @@ func (c Config) SetEndpoint(name string, getter TypeGetter) { c.endpointTypes[name] = getter } -// ForeachEndpointType calls cb on every endpoint type registered with the Config -func (c Config) ForeachEndpointType(cb func(string, TypeGetter) error) error { - for n, ep := range c.endpointTypes { - if err := cb(n, ep); err != nil { - return err - } - } - return nil -} - // NewConfig creates a config object func NewConfig(contextType TypeGetter, endpoints ...NamedTypeGetter) Config { res := Config{