Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiline labels and empty labels #968

Merged
merged 2 commits into from
Apr 4, 2018
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
Expand Up @@ -60,10 +60,10 @@ public class DockerFileBuilder {
private List<String> runCmds = new ArrayList<>();

// environment
private Map<String,String> envEntries = new HashMap<>();
private Map<String,String> envEntries = new LinkedHashMap<>();

// image labels
private Map<String, String> labels = new HashMap<>();
private Map<String, String> labels = new LinkedHashMap<>();

// exposed volumes
private List<String> volumes = new ArrayList<>();
Expand Down Expand Up @@ -225,14 +225,48 @@ private void addMap(StringBuilder b,DockerFileKeyword keyword, Map<String,String
String entries[] = new String[map.size()];
int i = 0;
for (Map.Entry<String, String> entry : map.entrySet()) {
entries[i++] = quote(entry.getKey()) + "=" + quote(entry.getValue());
entries[i++] = createKeyValue(entry.getKey(), entry.getValue());
}
keyword.addTo(b, entries);
}
}

private String quote(String value) {
return StringUtils.quoteAndEscape(value,'"');
/**
* Escape any slashes, quotes, and newlines int the value. If any escaping occurred, quote the value.
* @param key The key
* @param value The value
* @return Escaped and quoted key="value"
*/
private String createKeyValue(String key, String value) {
StringBuilder sb = new StringBuilder();
// no quoting the key; "Keys are alphanumeric strings which may contain periods (.) and hyphens (-)"
sb.append(key).append('=');
if (value == null || value.isEmpty()) {
return sb.append("\"\"").toString();
}
StringBuffer valBuf = new StringBuffer();
boolean escaped = false;
for (int i = 0; i < value.length(); ++i) {
char c = value.charAt(i);
switch (c) {
case '"':
case '\n':
case '\\':
escaped = true;
// escape the character
valBuf.append('\\');
// fall into writing the character
default:
valBuf.append(c);
}
}
if (escaped) {
// need to keep quotes
sb.append('"').append(valBuf.toString()).append('"');
} else {
sb.append(value);
}
return sb.toString();
}

private void addPorts(StringBuilder b) {
Expand Down Expand Up @@ -396,7 +430,6 @@ public DockerFileBuilder env(Map<String, String> values) {
public DockerFileBuilder labels(Map<String,String> values) {
if (values != null) {
this.labels.putAll(values);
validateMap(labels);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,22 @@ public void testBuildDockerFile() throws Exception {
String expected = loadFile("docker/Dockerfile.test");
assertEquals(expected, stripCR(dockerfileContent));
}


@Test
public void testBuildDockerFileMultilineLabel() throws Exception {
Arguments a = Arguments.Builder.get().withParam("c1").withParam("c2").build();
String dockerfileContent = new DockerFileBuilder()
.add("/src", "/dest")
.baseImage("image")
.cmd(a)
.labels(ImmutableMap.of("key", "unquoted",
"flag", "",
"some-json", "{\n \"key\": \"value\"\n}\n"))
.content();
String expected = loadFile("docker/Dockerfile.multiline_label.test");
assertEquals(expected, stripCR(dockerfileContent));
}

@Test
public void testBuildDockerFileUDPPort() throws Exception {
Arguments a = Arguments.Builder.get().withParam("c1").withParam("c2").build();
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/docker/Dockerfile.multiline_label.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM image
LABEL key=unquoted flag="" some-json="{\
\"key\": \"value\"\
}\
"
COPY /src /maven/dest
CMD ["c1","c2"]