Skip to content

Commit

Permalink
cmd/docker: don't print on status 130
Browse files Browse the repository at this point in the history
Signed-off-by: Alano Terblanche <[email protected]>
  • Loading branch information
Benehiko committed Jan 31, 2025
1 parent 9a2b872 commit 1cad6c0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cli/command/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
13 changes: 7 additions & 6 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != 0 {
os.Exit(0)
return
}

Expand All @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
})
}

0 comments on commit 1cad6c0

Please sign in to comment.