Skip to content

Commit

Permalink
cli/command: remove unused EndpointDefaultResolver interface
Browse files Browse the repository at this point in the history
this was added as part of 520be05, but
is not used anywhere, so we may as well simplify things a bit.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed May 5, 2023
1 parent 8fc956c commit 5348dbf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 57 deletions.
8 changes: 3 additions & 5 deletions cli/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
67 changes: 16 additions & 51 deletions cli/command/defaultcontextstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cli/command/defaultcontextstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5348dbf

Please sign in to comment.