1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
From: Nicolas Peugnet <nicolas@club1.fr>
Date: Tue, 4 Feb 2025 00:15:13 +0100
Subject: Add promptForConfirmation removed from docker-cli
See: https://github.com/docker/buildx/commit/adb9bc86e51c54924e1c6ee561e1bee3eaa56e5e
---
commands/util.go | 38 ++++++++++++++++++++++++++++++++++++--
1 file changed, 36 insertions(+), 2 deletions(-)
diff --git a/commands/util.go b/commands/util.go
index d7e44f5..3bde9b7 100644
--- a/commands/util.go
+++ b/commands/util.go
@@ -1,17 +1,22 @@
package commands
import (
+ "bufio"
"context"
+ "fmt"
"io"
+ "os"
+ "runtime"
+ "strings"
- "github.com/docker/cli/cli/command"
+ "github.com/docker/cli/cli/streams"
)
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)
+ ok = promptForConfirmation(ins, out, msg)
close(done)
}()
select {
@@ -21,3 +26,32 @@ func prompt(ctx context.Context, ins io.Reader, out io.Writer, msg string) (bool
return ok, nil
}
}
+
+// promptForConfirmation requests and checks confirmation from user.
+// This will display the provided message followed by ' [y/N] '. If
+// the user input 'y' or 'Y' it returns true other false. If no
+// message is provided "Are you sure you want to proceed? [y/N] "
+// will be used instead.
+//
+// Copied from github.com/docker/cli since the upstream version changed
+// recently with an incompatible change.
+//
+// See https://github.com/docker/buildx/pull/2359#discussion_r1544736494
+// for discussion on the issue.
+func promptForConfirmation(ins io.Reader, outs io.Writer, message string) bool {
+ if message == "" {
+ message = "Are you sure you want to proceed?"
+ }
+ message += " [y/N] "
+
+ _, _ = fmt.Fprint(outs, message)
+
+ // On Windows, force the use of the regular OS stdin stream.
+ if runtime.GOOS == "windows" {
+ ins = streams.NewIn(os.Stdin)
+ }
+
+ reader := bufio.NewReader(ins)
+ answer, _, _ := reader.ReadLine()
+ return strings.ToLower(string(answer)) == "y"
+}
|