Skip to content

Commit

Permalink
Add support workdir-root for clean command
Browse files Browse the repository at this point in the history
  • Loading branch information
skycastlelily committed Aug 22, 2024
1 parent f3158e5 commit 8c8da09
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions tmt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1880,12 +1880,14 @@ def status(
@option(
'-s', '--skip', choices=CLEAN_RESOURCES,
help='The resources which should be kept on the disk.', multiple=True)
@workdir_root_options
@verbosity_options
@dry_options
def clean(context: Context,
last: bool,
id_: tuple[str, ...],
skip: list[str],
workdir_root: str,
**kwargs: Any) -> None:
"""
Clean workdirs, guests or images.
Expand All @@ -1903,6 +1905,9 @@ def clean(context: Context,
raise tmt.utils.GeneralError(
"Options --last and --id cannot be used together.")

if workdir_root and not Path(workdir_root).exists():
raise tmt.utils.GeneralError(f"Path '{workdir_root}' doesn't exist.")

context.obj.clean_logger = context.obj.logger \
.descend(logger_name='clean', extra_shift=0) \
.apply_verbosity_options(**kwargs)
Expand All @@ -1917,8 +1922,6 @@ def clean(context: Context,
if context.invoked_subcommand is None:
assert context.obj.clean_logger is not None # narrow type

# Set path to default
context.params['workdir_root'] = tmt.utils.WORKDIR_ROOT
# Create another level to the hierarchy so that logging indent is
# consistent between the command and subcommands
clean_obj = tmt.Clean(
Expand All @@ -1927,14 +1930,16 @@ def clean(context: Context,
.apply_verbosity_options(**kwargs),
parent=clean_obj,
cli_invocation=CliInvocation.from_context(context))
if tmt.utils.WORKDIR_ROOT.exists():
root_path = effective_workdir_root(workdir_root)
clean_obj.workdir_root = root_path
if root_path.exists():
if 'guests' not in skip and not clean_obj.guests():
exit_code = 1
if 'runs' not in skip and not clean_obj.runs(id_):
exit_code = 1
else:
clean_obj.warn(
f"Directory '{tmt.utils.WORKDIR_ROOT}' does not exist, "
f"Directory '{root_path}' does not exist, "
f"skipping guest and run cleanup.")
if 'images' not in skip and not clean_obj.images():
exit_code = 1
Expand Down

0 comments on commit 8c8da09

Please sign in to comment.