From 7d1783d32bc72ee3fc659b02e0eeeb3acfecefd4 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 4 Jul 2024 16:20:24 +0200 Subject: [PATCH] Revert "cmd/docker: split handling exit-code to a separate utility" This reverts commit 4c62b2a29b510654b0518b0633f2ba1d5a3b14ac. Signed-off-by: Sebastiaan van Stijn --- cmd/docker/docker.go | 48 +++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index f24d580572d0..7825c7d35455 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -29,41 +29,43 @@ import ( ) func main() { - err := dockerMain(context.Background()) - if err != nil && !errdefs.IsCancelled(err) { - _, _ = fmt.Fprintln(os.Stderr, err) - os.Exit(getExitCode(err)) + statusCode := dockerMain() + if statusCode != 0 { + os.Exit(statusCode) } } -func dockerMain(ctx context.Context) error { - ctx, cancelNotify := signal.NotifyContext(ctx, platformsignals.TerminationSignals...) +func dockerMain() int { + ctx, cancelNotify := signal.NotifyContext(context.Background(), platformsignals.TerminationSignals...) defer cancelNotify() dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx)) if err != nil { - return err + fmt.Fprintln(os.Stderr, err) + return 1 } logrus.SetOutput(dockerCli.Err()) otel.SetErrorHandler(debug.OTELErrorHandler) - return runDocker(ctx, dockerCli) -} - -// getExitCode returns the exit-code to use for the given error. -// If err is a [cli.StatusError] and has a StatusCode set, it uses the -// status-code from it, otherwise it returns "1" for any error. -func getExitCode(err error) int { - if err == nil { - return 0 - } - var stErr cli.StatusError - if errors.As(err, &stErr) && stErr.StatusCode != 0 { - return stErr.StatusCode + if err := runDocker(ctx, dockerCli); err != nil { + if sterr, ok := err.(cli.StatusError); ok { + if sterr.Status != "" { + fmt.Fprintln(dockerCli.Err(), sterr.Status) + } + // StatusError should only be used for errors, and all errors should + // have a non-zero exit status, so never exit with 0 + if sterr.StatusCode == 0 { + return 1 + } + return sterr.StatusCode + } + if errdefs.IsCancelled(err) { + return 0 + } + fmt.Fprintln(dockerCli.Err(), err) + return 1 } - - // No status-code provided; all errors should have a non-zero exit code. - return 1 + return 0 } func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {