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
roytev authored and zkygr committed Aug 16, 2024
1 parent 53f075d commit 02b6f49
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ artifactory-cleanup --output=myfile.txt
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
artifactory-cleanup --output=myfile.json --output-format json --output-artifacts
```

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


class ArtifactoryCleanup:
Expand All @@ -27,21 +27,23 @@ def __init__(
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)

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
Expand Down Expand Up @@ -80,7 +82,7 @@ def _delete(artifact):
artifacts_size=artifacts_size,
artifacts_removed=len(artifacts_to_remove),
)
if self.output_format == "json-with-artifact-list":
if self.output_format == "json" and self.output_artifacts:
summary.removed_artifacts_list = artifacts_to_remove
yield summary
except KeyError:
Expand Down
11 changes: 9 additions & 2 deletions artifactory_cleanup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@ class ArtifactoryCleanupCLI(cli.Application):

_output_format = cli.SwitchAttr(
"--output-format",
Set("table", "json", "json-with-artifact-list", case_sensitive=False),
Set("table", "json", case_sensitive=False),
help="Choose the output format",
default="table",
requires=["--output"],
mandatory=False,
)

_output_artifacts = cli.Flag(
"--output-artifacts", help="When --output-format is json, append the a list of all deleted artifacts to --output.",
mandatory=False,
default=False,
)

@property
def VERSION(self):
# To prevent circular imports
Expand Down Expand Up @@ -140,7 +146,7 @@ def _create_output_file(self, result, filename, format):
text = None
if format == "table":
text = self._format_table(result).get_string()
elif format == "json" or format == "json-with-artifact-list":
elif format == "json":
text = json.dumps(result)

with open(filename, "w") as file:
Expand Down Expand Up @@ -173,6 +179,7 @@ def main(self):
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 Down

0 comments on commit 02b6f49

Please sign in to comment.