Browse code

Fix "command.PromptForConfirmation" to accept "enter" for the "N" default

This adjusts `command.PromptForConfirmation` in `cli/command/utils.go` to use `bufio`'s `ReadLine` rather than using `fmt.Fscan` for reading input, which makes `<Enter>` properly accept the default value of "No" as one would expect.

This new code actually came from `cli/command/plugin/install.go`'s `acceptPrivileges` function, which I've also refactored here to use `command.PromptForConfirmation` as it should.

Additionally, this updates `cli/command/plugin/upgrade.go`'s `runUpgrade` function to use `command.PromptForConfirmation` for further consistency.

Signed-off-by: Andrew "Tianon" Page <admwiggin@gmail.com>

Tianon Gravi authored on 2017/02/04 09:07:04
Showing 3 changed files
... ...
@@ -1,7 +1,6 @@
1 1
 package plugin
2 2
 
3 3
 import (
4
-	"bufio"
5 4
 	"errors"
6 5
 	"fmt"
7 6
 	"strings"
... ...
@@ -178,13 +177,6 @@ func acceptPrivileges(dockerCli *command.DockerCli, name string) func(privileges
178 178
 		for _, privilege := range privileges {
179 179
 			fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value)
180 180
 		}
181
-
182
-		fmt.Fprint(dockerCli.Out(), "Do you grant the above permissions? [y/N] ")
183
-		reader := bufio.NewReader(dockerCli.In())
184
-		line, _, err := reader.ReadLine()
185
-		if err != nil {
186
-			return false, err
187
-		}
188
-		return strings.ToLower(string(line)) == "y", nil
181
+		return command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), "Do you grant the above permissions?"), nil
189 182
 	}
190 183
 }
... ...
@@ -1,7 +1,6 @@
1 1
 package plugin
2 2
 
3 3
 import (
4
-	"bufio"
5 4
 	"context"
6 5
 	"fmt"
7 6
 	"strings"
... ...
@@ -64,17 +63,7 @@ func runUpgrade(dockerCli *command.DockerCli, opts pluginOptions) error {
64 64
 
65 65
 	fmt.Fprintf(dockerCli.Out(), "Upgrading plugin %s from %s to %s\n", p.Name, old, remote)
66 66
 	if !opts.skipRemoteCheck && remote.String() != old.String() {
67
-		_, err := fmt.Fprint(dockerCli.Out(), "Plugin images do not match, are you sure? ")
68
-		if err != nil {
69
-			return errors.Wrap(err, "error writing to stdout")
70
-		}
71
-
72
-		rdr := bufio.NewReader(dockerCli.In())
73
-		line, _, err := rdr.ReadLine()
74
-		if err != nil {
75
-			return errors.Wrap(err, "error reading from stdin")
76
-		}
77
-		if strings.ToLower(string(line)) != "y" {
67
+		if !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), "Plugin images do not match, are you sure?") {
78 68
 			return errors.New("canceling upgrade request")
79 69
 		}
80 70
 	}
... ...
@@ -1,6 +1,7 @@
1 1
 package command
2 2
 
3 3
 import (
4
+	"bufio"
4 5
 	"fmt"
5 6
 	"io"
6 7
 	"os"
... ...
@@ -80,11 +81,7 @@ func PromptForConfirmation(ins *InStream, outs *OutStream, message string) bool
80 80
 		ins = NewInStream(os.Stdin)
81 81
 	}
82 82
 
83
-	answer := ""
84
-	n, _ := fmt.Fscan(ins, &answer)
85
-	if n != 1 || (answer != "y" && answer != "Y") {
86
-		return false
87
-	}
88
-
89
-	return true
83
+	reader := bufio.NewReader(ins)
84
+	answer, _, _ := reader.ReadLine()
85
+	return strings.ToLower(string(answer)) == "y"
90 86
 }