Skip to content

Commit

Permalink
fixup! feat(output): save list of deleted artifacts as json
Browse files Browse the repository at this point in the history
  • Loading branch information
zkygr committed Aug 19, 2024
1 parent ad796af commit ddcdada
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
9 changes: 2 additions & 7 deletions artifactory_cleanup/artifactorycleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -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")
Expand Down
15 changes: 7 additions & 8 deletions artifactory_cleanup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit ddcdada

Please sign in to comment.