mirror of https://github.com/docker/cli.git
cmd/docker: split handling exit-code to a separate utility
This allows dockerMain() to return an error "as usual", and puts the responsibility for turning that into an appropriate exit-code in main() (which also sets the exit-code when terminating). We could consider putting this utility in the cli package and exporting it if would be useful for doing a similar handling in plugins. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
b7695d6c79
commit
eae75092a0
|
@ -29,42 +29,41 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
statusCode := dockerMain()
|
err := dockerMain(context.Background())
|
||||||
if statusCode != 0 {
|
if err != nil && !errdefs.IsCancelled(err) {
|
||||||
os.Exit(statusCode)
|
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(getExitCode(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dockerMain() int {
|
func dockerMain(ctx context.Context) error {
|
||||||
ctx, cancelNotify := signal.NotifyContext(context.Background(), platformsignals.TerminationSignals...)
|
ctx, cancelNotify := signal.NotifyContext(ctx, platformsignals.TerminationSignals...)
|
||||||
defer cancelNotify()
|
defer cancelNotify()
|
||||||
|
|
||||||
dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx))
|
dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
return err
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
logrus.SetOutput(dockerCli.Err())
|
logrus.SetOutput(dockerCli.Err())
|
||||||
otel.SetErrorHandler(debug.OTELErrorHandler)
|
otel.SetErrorHandler(debug.OTELErrorHandler)
|
||||||
|
|
||||||
if err := runDocker(ctx, dockerCli); err != nil {
|
return runDocker(ctx, dockerCli)
|
||||||
var stErr cli.StatusError
|
}
|
||||||
if errors.As(err, &stErr) {
|
|
||||||
// StatusError should only be used for errors, and all errors should
|
// getExitCode returns the exit-code to use for the given error.
|
||||||
// have a non-zero exit status, so never exit with 0
|
// If err is a [cli.StatusError] and has a StatusCode set, it uses the
|
||||||
if stErr.StatusCode == 0 { // FIXME(thaJeztah): StatusCode should never be used with a zero status-code. Check if we do this anywhere.
|
// status-code from it, otherwise it returns "1" for any error.
|
||||||
stErr.StatusCode = 1
|
func getExitCode(err error) int {
|
||||||
}
|
if err == nil {
|
||||||
_, _ = fmt.Fprintln(dockerCli.Err(), stErr)
|
return 0
|
||||||
return stErr.StatusCode
|
|
||||||
}
|
|
||||||
if errdefs.IsCancelled(err) {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
fmt.Fprintln(dockerCli.Err(), err)
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
return 0
|
var stErr cli.StatusError
|
||||||
|
if errors.As(err, &stErr) && stErr.StatusCode != 0 { // FIXME(thaJeztah): StatusCode should never be used with a zero status-code. Check if we do this anywhere.
|
||||||
|
return stErr.StatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
// No status-code provided; all errors should have a non-zero exit code.
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
|
func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
|
||||||
|
|
Loading…
Reference in New Issue