diff --git a/cli/command/cli.go b/cli/command/cli.go index 4d8b9dc406b6..524b934a321a 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -234,7 +234,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 @@ -245,12 +245,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 95f89fc6ff0a..4ddc466dc4be 100644 --- a/cli/command/defaultcontextstore.go +++ b/cli/command/defaultcontextstore.go @@ -28,66 +28,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)