Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better prompts and defaults when adding by URL and adding masterfiles via cfbs init #194

Merged
merged 7 commits into from
May 29, 2024
27 changes: 20 additions & 7 deletions cfbs/cfbs_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,43 @@ def _add_using_url(
remote_config = CFBSJson(path=config_path, url=url, url_commit=url_commit)

provides = remote_config.get_provides()
add_all = True
# URL specified in to_add, but no specific modules => let's add all (with a prompt)
if len(to_add) == 0:
modules = list(provides.values())
print("Found %d modules in '%s':" % (len(modules), url))
for m in modules:
print(" - " + m["name"])
if not any(modules):
user_error("no modules available, nothing to do")
if not self.non_interactive:
print("Found %d modules in '%s':" % (len(modules), url))
for m in modules:
deps = m.get("dependencies", [])
deps = "" if not deps else " (Depends on: " + ", ".join(deps) + ")"
print(" - " + m["name"] + deps)
if len(modules) > 1 and not self.non_interactive:
answer = prompt_user(
non_interactive=self.non_interactive,
prompt="Do you want to add all %d of them?" % (len(modules)),
choices=YES_NO_CHOICES,
default="no",
default="yes",
)
if answer.lower() not in ("y", "yes"):
return
add_all = False
else:
missing = [k for k in to_add if k not in provides]
if missing:
user_error("Missing modules: " + ", ".join(missing))
modules = [provides[k] for k in to_add]

for module in modules:
for i, module in enumerate(modules, start=1):
if not add_all:
answer = prompt_user(
non_interactive=self.non_interactive,
prompt="(%d/%d) Do you want to add '%s'?"
% (i, len(modules), module["name"]),
choices=YES_NO_CHOICES,
default="yes",
)
if answer.lower() not in ("y", "yes"):
continue
self.add_with_dependencies(module, remote_config)

@staticmethod
Expand Down
24 changes: 13 additions & 11 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,24 +240,26 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int:
CFBSConfig.reload()

branch = None
to_add = ""
to_add = []
if masterfiles is None:
if prompt_user(
non_interactive,
"Do you wish to build on top of the default policy set, masterfiles? (Recommended)",
choices=YES_NO_CHOICES,
default="yes",
) in ("yes", "y"):
to_add = "masterfiles"
to_add = ["masterfiles"]
else:
to_add = prompt_user(
non_interactive,
"Specify policy set to use instead (empty to skip)",
default="",
)
to_add = [
prompt_user(
non_interactive,
"Specify policy set to use instead (empty to skip)",
default="",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps an example response. I guess, but am not sure that I should provide a module repo URL and perhaps a module name, maybe a commit sha in the URL?

)
]
elif re.match(r"[0-9]+(\.[0-9]+){2}(\-[0-9]+)?", masterfiles):
log.debug("--masterfiles=%s appears to be a version number" % masterfiles)
to_add = "masterfiles@%s" % masterfiles
to_add = ["masterfiles@%s" % masterfiles]
elif masterfiles != "no":
"""This appears to be a branch. Thus we'll add masterfiles normally
and try to do the necessary modifications needed afterwards. I.e.
Expand All @@ -266,7 +268,7 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int:

log.debug("--masterfiles=%s appears to be a branch" % masterfiles)
branch = masterfiles
to_add = "masterfiles"
to_add = ["masterfiles"]

if branch is not None:
remote = "https://github.com/cfengine/masterfiles"
Expand All @@ -276,9 +278,9 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int:
"Failed to find branch or tag %s at remote %s" % (branch, remote)
)
log.debug("Current commit for masterfiles branch %s is %s" % (branch, commit))
to_add = "%s@%s" % (remote, commit)
to_add = ["%s@%s" % (remote, commit), "masterfiles"]
if to_add:
ret = add_command([to_add])
ret = add_command(to_add, added_by="cfbs init")
if ret != 0:
return ret

Expand Down
Loading