diff --git a/commands/prune.go b/commands/prune.go index 1a2a5220c02d..e282172128aa 100644 --- a/commands/prune.go +++ b/commands/prune.go @@ -49,8 +49,12 @@ func runPrune(ctx context.Context, dockerCli command.Cli, opts pruneOptions) err warning = allCacheWarning } - if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) { - return nil + if !opts.force { + if ok, err := prompt(ctx, dockerCli.In(), dockerCli.Out(), warning); err != nil { + return err + } else if !ok { + return nil + } } b, err := builder.New(dockerCli, builder.WithName(opts.builder)) diff --git a/commands/rm.go b/commands/rm.go index 081cdb781cb9..6987cc426159 100644 --- a/commands/rm.go +++ b/commands/rm.go @@ -28,8 +28,12 @@ const ( ) func runRm(ctx context.Context, dockerCli command.Cli, in rmOptions) error { - if in.allInactive && !in.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), rmInactiveWarning) { - return nil + if in.allInactive && !in.force { + if ok, err := prompt(ctx, dockerCli.In(), dockerCli.Out(), rmInactiveWarning); err != nil { + return err + } else if !ok { + return nil + } } txn, release, err := storeutil.GetStore(dockerCli) diff --git a/commands/util.go b/commands/util.go new file mode 100644 index 000000000000..d7e44f50bc09 --- /dev/null +++ b/commands/util.go @@ -0,0 +1,23 @@ +package commands + +import ( + "context" + "io" + + "github.com/docker/cli/cli/command" +) + +func prompt(ctx context.Context, ins io.Reader, out io.Writer, msg string) (bool, error) { + done := make(chan struct{}) + var ok bool + go func() { + ok = command.PromptForConfirmation(ins, out, msg) + close(done) + }() + select { + case <-ctx.Done(): + return false, context.Cause(ctx) + case <-done: + return ok, nil + } +}