From 6874c2e80b6e72f1a175b00c01358091c9a9db6d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 3 Mar 2022 18:47:36 +0100 Subject: [PATCH 1/3] cli/command: remove unused args from ResolveDefaultContext() Signed-off-by: Sebastiaan van Stijn --- cli/command/cli.go | 4 ++-- cli/command/defaultcontextstore.go | 6 ++---- cli/command/defaultcontextstore_test.go | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index 2e4c0b55a67f..ab89bef8b352 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -216,7 +216,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize cli.contextStore = &ContextStoreWithDefault{ Store: baseContextStore, Resolver: func() (*DefaultContext, error) { - return ResolveDefaultContext(opts.Common, cli.ConfigFile(), cli.contextStoreConfig, cli.Err()) + return ResolveDefaultContext(opts.Common, cli.contextStoreConfig) }, } cli.currentContext, err = resolveContextName(opts.Common, cli.configFile, cli.contextStore) @@ -244,7 +244,7 @@ func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile. contextStore := &ContextStoreWithDefault{ Store: store.New(config.ContextStoreDir(), storeConfig), Resolver: func() (*DefaultContext, error) { - return ResolveDefaultContext(opts, configFile, storeConfig, io.Discard) + return ResolveDefaultContext(opts, storeConfig) }, } contextName, err := resolveContextName(opts, configFile, contextStore) diff --git a/cli/command/defaultcontextstore.go b/cli/command/defaultcontextstore.go index 4f8e2ef52360..a0dd1b18653b 100644 --- a/cli/command/defaultcontextstore.go +++ b/cli/command/defaultcontextstore.go @@ -2,9 +2,7 @@ package command import ( "fmt" - "io" - "github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/context/docker" "github.com/docker/cli/cli/context/store" cliflags "github.com/docker/cli/cli/flags" @@ -45,7 +43,7 @@ type EndpointDefaultResolver interface { } // ResolveDefaultContext creates a Metadata for the current CLI invocation parameters -func ResolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.ConfigFile, storeconfig store.Config, stderr io.Writer) (*DefaultContext, error) { +func ResolveDefaultContext(opts *cliflags.CommonOptions, config store.Config) (*DefaultContext, error) { contextTLSData := store.ContextTLSData{ Endpoints: make(map[string]store.EndpointTLSData), } @@ -66,7 +64,7 @@ func ResolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.Conf contextTLSData.Endpoints[docker.DockerEndpoint] = *dockerEP.TLSData.ToStoreTLSData() } - if err := storeconfig.ForeachEndpointType(func(n string, get store.TypeGetter) error { + if err := config.ForeachEndpointType(func(n string, get store.TypeGetter) error { if n == docker.DockerEndpoint { // handled above return nil } diff --git a/cli/command/defaultcontextstore_test.go b/cli/command/defaultcontextstore_test.go index 69fe20cc51d0..cb45c60dd4d5 100644 --- a/cli/command/defaultcontextstore_test.go +++ b/cli/command/defaultcontextstore_test.go @@ -60,7 +60,7 @@ func TestDefaultContextInitializer(t *testing.T) { TLSOptions: &tlsconfig.Options{ CAFile: "./testdata/ca.pem", }, - }, cli.ConfigFile(), DefaultContextStoreConfig(), cli.Err()) + }, 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) From 5ed92699f33b4a97e9bcbe987ae3bfed5547648e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 30 Mar 2022 18:35:55 +0200 Subject: [PATCH 2/3] cli/command: remove WithContextEndpointType() as it's unused This was added in 3126920af14ea5127e00a2f8f9d8e07a6c3f6ff9, and modified in 520be05c494940dfd70f1c43765c52735570f212, but is not used anywhere. Signed-off-by: Sebastiaan van Stijn --- cli/command/cli_options.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/cli/command/cli_options.go b/cli/command/cli_options.go index cd8179498201..535a6d5338d4 100644 --- a/cli/command/cli_options.go +++ b/cli/command/cli_options.go @@ -1,13 +1,10 @@ package command import ( - "fmt" "io" "os" "strconv" - "github.com/docker/cli/cli/context/docker" - "github.com/docker/cli/cli/context/store" "github.com/docker/cli/cli/streams" "github.com/moby/term" ) @@ -82,19 +79,6 @@ func WithContentTrust(enabled bool) DockerCliOption { } } -// WithContextEndpointType add support for an additional typed endpoint in the context store -// Plugins should use this to store additional endpoints configuration in the context store -func WithContextEndpointType(endpointName string, endpointType store.TypeGetter) DockerCliOption { - return func(cli *DockerCli) error { - switch endpointName { - case docker.DockerEndpoint: - return fmt.Errorf("cannot change %q endpoint type", endpointName) - } - cli.contextStoreConfig.SetEndpoint(endpointName, endpointType) - return nil - } -} - // WithDefaultContextStoreConfig configures the cli to use the default context store configuration. func WithDefaultContextStoreConfig() DockerCliOption { return func(cli *DockerCli) error { From 049811fef0436cd4c5808a983d7626fe9dd636d3 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 30 Mar 2022 19:05:21 +0200 Subject: [PATCH 3/3] cli/command/context: remove newContextMetadata() utility Inlining what it does Signed-off-by: Sebastiaan van Stijn --- cli/command/context/create.go | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/cli/command/context/create.go b/cli/command/context/create.go index 794a8a72275e..647295e30855 100644 --- a/cli/command/context/create.go +++ b/cli/command/context/create.go @@ -92,17 +92,24 @@ func createNewContext(o *CreateOptions, cli command.Cli, s store.Writer) error { if o.Docker == nil { return errors.New("docker endpoint configuration is required") } - contextMetadata := newContextMetadata(o) - contextTLSData := store.ContextTLSData{ - Endpoints: make(map[string]store.EndpointTLSData), - } dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(cli, o.Docker) if err != nil { return errors.Wrap(err, "unable to create docker endpoint config") } - contextMetadata.Endpoints[docker.DockerEndpoint] = dockerEP + contextMetadata := store.Metadata{ + Endpoints: map[string]interface{}{ + docker.DockerEndpoint: dockerEP, + }, + Metadata: command.DockerContext{ + Description: o.Description, + }, + Name: o.Name, + } + contextTLSData := store.ContextTLSData{} if dockerTLS != nil { - contextTLSData.Endpoints[docker.DockerEndpoint] = *dockerTLS + contextTLSData.Endpoints = map[string]store.EndpointTLSData{ + docker.DockerEndpoint: *dockerTLS, + } } if err := validateEndpoints(contextMetadata); err != nil { return err @@ -161,13 +168,3 @@ func (d *descriptionDecorator) GetMetadata(name string) (store.Metadata, error) c.Metadata = typedContext return c, nil } - -func newContextMetadata(o *CreateOptions) store.Metadata { - return store.Metadata{ - Endpoints: make(map[string]interface{}), - Metadata: command.DockerContext{ - Description: o.Description, - }, - Name: o.Name, - } -}