Merge pull request #3887 from thaJeztah/context_lazy_evaluate_step4

context: minor refactoring and fixes
This commit is contained in:
Sebastiaan van Stijn 2022-11-28 16:08:07 +01:00 committed by GitHub
commit 8b1cbfd769
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 13 deletions

View File

@ -116,7 +116,13 @@ type TopLevelCommand struct {
// NewTopLevelCommand returns a new TopLevelCommand object // NewTopLevelCommand returns a new TopLevelCommand object
func NewTopLevelCommand(cmd *cobra.Command, dockerCli *command.DockerCli, opts *cliflags.ClientOptions, flags *pflag.FlagSet) *TopLevelCommand { func NewTopLevelCommand(cmd *cobra.Command, dockerCli *command.DockerCli, opts *cliflags.ClientOptions, flags *pflag.FlagSet) *TopLevelCommand {
return &TopLevelCommand{cmd, dockerCli, opts, flags, os.Args[1:]} return &TopLevelCommand{
cmd: cmd,
dockerCli: dockerCli,
opts: opts,
flags: flags,
args: os.Args[1:],
}
} }
// SetArgs sets the args (default os.Args[:1] used to invoke the command // SetArgs sets the args (default os.Args[:1] used to invoke the command

View File

@ -69,6 +69,7 @@ type Cli interface {
// Instances of the client can be returned from NewDockerCli. // Instances of the client can be returned from NewDockerCli.
type DockerCli struct { type DockerCli struct {
configFile *configfile.ConfigFile configFile *configfile.ConfigFile
options *cliflags.ClientOptions
in *streams.In in *streams.In
out *streams.Out out *streams.Out
err io.Writer err io.Writer
@ -132,16 +133,13 @@ func ShowHelp(err io.Writer) func(*cobra.Command, []string) error {
// ConfigFile returns the ConfigFile // ConfigFile returns the ConfigFile
func (cli *DockerCli) ConfigFile() *configfile.ConfigFile { func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
// TODO(thaJeztah): when would this happen? Is this only in tests (where cli.Initialize() is not called first?)
if cli.configFile == nil { if cli.configFile == nil {
cli.loadConfigFile() cli.configFile = config.LoadDefaultConfigFile(cli.err)
} }
return cli.configFile return cli.configFile
} }
func (cli *DockerCli) loadConfigFile() {
cli.configFile = config.LoadDefaultConfigFile(cli.err)
}
// ServerInfo returns the server version details for the host this client is // ServerInfo returns the server version details for the host this client is
// connected to // connected to
func (cli *DockerCli) ServerInfo() ServerInfo { func (cli *DockerCli) ServerInfo() ServerInfo {
@ -225,17 +223,16 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
return errors.New("conflicting options: either specify --host or --context, not both") return errors.New("conflicting options: either specify --host or --context, not both")
} }
cli.loadConfigFile() cli.options = opts
cli.configFile = config.LoadDefaultConfigFile(cli.err)
baseContextStore := store.New(config.ContextStoreDir(), cli.contextStoreConfig) cli.currentContext = resolveContextName(cli.options, cli.configFile)
cli.contextStore = &ContextStoreWithDefault{ cli.contextStore = &ContextStoreWithDefault{
Store: baseContextStore, Store: store.New(config.ContextStoreDir(), cli.contextStoreConfig),
Resolver: func() (*DefaultContext, error) { Resolver: func() (*DefaultContext, error) {
return ResolveDefaultContext(opts, cli.contextStoreConfig) return ResolveDefaultContext(cli.options, cli.contextStoreConfig)
}, },
} }
cli.currentContext = resolveContextName(opts, cli.configFile) cli.dockerEndpoint, err = resolveDockerEndpoint(cli.contextStore, resolveContextName(opts, cli.configFile))
cli.dockerEndpoint, err = resolveDockerEndpoint(cli.contextStore, cli.currentContext)
if err != nil { if err != nil {
return errors.Wrap(err, "unable to resolve docker endpoint") return errors.Wrap(err, "unable to resolve docker endpoint")
} }