diff --git a/pupa/cli/commands/clean.py b/pupa/cli/commands/clean.py index bc4dd83..f00fe51 100644 --- a/pupa/cli/commands/clean.py +++ b/pupa/cli/commands/clean.py @@ -44,6 +44,12 @@ def add_args(self): action="store_true", help="delete objects without getting user confirmation", ) + self.add_argument( + "--yes", + action="store_true", + help="assumes an answer of 'yes' to all interactive prompts", + default=False, + ) def get_stale_objects(self, window): """ @@ -88,15 +94,22 @@ def handle(self, args, other): ) self.report_stale_objects() else: - num_stale_objects = len(list(self.get_stale_objects(args.window))) + stale_objects = list(self.get_stale_objects(args.window)) + num_stale_objects = len(stale_objects) + + if args.noinput and args.yes: + self.remove_stale_objects(args.window) + sys.exit() if args.noinput: # Fail-safe to avoid deleting a large amount of objects # without explicit confimation if num_stale_objects > 10: print( - "This command would delete more than 10 objects." - "If you're sure, re-run without --noinput to provide confirmation." + f"This command would delete {num_stale_objects} objects: " + f"\n{stale_objects}" + "\nIf you're sure, re-run without --noinput to provide confirmation." + "\nOr re-run with --yes to assume a yes answer to all prompts." ) sys.exit(1) else: diff --git a/pupa/tests/clean/test_clean.py b/pupa/tests/clean/test_clean.py index d4494fc..d78df27 100644 --- a/pupa/tests/clean/test_clean.py +++ b/pupa/tests/clean/test_clean.py @@ -85,7 +85,7 @@ def test_clean_command(subparsers): # Call clean command Command(subparsers).handle( - argparse.Namespace(noinput=True, report=False, window=7), [] + argparse.Namespace(noinput=True, report=False, window=7, yes=False), [] ) expected_stale_objects = {stale_person, stale_membership} @@ -117,5 +117,5 @@ def test_clean_command_failsafe(subparsers): with pytest.raises(SystemExit): # Should trigger failsafe exist when deleting more than 10 objects Command(subparsers).handle( - argparse.Namespace(noinput=True, report=False, window=7), [] + argparse.Namespace(noinput=True, report=False, window=7, yes=False), [] )