Skip to content

Commit eeaeaf0

Browse files
author
Marcus Linke
committed
Merge branch 'list-containers-filters' of https://github.com/carlossg/docker-java into carlossg-list-containers-filters
2 parents 0b256d3 + 769f176 commit eeaeaf0

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

src/main/java/com/github/dockerjava/api/model/Filters.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.dockerjava.api.model;
22

3+
import java.util.Arrays;
34
import java.util.HashMap;
45
import java.util.List;
56
import java.util.Map;
@@ -21,7 +22,7 @@ public class Filters {
2122
private static ObjectMapper OBJECT_MAPPER = new JacksonJaxbJsonProvider().locateMapper(Map.class,
2223
MediaType.APPLICATION_JSON_TYPE);
2324

24-
private Map<String, String[]> filters = new HashMap<String, String[]>();
25+
private Map<String, List<String>> filters = new HashMap<String, List<String>>();
2526

2627
public Filters() {
2728
}
@@ -40,42 +41,41 @@ public Filters(String image, String container) {
4041
}
4142

4243
public Filters withFilter(String key, String... value) {
43-
filters.put(key, value);
44+
filters.put(key, Arrays.asList(value));
4445
return this;
4546
}
4647

47-
public String[] getFilter(String key) {
48+
public List<String> getFilter(String key) {
4849
return filters.get(key);
4950
}
5051

5152
public Filters withImage(String... image) {
52-
filters.put("image", image);
53+
filters.put("image", Arrays.asList(image));
5354
return this;
5455
}
5556

56-
public String[] getImage() {
57+
public List<String> getImage() {
5758
return getFilter("image");
5859
}
5960

6061
public Filters withContainer(String... container) {
61-
filters.put("container", container);
62+
filters.put("container", Arrays.asList(container));
6263
return this;
6364
}
6465

65-
public String[] getContainer() {
66+
public List<String> getContainer() {
6667
return getFilter("container");
6768
}
6869

69-
public Filters withLabel(String label) {
70-
return withLabel(label, (String[]) null);
71-
}
72-
73-
public Filters withLabel(String label, String... value) {
74-
if (value != null) {
75-
filters.put(label, value);
76-
} else {
77-
filters.put("label", new String[] { label });
78-
}
70+
/**
71+
* Filter by labels
72+
*
73+
* @param labels
74+
* string array in the form ["key"] or ["key=value"] or a mix of both
75+
* @return
76+
*/
77+
public Filters withLabel(String... labels) {
78+
filters.put("label", Arrays.asList(labels));
7979
return this;
8080
}
8181

@@ -87,4 +87,4 @@ public String toString() {
8787
throw new RuntimeException(e);
8888
}
8989
}
90-
}
90+
}

src/main/java/com/github/dockerjava/core/command/ListContainersCmdImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public ListContainersCmd withFilters(Filters filters) {
111111
@Override
112112
public String toString() {
113113
return new StringBuilder("ps ").append(showAll ? "--all=true" : "").append(showSize ? "--size=true" : "")
114-
.append(sinceId != null ? "--since " + sinceId : "")
115-
.append(beforeId != null ? "--before " + beforeId : "").append(limit != -1 ? "-n " + limit : "")
116-
.append(filters != null ? "--filters " + filters : "").toString();
114+
.append(sinceId != null ? " --since " + sinceId : "")
115+
.append(beforeId != null ? " --before " + beforeId : "").append(limit != -1 ? "-n " + limit : "")
116+
.append(filters != null ? " --filters " + filters : "").toString();
117117
}
118118
}

src/test/java/com/github/dockerjava/core/command/ListContainersCmdImplTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public void testListContainers() throws DockerException {
6767

6868
int size = containers.size();
6969

70-
CreateContainerResponse container1 = dockerClient.createContainerCmd(testImage).withCmd("echo")
71-
.withLabels(ImmutableMap.of("test", "docker-java")).exec();
70+
CreateContainerResponse container1 = dockerClient.createContainerCmd(testImage).withCmd("echo").exec();
7271

7372
assertThat(container1.getId(), not(isEmptyString()));
7473

@@ -102,8 +101,10 @@ public void testListContainers() throws DockerException {
102101
assertThat(container2.getImage(), startsWith(testImage));
103102

104103
// list with filter by label
104+
dockerClient.createContainerCmd(testImage).withCmd("echo").withLabels(ImmutableMap.of("test", "docker-java"))
105+
.exec();
105106
filteredContainers = dockerClient.listContainersCmd().withShowAll(true)
106-
.withFilters(new Filters().withLabel("test", "docker-java")).exec();
107+
.withFilters(new Filters().withLabel("test=docker-java")).exec();
107108
assertThat(filteredContainers.size(), is(equalTo(1)));
108109
Container container3 = filteredContainers.get(0);
109110
assertThat(container3.getCommand(), not(isEmptyString()));

0 commit comments

Comments
 (0)