Skip to content

Commit 9d355a0

Browse files
committed
MB-66604 Refactor confirmation prompts into a function
Previously we duplicated the logic, allowing different strings for yes/no in different subcommands. Testing ------- Tried all commands that prompt for confirmation, except `reset-cipher-suites` as it does not work with cluster_run (raised a ticket for this) Change-Id: I6a6a636cae2570bc5fe62e10caf6b63ec345ae05 Reviewed-on: https://review.couchbase.org/c/couchbase-cli/+/227956 Tested-by: Build Bot <build@couchbase.com> Reviewed-by: Lubo Marinski <lubo.marinski@couchbase.com>
1 parent 8b7ce2d commit 9d355a0

1 file changed

Lines changed: 38 additions & 17 deletions

File tree

cbmgr.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,33 @@ def append_port(node):
491491
return [append_port(x) for x in nodes]
492492

493493

494+
def prompt_for_confirmation(question, default=None):
495+
confirm = 'Are you sure? '
496+
if default is None:
497+
confirm += 'Y/N'
498+
elif default:
499+
confirm += '[Y]/N'
500+
else:
501+
confirm += 'Y/[N]'
502+
503+
while True:
504+
answer = input(question + ' ' + confirm + ' ')
505+
if not answer:
506+
if default is None:
507+
continue
508+
return default
509+
510+
if answer.lower() in ('y', 'yes'):
511+
return True
512+
513+
if answer.lower() in ('n', 'no'):
514+
return False
515+
516+
print(f'Unrecognised option "{answer}"')
517+
if confirm not in ('y', 'Y', 'yes', 'Yes'):
518+
return
519+
520+
494521
class CLIHelpFormatter(HelpFormatter):
495522
"""Format help with indented section bodies"""
496523

@@ -1534,10 +1561,8 @@ def execute(self, opts):
15341561
_exit_if_errors(errors)
15351562

15361563
if not opts.force:
1537-
question = "Running this command will totally PURGE database data from disk. " + \
1538-
"Do you really want to do it? (Yes/No)"
1539-
confirm = input(question)
1540-
if confirm not in ('y', 'Y', 'yes', 'Yes'):
1564+
confirm = prompt_for_confirmation("Running this command will totally PURGE database data from disk.")
1565+
if not confirm:
15411566
return
15421567

15431568
_, errors = self.rest.flush_bucket(opts.bucket_name)
@@ -1940,8 +1965,8 @@ def execute(self, opts):
19401965
check_versions(rest)
19411966

19421967
if not opts.force:
1943-
confirm = str(input("Are you sure that the cipher should be reset?: Y/[N]"))
1944-
if confirm != "Y":
1968+
confirm = prompt_for_confirmation("This command will reset the cipher.", default=False)
1969+
if not confirm:
19451970
_success("Cipher suites have not been reset to default")
19461971

19471972
_, errors = rest.reset_cipher_suites()
@@ -2119,12 +2144,10 @@ def __init__(self):
21192144
@rest_initialiser(cluster_init_check=True)
21202145
def execute(self, opts):
21212146
if not opts.force:
2122-
confirm = input('This command will purge all data on this node.\nAre you sure? [y/n]: ')
2123-
if confirm == 'n':
2147+
confirm = prompt_for_confirmation('This command will purge all data on this node.')
2148+
if not confirm:
21242149
print("Node has not been reset")
21252150
sys.exit(0)
2126-
elif confirm != 'y':
2127-
_exit_if_errors(["Unknown option provided"])
21282151

21292152
_, errors = self.rest.reset_node()
21302153
_exit_if_errors(errors)
@@ -5963,17 +5986,15 @@ def execute(self, opts):
59635986
_exit_if_errors(['cannot provide both --enable and --list'])
59645987

59655988
if opts.enable:
5966-
confirm = input('Developer preview cannot be disabled once it is enabled. '
5967-
'If you enter developer preview mode you will not be able to '
5968-
'upgrade. DO NOT USE IN PRODUCTION.\nAre you sure [y/n]: ')
5969-
if confirm == 'y':
5989+
confirm = prompt_for_confirmation('Developer preview cannot be disabled once it is enabled. '
5990+
'If you enter developer preview mode you will not be able to '
5991+
'upgrade. DO NOT USE IN PRODUCTION.')
5992+
if confirm:
59705993
_, errors = self.rest.set_dp_mode()
59715994
_exit_if_errors(errors)
59725995
_success("Cluster is in developer preview mode")
5973-
elif confirm == 'n':
5974-
_success("Developer preview mode has NOT been enabled")
59755996
else:
5976-
_exit_if_errors(["Unknown option provided"])
5997+
_success("Developer preview mode has NOT been enabled")
59775998

59785999
if opts.list:
59796000
pools, rv = self.rest.pools()

0 commit comments

Comments
 (0)