Skip to content

Commit 47fb954

Browse files
committed
Added a helper function for yes-no question user prompts
Signed-off-by: jakub-nt <[email protected]>
1 parent 3648c3d commit 47fb954

File tree

5 files changed

+51
-77
lines changed

5 files changed

+51
-77
lines changed

cfbs/cfbs_config.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from cfbs.pretty import pretty, CFBS_DEFAULT_SORTING_RULES
3838
from cfbs.cfbs_json import CFBSJson
3939
from cfbs.module import Module, is_module_added_manually
40-
from cfbs.prompts import prompt_user, YES_NO_CHOICES
40+
from cfbs.prompts import prompt_user, prompt_user_yesno
4141
from cfbs.validate import validate_single_module
4242

4343

@@ -175,13 +175,10 @@ def _add_using_url(
175175
deps = "" if not deps else " (Depends on: " + ", ".join(deps) + ")"
176176
print(" - " + m["name"] + deps)
177177
if len(modules) > 1 and not self.non_interactive:
178-
answer = prompt_user(
178+
if not prompt_user_yesno(
179179
non_interactive=self.non_interactive,
180180
prompt="Do you want to add all %d of them?" % (len(modules)),
181-
choices=YES_NO_CHOICES,
182-
default="yes",
183-
)
184-
if answer.lower() not in ("y", "yes"):
181+
):
185182
add_all = False
186183
else:
187184
missing = [k for k in to_add if k not in provides]
@@ -191,14 +188,11 @@ def _add_using_url(
191188

192189
for i, module in enumerate(modules, start=1):
193190
if not add_all:
194-
answer = prompt_user(
191+
if not prompt_user_yesno(
195192
non_interactive=self.non_interactive,
196193
prompt="(%d/%d) Do you want to add '%s'?"
197194
% (i, len(modules), module["name"]),
198-
choices=YES_NO_CHOICES,
199-
default="yes",
200-
)
201-
if answer.lower() not in ("y", "yes"):
195+
):
202196
continue
203197
self.add_with_dependencies(module, remote_config, str_added_by=added_by)
204198

@@ -454,13 +448,12 @@ def add_command(
454448
+ "Please make sure to run 'cfbs input' to re-enter input "
455449
+ "before building and deploying/installing your project."
456450
)
457-
elif prompt_user(
451+
elif prompt_user_yesno(
458452
self.non_interactive,
459453
"The added module '%s' accepts user input. " % name
460454
+ "Do you want to add it now?",
461-
YES_NO_CHOICES,
462-
"no",
463-
).lower() in ("yes", "y"):
455+
default="no",
456+
):
464457
input_data = copy.deepcopy(module["input"])
465458
self.input_command(name, input_data)
466459
write_json(input_path, input_data)
@@ -515,12 +508,9 @@ def _input_list(input_data):
515508
result = []
516509

517510
result.append(_input_elements(subtype))
518-
while prompt_user(
519-
self.non_interactive,
520-
input_data["while"],
521-
choices=YES_NO_CHOICES,
522-
default="no",
523-
).lower() in ("yes", "y"):
511+
while prompt_user_yesno(
512+
self.non_interactive, input_data["while"], default="no"
513+
):
524514
result.append(_input_elements(subtype))
525515
return result
526516

@@ -532,12 +522,9 @@ def _input_list(input_data):
532522
% subtype["type"]
533523
)
534524
result = [_input_string(subtype)]
535-
while prompt_user(
536-
self.non_interactive,
537-
input_data["while"],
538-
choices=YES_NO_CHOICES,
539-
default="no",
540-
).lower() in ("yes", "y"):
525+
while prompt_user_yesno(
526+
self.non_interactive, input_data["while"], default="no"
527+
):
541528
result.append(_input_string(subtype))
542529
return result
543530
raise CFBSExitError(

cfbs/commands.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def search_command(terms: List[str]) -> int:
109109
ls_remote,
110110
)
111111
from cfbs.git_magic import Result, commit_after_command, git_commit_maybe_prompt
112-
from cfbs.prompts import YES_NO_CHOICES, prompt_user
112+
from cfbs.prompts import prompt_user, prompt_user_yesno
113113
from cfbs.module import Module, is_module_added_manually
114114
from cfbs.masterfiles.generate_release_information import generate_release_information
115115

@@ -199,20 +199,15 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int:
199199
is_git = is_git_repo()
200200
if do_git is None:
201201
if is_git:
202-
git_ans = prompt_user(
202+
do_git = prompt_user_yesno(
203203
non_interactive,
204204
"This is a git repository. Do you want cfbs to make commits to it?",
205-
choices=YES_NO_CHOICES,
206-
default="yes",
207205
)
208206
else:
209-
git_ans = prompt_user(
207+
do_git = prompt_user_yesno(
210208
non_interactive,
211209
"Do you want cfbs to initialize a git repository and make commits to it?",
212-
choices=YES_NO_CHOICES,
213-
default="yes",
214210
)
215-
do_git = git_ans.lower() in ("yes", "y")
216211
else:
217212
assert do_git in ("yes", "no")
218213
do_git = True if do_git == "yes" else False
@@ -287,12 +282,10 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int:
287282
branch = None
288283
to_add = []
289284
if masterfiles is None:
290-
if prompt_user(
285+
if prompt_user_yesno(
291286
non_interactive,
292287
"Do you wish to build on top of the default policy set, masterfiles? (Recommended)",
293-
choices=YES_NO_CHOICES,
294-
default="yes",
295-
) in ("yes", "y"):
288+
):
296289
to_add = ["masterfiles"]
297290
else:
298291
answer = prompt_user(
@@ -477,7 +470,7 @@ def _get_module_by_name(name) -> Union[dict, None]:
477470

478471
def _remove_module_user_prompt(module):
479472
dependents = _get_dependents(module["name"])
480-
return prompt_user(
473+
return prompt_user_yesno(
481474
config.non_interactive,
482475
"Do you wish to remove '%s'?" % module["name"]
483476
+ (
@@ -486,8 +479,6 @@ def _remove_module_user_prompt(module):
486479
if dependents
487480
else ""
488481
),
489-
choices=YES_NO_CHOICES,
490-
default="yes",
491482
)
492483

493484
def _get_modules_by_url(name) -> list:
@@ -506,31 +497,28 @@ def _get_modules_by_url(name) -> list:
506497
if not matches:
507498
raise CFBSExitError("Could not find module with URL '%s'" % name)
508499
for module in matches:
509-
answer = _remove_module_user_prompt(module)
510-
if answer.lower() in ("yes", "y"):
500+
if _remove_module_user_prompt(module):
511501
print("Removing module '%s'" % module["name"])
512502
modules.remove(module)
513503
msg += "\n - Removed module '%s'" % module["name"]
514504
num_removed += 1
515505
else:
516506
module = _get_module_by_name(name)
517507
if module:
518-
answer = _remove_module_user_prompt(module)
519-
if answer.lower() in ("yes", "y"):
508+
if _remove_module_user_prompt(module):
520509
print("Removing module '%s'" % name)
521510
modules.remove(module)
522511
msg += "\n - Removed module '%s'" % module["name"]
523512
num_removed += 1
524513
else:
525514
print("Module '%s' not found" % name)
526515
input_path = os.path.join(".", name, "input.json")
527-
if os.path.isfile(input_path) and prompt_user(
516+
if os.path.isfile(input_path) and prompt_user_yesno(
528517
config.non_interactive,
529518
"Module '%s' has input data '%s'. Do you want to remove it?"
530519
% (name, input_path),
531-
choices=YES_NO_CHOICES,
532520
default="no",
533-
).lower() in ("yes", "y"):
521+
):
534522
rm(input_path)
535523
files.append(input_path)
536524
msg += "\n - Removed input data for module '%s'" % name
@@ -594,13 +582,10 @@ def _someone_needs_me(this) -> bool:
594582
added_by = module["added_by"] if "added_by" in module else ""
595583
print("%s - %s - added by: %s" % (name, description, added_by))
596584

597-
answer = prompt_user(
585+
if prompt_user_yesno(
598586
config.non_interactive,
599587
"Do you wish to remove these modules?",
600-
choices=YES_NO_CHOICES,
601-
default="yes",
602-
)
603-
if answer.lower() in ("yes", "y"):
588+
):
604589
for module in to_remove:
605590
modules.remove(module)
606591
config.save()
@@ -1144,12 +1129,11 @@ def input_command(args, input_from="cfbs input") -> Result:
11441129

11451130
input_path = os.path.join(".", module_name, "input.json")
11461131
if os.path.isfile(input_path):
1147-
if prompt_user(
1132+
if not prompt_user_yesno(
11481133
config.non_interactive,
11491134
"Input already exists for this module, do you want to overwrite it?",
1150-
YES_NO_CHOICES,
1151-
"no",
1152-
).lower() in ("no", "n"):
1135+
default="no",
1136+
):
11531137
continue
11541138

11551139
input_data = copy.deepcopy(module["input"])

cfbs/git_magic.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
from typing import Iterable, Union
8-
from cfbs.prompts import YES_NO_CHOICES, prompt_user
8+
from cfbs.prompts import prompt_user_yesno
99
from cfbs.cfbs_config import CFBSConfig, CFBSReturnWithoutCommit
1010
from cfbs.git import (
1111
git_commit,
@@ -49,13 +49,7 @@ def git_commit_maybe_prompt(
4949
prompt += "\n" if line == "" else "\t" + line + "\n"
5050
prompt += "\nEdit it?"
5151

52-
ans = prompt_user(
53-
non_interactive,
54-
prompt,
55-
choices=YES_NO_CHOICES,
56-
default="no",
57-
)
58-
edit_commit_msg = ans.lower() in ("yes", "y")
52+
edit_commit_msg = prompt_user_yesno(non_interactive, prompt, default="no")
5953

6054
git_commit(
6155
commit_msg,

cfbs/prompts.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
YES_NO_CHOICES = ("yes", "y", "no", "n")
22

33

4-
def prompt_user(non_interactive, prompt, choices=None, default=None):
4+
def prompt_user(non_interactive: bool, prompt: str, choices=None, default=None):
55
if non_interactive:
66
if default is None:
77
raise ValueError(
@@ -38,3 +38,16 @@ def prompt_user(non_interactive, prompt, choices=None, default=None):
3838
answer = None
3939

4040
return answer
41+
42+
43+
def prompt_user_yesno(non_interactive: bool, prompt: str, default="yes"):
44+
"""Returns `True` if the answer is yes, and `False` otherwise."""
45+
46+
answer = prompt_user(
47+
non_interactive,
48+
prompt,
49+
choices=YES_NO_CHOICES,
50+
default=default,
51+
)
52+
53+
return answer.lower() in ("yes", "y")

cfbs/updates.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import logging as log
44

5-
from cfbs.prompts import YES_NO_CHOICES, prompt_user
5+
from cfbs.prompts import prompt_user_yesno
66
from cfbs.utils import read_json, CFBSExitError, write_json
77

88

@@ -134,16 +134,13 @@ def update_module(old_module, new_module, module_updates, update):
134134
if key == "steps":
135135
# same commit => user modifications, don't revert them
136136
if commit_differs:
137-
ans = prompt_user(
137+
if prompt_user_yesno(
138138
module_updates.config.non_interactive,
139139
"Module %s has different build steps now\n" % old_module["name"]
140140
+ "old steps: %s\n" % old_module["steps"]
141141
+ "new steps: %s\n" % new_module["steps"]
142142
+ "Do you want to use the new build steps?",
143-
choices=YES_NO_CHOICES,
144-
default="yes",
145-
)
146-
if ans.lower() in ["y", "yes"]:
143+
):
147144
old_module["steps"] = new_module["steps"]
148145
local_changes_made = True
149146
else:
@@ -167,14 +164,13 @@ def update_module(old_module, new_module, module_updates, update):
167164
local_changes_made |= update_input_data(old_module, input_data)
168165
except InputDataUpdateFailed as e:
169166
log.warning(e)
170-
if prompt_user(
167+
if not prompt_user_yesno(
171168
module_updates.config.non_interactive,
172169
"Input for module '%s' has changed " % old_module["name"]
173170
+ "and may no longer be compatible. "
174171
+ "Do you want to re-enter input now?",
175-
YES_NO_CHOICES,
176-
"no",
177-
).lower() in ("no", "n"):
172+
default="no",
173+
):
178174
continue
179175
input_data = copy.deepcopy(old_module["input"])
180176
module_updates.config.input_command(

0 commit comments

Comments
 (0)