diff --git a/README.md b/README.md index acce4b6..37d3f85 100644 --- a/README.md +++ b/README.md @@ -153,8 +153,11 @@ docker run -v "$(pwd)":/app devopshq/artifactory-cleanup artifactory-cleanup --l # Save the table summary in a file artifactory-cleanup --output=myfile.txt -# Save the summary in a Json file +# Save the summary in a json file artifactory-cleanup --output=myfile.txt --output-format=json + +# Save the summary in a json file and append the list of all removed artifacts +artifactory-cleanup --output=myfile.json --output-format json --output-artifacts ``` # Rules diff --git a/artifactory_cleanup/artifactorycleanup.py b/artifactory_cleanup/artifactorycleanup.py index af447b7..907fe70 100644 --- a/artifactory_cleanup/artifactorycleanup.py +++ b/artifactory_cleanup/artifactorycleanup.py @@ -1,12 +1,12 @@ from concurrent.futures import ThreadPoolExecutor from datetime import date -from typing import List, Iterator +from typing import List, Iterator, Optional from attr import dataclass from requests import Session from artifactory_cleanup.errors import ArtifactoryCleanupException -from artifactory_cleanup.rules.base import CleanupPolicy, ArtifactDict +from artifactory_cleanup.rules.base import ArtifactsList, CleanupPolicy, ArtifactDict @dataclass @@ -14,6 +14,7 @@ class CleanupSummary: policy_name: str artifacts_removed: int artifacts_size: int + removed_artifacts: ArtifactsList class ArtifactoryCleanup: @@ -38,7 +39,7 @@ def _init_policies(self, today): for policy in self.policies: policy.init(self.session, today) - def cleanup(self, block_ctx_mgr, test_ctx_mgr) -> Iterator[CleanupSummary]: + def cleanup(self, block_ctx_mgr, test_ctx_mgr) -> Iterator[Optional[CleanupSummary]]: for policy in self.policies: with block_ctx_mgr(policy.name): # Prepare @@ -72,12 +73,13 @@ def _delete(artifact): try: artifacts_size = sum([x["size"] for x in artifacts_to_remove]) print("Summary size: {}".format(artifacts_size)) - yield CleanupSummary( + summary = CleanupSummary( policy_name=policy.name, artifacts_size=artifacts_size, artifacts_removed=len(artifacts_to_remove), + removed_artifacts=artifacts_to_remove ) - + yield summary except KeyError: print("Summary size not defined") yield None diff --git a/artifactory_cleanup/cli.py b/artifactory_cleanup/cli.py index 5ad6f3a..999744b 100644 --- a/artifactory_cleanup/cli.py +++ b/artifactory_cleanup/cli.py @@ -14,12 +14,12 @@ ArtifactoryCleanup, ) from artifactory_cleanup.base_url_session import BaseUrlSession +from artifactory_cleanup.context_managers import get_context_managers from artifactory_cleanup.errors import InvalidConfigError from artifactory_cleanup.loaders import ( PythonLoader, YamlConfigLoader, ) -from artifactory_cleanup.context_managers import get_context_managers requests.packages.urllib3.disable_warnings() @@ -95,6 +95,13 @@ class ArtifactoryCleanupCLI(cli.Application): mandatory=False, ) + _output_artifacts = cli.Flag( + "--output-artifacts", + help="When --output-format is json, append the list of all removed artifacts to output", + mandatory=False, + default=False, + ) + @property def VERSION(self): # To prevent circular imports @@ -137,13 +144,13 @@ def _print_table(self, result: dict): print(self._format_table(result)) def _create_output_file(self, result, filename, format): - text = None + text = "" if format == "table": text = self._format_table(result).get_string() - else: - text = json.dumps(result) + elif format == "json": + text = json.dumps(result, indent=4) - with open(filename, "w") as file: + with open(filename, "w", encoding="utf-8") as file: file.write(text) def main(self): @@ -189,14 +196,14 @@ def main(self): continue total_size += summary.artifacts_size - result["policies"].append( - { - "name": summary.policy_name, - "file_count": summary.artifacts_removed, - "size": summary.artifacts_size, - } - ) - + policy = { + "name": summary.policy_name, + "file_count": summary.artifacts_removed, + "size": summary.artifacts_size + } + if self._output_artifacts: + policy["removed_artifacts"] = summary.removed_artifacts + result["policies"].append(policy) result["total_size"] = total_size self._print_table(result)