Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.dockerjava.api.command;

import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -44,6 +45,15 @@ public interface ListImagesCmd extends SyncDockerCmd<List<Image>> {
*/
ListImagesCmd withLabelFilter(Map<String, String> labels);

/**
* Filter images by reference
*
* @param reference string in the form {@code <image-name>[:<tag>]}
*/
ListImagesCmd withReferenceFilter(String reference);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking out loud: should we add withFilter(String name, Collection<String> value) and make these methods default implementations that would call it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree!

@kiview kiview Sep 28, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for having it as a default method rather than in the Impl class? Binary compatibility if there are implementations of ListImagesCmd by users out there?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point to not break existing implementations. However, FiltersBuilder belongs to the core module, not the api


ListImagesCmd withFilter(String key, Collection<String> values);

interface Exec extends DockerCmdSyncExec<ListImagesCmd, List<Image>> {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -46,7 +48,7 @@ public ListImagesCmd withShowAll(Boolean showAll) {
@Override
public ListImagesCmd withDanglingFilter(Boolean dangling) {
checkNotNull(dangling, "dangling have not been specified");
filters.withFilter("dangling", dangling.toString());
withFilter("dangling", Collections.singletonList(dangling.toString()));
return this;
}

Expand All @@ -71,6 +73,21 @@ public ListImagesCmd withImageNameFilter(String imageNameFilter) {
return this;
}

@Override
public ListImagesCmd withReferenceFilter(String reference) {
checkNotNull(reference, "reference filter not specified");
withFilter("reference", Collections.singletonList(reference));
return this;
}

@Override
public ListImagesCmd withFilter(String key, Collection<String> values) {
checkNotNull(key, "key not specified");
checkNotNull(values, "values not specified");
filters.withFilter(key, values);
return this;
}

@Override
public String getImageNameFilter() {
return this.imageNameFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.api.model.Info;
import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

import static com.github.dockerjava.utils.TestUtils.isNotSwarm;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -54,6 +57,36 @@ public void listImagesWithDanglingFilter() throws DockerException {
assertTrue(imageInFilteredList);
}

@Test
public void listImagesWithReferenceFilter() throws DockerException {
String tag = "" + RandomUtils.nextInt(0, Integer.MAX_VALUE);

dockerRule.getClient().tagImageCmd("busybox:latest", "docker-java/busybox", tag).exec();
try {
List<Image> images = dockerRule.getClient().listImagesCmd().withReferenceFilter("docker-java/busybox")
.exec();
assertThat(images, hasSize(1));
}
finally {
dockerRule.getClient().removeImageCmd("docker-java/busybox:" + tag).exec();
}
}

@Test
public void listImagesWithFilter() throws DockerException {
String tag = "" + RandomUtils.nextInt(0, Integer.MAX_VALUE);

dockerRule.getClient().tagImageCmd("busybox:latest", "docker-java/busybox", tag).exec();
try {
List<Image> images = dockerRule.getClient().listImagesCmd().withFilter("reference", Collections.singletonList("docker-java/busybox"))
.exec();
assertThat(images, hasSize(1));
}
finally {
dockerRule.getClient().removeImageCmd("docker-java/busybox:" + tag).exec();
}
}

private boolean isImageInFilteredList(List<Image> images, String expectedImageId) {
for (Image image : images) {
if (expectedImageId.equals(image.getId())) {
Expand Down