From da5701e05dda1e3a7bff8566aa16ddd3366f74ff Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:10:27 +0100 Subject: [PATCH] cmd/docker: don't print on status 130 Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> --- cli/command/container/run.go | 4 ++++ cmd/docker/docker.go | 13 +++++++------ cmd/docker/docker_test.go | 6 +++++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 5ef3e8b7bf7b..3d7cf16ef88a 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -319,6 +319,10 @@ func withHelp(err error, commandName string) error { // - 127: if container start fails with 'not found'/'no such' error func toStatusError(err error) error { // TODO(thaJeztah): some of these errors originate from the container: should we actually suggest "--help" for those? + var statusError cli.StatusError + if errors.As(err, &statusError) { + return err + } errMsg := err.Error() diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index aa7a2b578685..bff516167bff 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -28,14 +28,12 @@ import ( "go.opentelemetry.io/otel" ) -var errCtxUserTerminated = errors.New("user terminated the process") - func main() { ctx := context.Background() err := dockerMain(ctx) - if errors.Is(err, errCtxUserTerminated) { - os.Exit(130) + if code := getExitCode(err); code == 130 { + os.Exit(0) return } @@ -56,8 +54,11 @@ func notifyContext(ctx context.Context, signals ...os.Signal) (context.Context, case <-ctx.Done(): signal.Stop(ch) return - case <-ch: - cancel(errCtxUserTerminated) + case sig := <-ch: + cancel(cli.StatusError{ + Status: "user terminated", + StatusCode: 128 + int(sig.(syscall.Signal)), + }) signal.Stop(ch) return } diff --git a/cmd/docker/docker_test.go b/cmd/docker/docker_test.go index 9c7eb766d537..c08d049516ec 100644 --- a/cmd/docker/docker_test.go +++ b/cmd/docker/docker_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/debug" platformsignals "github.com/docker/cli/cmd/docker/internal/signals" @@ -90,5 +91,8 @@ func TestUserTerminatedError(t *testing.T) { syscall.Kill(syscall.Getpid(), syscall.SIGINT) <-notifyCtx.Done() - assert.ErrorIs(t, context.Cause(notifyCtx), errCtxUserTerminated) + assert.ErrorIs(t, context.Cause(notifyCtx), cli.StatusError{ + Status: "user terminated", + StatusCode: 130, + }) }