From ddcdadaf194b42c6f27d8f6ff69b0bea7de73ff7 Mon Sep 17 00:00:00 2001 From: kygr Date: Mon, 19 Aug 2024 10:26:04 +0200 Subject: [PATCH] fixup! feat(output): save list of deleted artifacts as json --- README.md | 4 ++-- artifactory_cleanup/artifactorycleanup.py | 9 ++------- artifactory_cleanup/cli.py | 15 +++++++-------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 9e6e6ba..37d3f85 100644 --- a/README.md +++ b/README.md @@ -153,10 +153,10 @@ 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 a list with removed artifacts +# 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 ``` diff --git a/artifactory_cleanup/artifactorycleanup.py b/artifactory_cleanup/artifactorycleanup.py index a1d46dd..e1c8c74 100644 --- a/artifactory_cleanup/artifactorycleanup.py +++ b/artifactory_cleanup/artifactorycleanup.py @@ -14,7 +14,7 @@ class CleanupSummary: policy_name: str artifacts_removed: int artifacts_size: int - removed_artifacts_list: Optional[ArtifactsList] = None + artifacts: ArtifactsList class ArtifactoryCleanup: @@ -26,16 +26,12 @@ def __init__( today: date, ignore_not_found: bool, worker_count: int, - output_format: str, - output_artifacts: bool, ): self.session = session self.policies = policies self.destroy = destroy self.ignore_not_found = ignore_not_found self.worker_count = worker_count - self.output_format = output_format - self.output_artifacts = output_artifacts, self._init_policies(today) @@ -81,9 +77,8 @@ def _delete(artifact): policy_name=policy.name, artifacts_size=artifacts_size, artifacts_removed=len(artifacts_to_remove), + artifacts=artifacts_to_remove ) - if self.output_format == "json" and self.output_artifacts: - summary.removed_artifacts_list = artifacts_to_remove yield summary except KeyError: print("Summary size not defined") diff --git a/artifactory_cleanup/cli.py b/artifactory_cleanup/cli.py index 719cc72..0a625f9 100644 --- a/artifactory_cleanup/cli.py +++ b/artifactory_cleanup/cli.py @@ -96,7 +96,8 @@ class ArtifactoryCleanupCLI(cli.Application): ) _output_artifacts = cli.Flag( - "--output-artifacts", help="When --output-format is json, append the list of all deleted artifacts to --output.", + "--output-artifacts", + help="When --output-format is json, append the list of all removed artifacts to output", mandatory=False, default=False, ) @@ -143,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() elif format == "json": - text = json.dumps(result) + 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): @@ -178,8 +179,6 @@ def main(self): today=today, ignore_not_found=self._ignore_not_found, worker_count=self._worker_count, - output_format=self._output_format, - output_artifacts=self._output_artifacts, ) # Filter policies by name @@ -202,8 +201,8 @@ def main(self): "file_count": summary.artifacts_removed, "size": summary.artifacts_size } - if summary.removed_artifacts_list is not None: - policy["artifacts_list"] = summary.removed_artifacts_list + if summary.artifacts is not None and self._output_artifacts: + policy["artifacts"] = summary.artifacts result["policies"].append(policy) result["total_size"] = total_size