From 26d3a00b21930cf12c2bcd0fe42249821a2ad231 Mon Sep 17 00:00:00 2001
From: Tamer Tas <contact@tmrts.com>
Date: Sat, 12 Dec 2015 15:22:58 +0200
Subject: [PATCH] Add input scanning utilities

---
 pkg/util/inpututil/scan.go | 56 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 pkg/util/inpututil/scan.go

diff --git a/pkg/util/inpututil/scan.go b/pkg/util/inpututil/scan.go
new file mode 100644
index 0000000..cbce8f5
--- /dev/null
+++ b/pkg/util/inpututil/scan.go
@@ -0,0 +1,56 @@
+package inpututil
+
+import (
+	"fmt"
+	"strings"
+)
+
+var (
+	booleanValues = map[string]bool{
+		"y":   true,
+		"yes": true,
+		"yup": true,
+
+		"n":    false,
+		"no":   false,
+		"nope": false,
+	}
+)
+
+func ScanYesOrNo(msg string, def bool) (bool, error) {
+	var choices string
+	if def {
+		choices = "Y/n"
+	} else {
+		choices = "y/N"
+	}
+
+	fmt.Print(msg, " ", choices, " ")
+
+	var choice string
+	_, err := fmt.Scanf("%s", &choice)
+	if err != nil {
+		return def, err
+	}
+
+	switch val, ok := booleanValues[strings.ToLower(choice)]; {
+	case ok:
+		return val, nil
+	case choice == "":
+		return def, nil
+	}
+
+	return def, fmt.Errorf("unrecognized choice %#q", choice)
+}
+
+func ScanValue(msg string) (string, error) {
+	fmt.Print(msg)
+
+	var value string
+	_, err := fmt.Scanf("%s", &value)
+	if err != nil {
+		return "", err
+	}
+
+	return value, nil
+}
-- 
GitLab