Skip to content

Commit

Permalink
feat(output): save list of deleted artifacts as json
Browse files Browse the repository at this point in the history
  • Loading branch information
roytev authored and zkygr committed Jul 10, 2024
1 parent 929c7b0 commit 53f075d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ artifactory-cleanup --output=myfile.txt
# 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
artifactory-cleanup --output=myfile.json --output-format json-with-artifact-list
```

# Rules
Expand Down
11 changes: 8 additions & 3 deletions artifactory_cleanup/artifactorycleanup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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
Expand All @@ -14,6 +14,7 @@ class CleanupSummary:
policy_name: str
artifacts_removed: int
artifacts_size: int
removed_artifacts_list: Optional[dict] = None


class ArtifactoryCleanup:
Expand All @@ -25,12 +26,14 @@ def __init__(
today: date,
ignore_not_found: bool,
worker_count: int,
output_format: str,
):
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._init_policies(today)

Expand Down Expand Up @@ -72,12 +75,14 @@ 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),
)

if self.output_format == "json-with-artifact-list":
summary.removed_artifacts_list = artifacts_to_remove
yield summary
except KeyError:
print("Summary size not defined")
yield None
Expand Down
23 changes: 12 additions & 11 deletions artifactory_cleanup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -88,7 +88,7 @@ class ArtifactoryCleanupCLI(cli.Application):

_output_format = cli.SwitchAttr(
"--output-format",
Set("table", "json", case_sensitive=False),
Set("table", "json", "json-with-artifact-list", case_sensitive=False),
help="Choose the output format",
default="table",
requires=["--output"],
Expand Down Expand Up @@ -140,7 +140,7 @@ def _create_output_file(self, result, filename, format):
text = None
if format == "table":
text = self._format_table(result).get_string()
else:
elif format == "json" or format == "json-with-artifact-list":
text = json.dumps(result)

with open(filename, "w") as file:
Expand Down Expand Up @@ -172,6 +172,7 @@ def main(self):
today=today,
ignore_not_found=self._ignore_not_found,
worker_count=self._worker_count,
output_format=self._output_format,
)

# Filter policies by name
Expand All @@ -189,14 +190,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 summary.removed_artifacts_list is not None:
policy["artifacts_list"] = summary.removed_artifacts_list
result["policies"].append(policy)
result["total_size"] = total_size

self._print_table(result)
Expand Down

0 comments on commit 53f075d

Please sign in to comment.