Merge pull request #4362 from thaJeztah/context_dont_pass_cli

cli/command/context: don't pass CLI if we only need context-store
This commit is contained in:
Sebastiaan van Stijn 2023-06-21 11:37:58 +02:00 committed by GitHub
commit d1f5262bc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 11 deletions

View File

@ -80,7 +80,7 @@ func RunCreate(cli command.Cli, o *CreateOptions) error {
case o.From != "": case o.From != "":
err = createFromExistingContext(s, o.From, o) err = createFromExistingContext(s, o.From, o)
default: default:
err = createNewContext(o, cli, s) err = createNewContext(s, o)
} }
if err == nil { if err == nil {
fmt.Fprintln(cli.Out(), o.Name) fmt.Fprintln(cli.Out(), o.Name)
@ -89,11 +89,11 @@ func RunCreate(cli command.Cli, o *CreateOptions) error {
return err return err
} }
func createNewContext(o *CreateOptions, cli command.Cli, s store.Writer) error { func createNewContext(contextStore store.ReaderWriter, o *CreateOptions) error {
if o.Docker == nil { if o.Docker == nil {
return errors.New("docker endpoint configuration is required") return errors.New("docker endpoint configuration is required")
} }
dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(cli, o.Docker) dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(contextStore, o.Docker)
if err != nil { if err != nil {
return errors.Wrap(err, "unable to create docker endpoint config") return errors.Wrap(err, "unable to create docker endpoint config")
} }
@ -115,10 +115,10 @@ func createNewContext(o *CreateOptions, cli command.Cli, s store.Writer) error {
if err := validateEndpoints(contextMetadata); err != nil { if err := validateEndpoints(contextMetadata); err != nil {
return err return err
} }
if err := s.CreateOrUpdate(contextMetadata); err != nil { if err := contextStore.CreateOrUpdate(contextMetadata); err != nil {
return err return err
} }
return s.ResetTLSMaterial(o.Name, &contextTLSData) return contextStore.ResetTLSMaterial(o.Name, &contextTLSData)
} }
func checkContextNameForCreation(s store.Reader, name string) error { func checkContextNameForCreation(s store.Reader, name string) error {

View File

@ -5,7 +5,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/context" "github.com/docker/cli/cli/context"
"github.com/docker/cli/cli/context/docker" "github.com/docker/cli/cli/context/docker"
"github.com/docker/cli/cli/context/store" "github.com/docker/cli/cli/context/store"
@ -86,12 +85,12 @@ func validateConfig(config map[string]string, allowedKeys map[string]struct{}) e
return errors.New(strings.Join(errs, "\n")) return errors.New(strings.Join(errs, "\n"))
} }
func getDockerEndpoint(dockerCli command.Cli, config map[string]string) (docker.Endpoint, error) { func getDockerEndpoint(contextStore store.Reader, config map[string]string) (docker.Endpoint, error) {
if err := validateConfig(config, allowedDockerConfigKeys); err != nil { if err := validateConfig(config, allowedDockerConfigKeys); err != nil {
return docker.Endpoint{}, err return docker.Endpoint{}, err
} }
if contextName, ok := config[keyFrom]; ok { if contextName, ok := config[keyFrom]; ok {
metadata, err := dockerCli.ContextStore().GetMetadata(contextName) metadata, err := contextStore.GetMetadata(contextName)
if err != nil { if err != nil {
return docker.Endpoint{}, err return docker.Endpoint{}, err
} }
@ -126,8 +125,8 @@ func getDockerEndpoint(dockerCli command.Cli, config map[string]string) (docker.
return ep, nil return ep, nil
} }
func getDockerEndpointMetadataAndTLS(dockerCli command.Cli, config map[string]string) (docker.EndpointMeta, *store.EndpointTLSData, error) { func getDockerEndpointMetadataAndTLS(contextStore store.Reader, config map[string]string) (docker.EndpointMeta, *store.EndpointTLSData, error) {
ep, err := getDockerEndpoint(dockerCli, config) ep, err := getDockerEndpoint(contextStore, config)
if err != nil { if err != nil {
return docker.EndpointMeta{}, nil, err return docker.EndpointMeta{}, nil, err
} }

View File

@ -84,7 +84,7 @@ func RunUpdate(cli command.Cli, o *UpdateOptions) error {
tlsDataToReset := make(map[string]*store.EndpointTLSData) tlsDataToReset := make(map[string]*store.EndpointTLSData)
if o.Docker != nil { if o.Docker != nil {
dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(cli, o.Docker) dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(s, o.Docker)
if err != nil { if err != nil {
return errors.Wrap(err, "unable to create docker endpoint config") return errors.Wrap(err, "unable to create docker endpoint config")
} }