commands: handle ctrl-c on active prompt

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2024-02-27 17:30:51 -08:00
parent 85abcc413e
commit ce9a486a0e
3 changed files with 35 additions and 4 deletions

23
commands/util.go Normal file
View File

@ -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
}
}