Initialize the dockerCli runs initialization that must happen after command line flags are parsed.
(opts *cliflags.ClientOptions, ops ...CLIOption)
| 209 | // Initialize the dockerCli runs initialization that must happen after command |
| 210 | // line flags are parsed. |
| 211 | func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption) error { |
| 212 | for _, o := range ops { |
| 213 | if err := o(cli); err != nil { |
| 214 | return err |
| 215 | } |
| 216 | } |
| 217 | cliflags.SetLogLevel(opts.LogLevel) |
| 218 | |
| 219 | if opts.ConfigDir != "" { |
| 220 | config.SetDir(opts.ConfigDir) |
| 221 | } |
| 222 | |
| 223 | if opts.Debug { |
| 224 | debug.Enable() |
| 225 | } |
| 226 | if opts.Context != "" && len(opts.Hosts) > 0 { |
| 227 | return errors.New("conflicting options: cannot specify both --host and --context") |
| 228 | } |
| 229 | |
| 230 | if cli.contextStoreConfig == nil { |
| 231 | // This path can be hit when calling Initialize on a DockerCli that's |
| 232 | // not constructed through [NewDockerCli]. Using the default context |
| 233 | // store without a config set will result in Endpoints from contexts |
| 234 | // not being type-mapped correctly, and used as a generic "map[string]any", |
| 235 | // instead of a [docker.EndpointMeta]. |
| 236 | // |
| 237 | // When looking up the API endpoint (using [EndpointFromContext]), no |
| 238 | // endpoint will be found, and a default, empty endpoint will be used |
| 239 | // instead which in its turn, causes newAPIClientFromEndpoint to |
| 240 | // be initialized with the default config instead of settings for |
| 241 | // the current context (which may mean; connecting with the wrong |
| 242 | // endpoint and/or TLS Config to be missing). |
| 243 | // |
| 244 | // [EndpointFromContext]: https://github.com/docker/cli/blob/33494921b80fd0b5a06acc3a34fa288de4bb2e6b/cli/context/docker/load.go#L139-L149 |
| 245 | if err := WithDefaultContextStoreConfig()(cli); err != nil { |
| 246 | return err |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | cli.options = opts |
| 251 | cli.configFile = config.LoadDefaultConfigFile(cli.err) |
| 252 | cli.currentContext = resolveContextName(cli.options, cli.configFile) |
| 253 | cli.contextStore = &ContextStoreWithDefault{ |
| 254 | Store: store.New(config.ContextStoreDir(), *cli.contextStoreConfig), |
| 255 | Resolver: func() (*DefaultContext, error) { |
| 256 | return resolveDefaultContext(cli.options, *cli.contextStoreConfig) |
| 257 | }, |
| 258 | } |
| 259 | |
| 260 | // TODO(krissetto): pass ctx to the funcs instead of using this |
| 261 | if cli.enableGlobalMeter { |
| 262 | cli.createGlobalMeterProvider(cli.baseCtx) |
| 263 | } |
| 264 | if cli.enableGlobalTracer { |
| 265 | cli.createGlobalTracerProvider(cli.baseCtx) |
| 266 | } |
| 267 | filterResourceAttributesEnvvar() |
| 268 |