commit 5a9f995080736afad5d1d1496508bbe01b8c094b
parent c30d0c248d15aafba4bdc8616e216d813717bffb
Author: Sean Enck <sean@ttypty.com>
Date: Fri, 15 Jul 2022 18:40:52 -0400
moving some common items around
Diffstat:
2 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/cmd/lb/main.go b/cmd/lb/main.go
@@ -102,7 +102,7 @@ func main() {
}
var password string
if !options.Multi && !isPipe {
- input, err := internal.ConfirmInput()
+ input, err := internal.ConfirmInputsMatch("password")
if err != nil {
internal.Die("password input failed", err)
}
@@ -278,10 +278,9 @@ func main() {
}
func confirm(prompt string) bool {
- fmt.Printf("%s? (y/N) ", prompt)
- resp, err := internal.Stdin(true)
+ yesNo, err := internal.ConfirmYesNoPrompt(prompt)
if err != nil {
internal.Die("failed to get response", err)
}
- return resp == "Y" || resp == "y"
+ return yesNo
}
diff --git a/internal/utils.go b/internal/utils.go
@@ -137,24 +137,24 @@ func termEcho(on bool) {
}
}
-// ConfirmInput will get 2 inputs and confirm they are the same.
-func ConfirmInput() (string, error) {
+// ConfirmInputsMatch will get 2 inputs and confirm they are the same.
+func ConfirmInputsMatch(object string) (string, error) {
termEcho(false)
defer func() {
termEcho(true)
}()
- fmt.Printf("please enter password: ")
+ fmt.Printf("please enter %s: ", object)
first, err := Stdin(true)
if err != nil {
return "", err
}
- fmt.Printf("\nplease re-enter password: ")
+ fmt.Printf("\nplease re-enter %s: ", object)
second, err := Stdin(true)
if err != nil {
return "", err
}
if first != second {
- return "", NewLockboxError("passwords do NOT match")
+ return "", NewLockboxError(fmt.Sprintf("%s(s) do NOT match", object))
}
return first, nil
}
@@ -209,6 +209,16 @@ func PathExists(path string) bool {
return true
}
+// ConfirmYesNoPrompt will ask a yes/no question.
+func ConfirmYesNoPrompt(prompt string) (bool, error) {
+ fmt.Printf("%s? (y/N) ", prompt)
+ resp, err := Stdin(true)
+ if err != nil {
+ return false, err
+ }
+ return resp == "Y" || resp == "y", nil
+}
+
func getStdin(one bool) ([]byte, error) {
scanner := bufio.NewScanner(os.Stdin)
var b bytes.Buffer