aboutsummaryrefslogtreecommitdiff
path: root/cmd/fscrypt/prompt.go
diff options
context:
space:
mode:
authorJoseph Richey <joerichey@google.com>2017-07-17 15:12:28 -0700
committerGitHub <noreply@github.com>2017-07-17 15:12:28 -0700
commit6f32bbc8bf51d615ef23ed37aa40910ec23cd587 (patch)
treed97df4ac65a22333570a2b40098b721785574c0d /cmd/fscrypt/prompt.go
parent4d6827a49491e941ea535178789b29e4c6b6d51f (diff)
parentf03dc9e84eea127f0efc3db21375abbafa78ce59 (diff)
Merge pull request #20 from google/fix
Refactor ReadLine functions
Diffstat (limited to 'cmd/fscrypt/prompt.go')
-rw-r--r--cmd/fscrypt/prompt.go36
1 files changed, 14 insertions, 22 deletions
diff --git a/cmd/fscrypt/prompt.go b/cmd/fscrypt/prompt.go
index 1b02280..fdbef81 100644
--- a/cmd/fscrypt/prompt.go
+++ b/cmd/fscrypt/prompt.go
@@ -20,7 +20,6 @@
package main
import (
- "bufio"
"fmt"
"log"
"os"
@@ -30,6 +29,7 @@ import (
"github.com/google/fscrypt/actions"
"github.com/google/fscrypt/metadata"
+ "github.com/google/fscrypt/util"
)
const (
@@ -45,17 +45,6 @@ var sourceDescriptions = map[metadata.SourceType]string{
metadata.SourceType_raw_key: "A raw 256-bit key",
}
-// promptUser presents a message to the user and returns their input string. An
-// error is returned if our read from standard input fails.
-func promptUser(prompt string) (string, error) {
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Print(prompt)
- if !scanner.Scan() {
- return "", ErrReadingStdin
- }
- return scanner.Text(), nil
-}
-
// askQuestion asks the user a yes or no question. Returning a boolean on a
// successful answer and an error if there was not a response from the user.
// Returns the defaultChoice on empty input (or in quiet mode).
@@ -65,14 +54,14 @@ func askQuestion(question string, defaultChoice bool) (bool, error) {
return defaultChoice, nil
}
// Loop until failure or valid input
- var input string
- var err error
for {
if defaultChoice {
- input, err = promptUser(question + defaultYesSuffix)
+ fmt.Print(question + defaultYesSuffix)
} else {
- input, err = promptUser(question + defaultNoSuffix)
+ fmt.Print(question + defaultNoSuffix)
}
+
+ input, err := util.ReadLine()
if err != nil {
return false, err
}
@@ -154,7 +143,8 @@ func promptForName(ctx *actions.Context) (string, error) {
}
for {
- name, err := promptUser("Enter a name for the new protector: ")
+ fmt.Print("Enter a name for the new protector: ")
+ name, err := util.ReadLine()
if err != nil {
return "", err
}
@@ -190,10 +180,10 @@ func promptForSource(ctx *actions.Context) error {
fmt.Printf("%d - %s (%s)\n", idx, description, source)
}
- prompt := fmt.Sprintf("Enter the source number for the new protector [%d - %s]: ",
- ctx.Config.Source, ctx.Config.Source)
for {
- input, err := promptUser(prompt)
+ fmt.Printf("Enter the source number for the new protector [%d - %s]: ",
+ ctx.Config.Source, ctx.Config.Source)
+ input, err := util.ReadLine()
if err != nil {
return err
}
@@ -225,7 +215,8 @@ func promptForKeyFile(prompt string) (*os.File, error) {
// Prompt for a valid path until we get a file we can open.
for {
- filename, err := promptUser(prompt)
+ fmt.Print(prompt)
+ filename, err := util.ReadLine()
if err != nil {
return nil, err
}
@@ -283,7 +274,8 @@ func promptForProtector(options []*actions.ProtectorOption) (int, error) {
}
for {
- input, err := promptUser("Enter the number of protector to use: ")
+ fmt.Print("Enter the number of protector to use: ")
+ input, err := util.ReadLine()
if err != nil {
return 0, err
}