docker / docker/cli

add method for reading APIClient initialization errors

Open
#4,489 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

kind/feature
Dominant language
Go
Stars
6.1k
Forks
2.2k
Avg merge
1d 15h
Merged PRs (30d)
43

Description

Description

Background

We use DockerCli as a library to create an APIClient. As far as I know, this is the only way to get an APIClient that's DOCKER_CONTEXT aware.

The Problem

If the APIClient fails to initialize, the library mysteriously exits.

https://github.com/docker/cli/blob/d1c04fb0f8db4a3c163f310652d19c65f6e2ff11/cli/command/cli.go#L106

func (cli *DockerCli) Client() client.APIClient {
	if err := cli.initialize(); err != nil {
		_, _ = fmt.Fprintf(cli.Err(), "Failed to initialize: %s\n", err)
		os.Exit(1)
	}
	return cli.client
}

This is a sad way to do error-handling:

  • It's usually considered bad form for libraries to call os.Exit
  • Err is a generic Writer, and you can't guarantee that the message will get flushed before the process terminates

Proposal

  • Mark Client() as deprecated
  • Add two new functions: ClientOrDie() and ClientOrError()
  • ClientOrDie has the same signature and behavior as ClientOrDie() today
  • ClientOrError returns the error to the caller

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at cli/command/cli.go, especially DockerCli.Client and the linked initialization path, to understand the current failure behavior. Define the new ClientOrDie and ClientOrError entry points and deprecate Client; done means callers can receive initialization errors instead of the library unconditionally terminating.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, cli
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.