]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume util create prompt helpers
authorAlfredo Deza <adeza@redhat.com>
Mon, 16 Jul 2018 13:22:50 +0000 (09:22 -0400)
committerAlfredo Deza <adeza@redhat.com>
Tue, 17 Jul 2018 18:43:00 +0000 (14:43 -0400)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
src/ceph-volume/ceph_volume/util/__init__.py

index 1da2a3da6a34fe811408c483b7cb34908f67e00c..0222d56a0791f44c91591ad9f420341fce9e16d9 100644 (file)
@@ -1,5 +1,6 @@
 import logging
 from math import floor
+from ceph_volume import terminal
 
 logger = logging.getLogger(__name__)
 
@@ -32,3 +33,42 @@ def str_to_int(string, round_down=True):
     else:
         integer = round(integer)
     return int(integer)
+
+
+def str_to_bool(val):
+    """
+    Convert a string representation of truth to True or False
+
+    True values are 'y', 'yes', or ''; case-insensitive
+    False values are 'n', or 'no'; case-insensitive
+    Raises ValueError if 'val' is anything else.
+    """
+    true_vals = ['yes', 'y', '']
+    false_vals = ['no', 'n']
+    try:
+        val = val.lower()
+    except AttributeError:
+        val = str(val).lower()
+    if val in true_vals:
+        return True
+    elif val in false_vals:
+        return False
+    else:
+        raise ValueError("Invalid input value: %s" % val)
+
+
+def prompt_bool(question, _raw_input=None):
+    """
+    Interface to prompt a boolean (or boolean-like) response from a user.
+    Usually a confirmation.
+    """
+    input_prompt = _raw_input or raw_input
+    prompt_format = '--> {question} '.format(question=question)
+    response = input_prompt(prompt_format)
+    try:
+        return str_to_bool(response)
+    except ValueError:
+        terminal.error('Valid true responses are: y, yes, <Enter>')
+        terminal.error('Valid false responses are: n, no')
+        terminal.error('That response was invalid, please try again')
+        return prompt_bool(question, _raw_input=input_prompt)