Skip to content

Commit

Permalink
Follow up PR for #1373
Browse files Browse the repository at this point in the history
Use regex in order to resolve ARG value
  • Loading branch information
rohanKanojia committed Sep 29, 2020
1 parent 207c0f3 commit e0279d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ static Map<String, String> extractArgsFromLines(List<String[]> argLines, Map<Str
}

static String resolveArgValueFromStrContainingArgKey(String argString, Map<String, String> args) {
if (argString.startsWith("$") && args.containsKey(argString.substring(1))) {
return args.get(argString.substring(1));
} else if (argString.startsWith("${") && argString.endsWith("}") && args.containsKey(argString.substring(2, argString.length() - 1))) {
return args.get(argString.substring(2, argString.length() - 1));
Pattern argPattern = Pattern.compile("\\$(?:\\{(.*)\\}|(.*))");
Matcher matcher = argPattern.matcher(argString);
if (matcher.matches()) {
if (matcher.group(1) != null) {
return args.get(matcher.group(1));
} else if (matcher.group(2) != null) {
return args.get(matcher.group(2));
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static io.fabric8.maven.docker.util.PathTestUtil.createTmpFile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

/**
* @author roland
Expand Down Expand Up @@ -133,6 +134,11 @@ public void testMultiStageNamedWithDuplicates() throws Exception {
public void testResolveArgValueFromStrContainingArgKey() {
assertEquals("latest", DockerFileUtil.resolveArgValueFromStrContainingArgKey("$VERSION", Collections.singletonMap("VERSION", "latest")));
assertEquals("test", DockerFileUtil.resolveArgValueFromStrContainingArgKey("${project.scope}", Collections.singletonMap("project.scope", "test")));
assertEquals("test", DockerFileUtil.resolveArgValueFromStrContainingArgKey("$ad", Collections.singletonMap("ad", "test")));
assertNull(DockerFileUtil.resolveArgValueFromStrContainingArgKey("bla$ad", Collections.singletonMap("ad", "test")));
assertNull(DockerFileUtil.resolveArgValueFromStrContainingArgKey("${foo}bar", Collections.singletonMap("foo", "test")));
assertNull(DockerFileUtil.resolveArgValueFromStrContainingArgKey("bar${foo}", Collections.singletonMap("foo", "test")));
assertNull(DockerFileUtil.resolveArgValueFromStrContainingArgKey("$ad", Collections.emptyMap()));
}

private File copyToTempDir(String resource) throws IOException {
Expand Down

0 comments on commit e0279d5

Please sign in to comment.