Skip to content

Commit

Permalink
Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospereira committed Nov 30, 2023
1 parent de98052 commit e9d923e
Showing 1 changed file with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,24 @@ public void targetImageId(Provider<String> imageId) {
}

private void logWarning() {
getLogger().warn("Use property 'images' instead of 'targetImageId' when removing images");
getLogger().warn("Use property 'images' instead of 'targetImageId' when listing images to remove");
}

@Override
public void runRemoteCommand() {
if (mixesBothProperties()) {
throw new IllegalStateException("Project sets both properties 'images' and 'targetImageId', but only one is allowed.");
}
java.util.Optional<String> imageId = java.util.Optional.ofNullable(this.getImageId().getOrNull());
List<String> imagesIds = this.images.get();

if (this.getImageId().isPresent()) {
removeImage(this.getImageId().get());
if (imageId.isPresent() && !imagesIds.isEmpty()) {
throw new IllegalStateException("Project sets both properties 'images' and 'targetImageId', but only one is allowed. Please use 'images'.");
}

if (this.images.isPresent()) {
this.images.get().forEach(this::removeImage);
}
imageId.ifPresent(this::removeImage);
imagesIds.forEach(this::removeImage);
}

private void removeImage(String imageId) {
getLogger().quiet("Removing image with ID \'" + imageId + "\'.");
getLogger().quiet("Removing image with ID '{}'.", imageId);
try (RemoveImageCmd removeImageCmd = getDockerClient().removeImageCmd(imageId)) {
if (Boolean.TRUE.equals(force.getOrNull())) {
removeImageCmd.withForce(force.get());
Expand All @@ -165,8 +163,4 @@ private void removeImage(String imageId) {
removeImageCmd.exec();
}
}

private boolean mixesBothProperties() {
return !this.images.get().isEmpty() && java.util.Optional.ofNullable(this.getImageId().getOrNull()).isPresent();
}
}

0 comments on commit e9d923e

Please sign in to comment.