@@ -175,9 +175,28 @@ func (cli *DockerCli) RegistryClient(allowInsecure bool) registryclient.Registry
175175 return registryclient .NewRegistryClient (resolver , UserAgent (), allowInsecure )
176176}
177177
178+ // InitializeOpt is the type of the functional options passed to DockerCli.Initialize
179+ type InitializeOpt func (dockerCli * DockerCli ) error
180+
181+ // WithInitializeClient is passed to DockerCli.Initialize by callers who wish to set a particular API Client for use by the CLI.
182+ func WithInitializeClient (makeClient func (dockerCli * DockerCli ) (client.APIClient , error )) InitializeOpt {
183+ return func (dockerCli * DockerCli ) error {
184+ var err error
185+ dockerCli .client , err = makeClient (dockerCli )
186+ return err
187+ }
188+ }
189+
178190// Initialize the dockerCli runs initialization that must happen after command
179191// line flags are parsed.
180- func (cli * DockerCli ) Initialize (opts * cliflags.ClientOptions ) error {
192+ func (cli * DockerCli ) Initialize (opts * cliflags.ClientOptions , ops ... InitializeOpt ) error {
193+ var err error
194+
195+ for _ , o := range ops {
196+ if err := o (cli ); err != nil {
197+ return err
198+ }
199+ }
181200 cliflags .SetLogLevel (opts .Common .LogLevel )
182201
183202 if opts .ConfigDir != "" {
@@ -189,29 +208,31 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
189208 }
190209
191210 cli .configFile = cliconfig .LoadDefaultConfigFile (cli .err )
192- var err error
193- cli .contextStore = store .New (cliconfig .ContextStoreDir (), cli .contextStoreConfig )
194- cli .currentContext , err = resolveContextName (opts .Common , cli .configFile , cli .contextStore )
195- if err != nil {
196- return err
197- }
198- endpoint , err := resolveDockerEndpoint (cli .contextStore , cli .currentContext , opts .Common )
199- if err != nil {
200- return errors .Wrap (err , "unable to resolve docker endpoint" )
201- }
202- cli .dockerEndpoint = endpoint
203211
204- cli .client , err = newAPIClientFromEndpoint (endpoint , cli .configFile )
205- if tlsconfig .IsErrEncryptedKey (err ) {
206- passRetriever := passphrase .PromptRetrieverWithInOut (cli .In (), cli .Out (), nil )
207- newClient := func (password string ) (client.APIClient , error ) {
208- endpoint .TLSPassword = password
209- return newAPIClientFromEndpoint (endpoint , cli .configFile )
212+ if cli .client == nil {
213+ cli .contextStore = store .New (cliconfig .ContextStoreDir (), cli .contextStoreConfig )
214+ cli .currentContext , err = resolveContextName (opts .Common , cli .configFile , cli .contextStore )
215+ if err != nil {
216+ return err
217+ }
218+ endpoint , err := resolveDockerEndpoint (cli .contextStore , cli .currentContext , opts .Common )
219+ if err != nil {
220+ return errors .Wrap (err , "unable to resolve docker endpoint" )
221+ }
222+ cli .dockerEndpoint = endpoint
223+
224+ cli .client , err = newAPIClientFromEndpoint (endpoint , cli .configFile )
225+ if tlsconfig .IsErrEncryptedKey (err ) {
226+ passRetriever := passphrase .PromptRetrieverWithInOut (cli .In (), cli .Out (), nil )
227+ newClient := func (password string ) (client.APIClient , error ) {
228+ endpoint .TLSPassword = password
229+ return newAPIClientFromEndpoint (endpoint , cli .configFile )
230+ }
231+ cli .client , err = getClientWithPassword (passRetriever , newClient )
232+ }
233+ if err != nil {
234+ return err
210235 }
211- cli .client , err = getClientWithPassword (passRetriever , newClient )
212- }
213- if err != nil {
214- return err
215236 }
216237 var experimentalValue string
217238 // Environment variable always overrides configuration
0 commit comments