From 864eae0d6bd0732260c0c56583bb77f9b25b60f6 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 17 Feb 2019 16:12:06 +0100 Subject: identity: work on higher level now, cache, first two identity commands --- input/prompt.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 input/prompt.go (limited to 'input') diff --git a/input/prompt.go b/input/prompt.go new file mode 100644 index 00000000..7a059b1a --- /dev/null +++ b/input/prompt.go @@ -0,0 +1,44 @@ +package input + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +func PromptValue(name string, preValue string) (string, error) { + return promptValue(name, preValue, false) +} + +func PromptValueRequired(name string, preValue string) (string, error) { + return promptValue(name, preValue, true) +} + +func promptValue(name string, preValue string, required bool) (string, error) { + for { + if preValue != "" { + fmt.Printf("%s [%s]: ", name, preValue) + } else { + fmt.Printf("%s: ", name) + } + + line, err := bufio.NewReader(os.Stdin).ReadString('\n') + if err != nil { + return "", err + } + + line = strings.TrimSpace(line) + + if preValue != "" && line == "" { + return preValue, nil + } + + if required && line == "" { + fmt.Printf("%s is empty\n", name) + continue + } + + return line, nil + } +} -- cgit From b8caddddc7aaf34b2da61c590fd1d9a0fae024fb Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 23 Feb 2019 13:01:46 +0100 Subject: identity: some UX cleanup --- input/prompt.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'input') diff --git a/input/prompt.go b/input/prompt.go index 7a059b1a..6036c062 100644 --- a/input/prompt.go +++ b/input/prompt.go @@ -18,9 +18,9 @@ func PromptValueRequired(name string, preValue string) (string, error) { func promptValue(name string, preValue string, required bool) (string, error) { for { if preValue != "" { - fmt.Printf("%s [%s]: ", name, preValue) + _, _ = fmt.Fprintf(os.Stderr, "%s [%s]: ", name, preValue) } else { - fmt.Printf("%s: ", name) + _, _ = fmt.Fprintf(os.Stderr, "%s: ", name) } line, err := bufio.NewReader(os.Stdin).ReadString('\n') @@ -35,7 +35,7 @@ func promptValue(name string, preValue string, required bool) (string, error) { } if required && line == "" { - fmt.Printf("%s is empty\n", name) + _, _ = fmt.Fprintf(os.Stderr, "%s is empty\n", name) continue } -- cgit