Skip to content

Commit

Permalink
Merge pull request moby#30769 from tianon/prompt-for-confirmation
Browse files Browse the repository at this point in the history
Fix "command.PromptForConfirmation" to accept "enter" for the "N" default
  • Loading branch information
LK4D4 authored Feb 6, 2017
2 parents 00d2057 + 2198b05 commit d4136eb
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 28 deletions.
10 changes: 1 addition & 9 deletions cli/command/plugin/install.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package plugin

import (
"bufio"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -178,13 +177,6 @@ func acceptPrivileges(dockerCli *command.DockerCli, name string) func(privileges
for _, privilege := range privileges {
fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value)
}

fmt.Fprint(dockerCli.Out(), "Do you grant the above permissions? [y/N] ")
reader := bufio.NewReader(dockerCli.In())
line, _, err := reader.ReadLine()
if err != nil {
return false, err
}
return strings.ToLower(string(line)) == "y", nil
return command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), "Do you grant the above permissions?"), nil
}
}
13 changes: 1 addition & 12 deletions cli/command/plugin/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package plugin

import (
"bufio"
"context"
"fmt"
"strings"
Expand Down Expand Up @@ -64,17 +63,7 @@ func runUpgrade(dockerCli *command.DockerCli, opts pluginOptions) error {

fmt.Fprintf(dockerCli.Out(), "Upgrading plugin %s from %s to %s\n", p.Name, old, remote)
if !opts.skipRemoteCheck && remote.String() != old.String() {
_, err := fmt.Fprint(dockerCli.Out(), "Plugin images do not match, are you sure? ")
if err != nil {
return errors.Wrap(err, "error writing to stdout")
}

rdr := bufio.NewReader(dockerCli.In())
line, _, err := rdr.ReadLine()
if err != nil {
return errors.Wrap(err, "error reading from stdin")
}
if strings.ToLower(string(line)) != "y" {
if !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), "Plugin images do not match, are you sure?") {
return errors.New("canceling upgrade request")
}
}
Expand Down
11 changes: 4 additions & 7 deletions cli/command/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"bufio"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -80,11 +81,7 @@ func PromptForConfirmation(ins *InStream, outs *OutStream, message string) bool
ins = NewInStream(os.Stdin)
}

answer := ""
n, _ := fmt.Fscan(ins, &answer)
if n != 1 || (answer != "y" && answer != "Y") {
return false
}

return true
reader := bufio.NewReader(ins)
answer, _, _ := reader.ReadLine()
return strings.ToLower(string(answer)) == "y"
}

0 comments on commit d4136eb

Please sign in to comment.