Skip to content

Commit

Permalink
Issues #1430: Add support for multiple ARGS defined as a part of dock…
Browse files Browse the repository at this point in the history
…er imageTag string

Signed-off-by: Damian Galka <galka.damian.91@gmail.com>
  • Loading branch information
gauee authored and rohanKanojia committed Sep 7, 2021
1 parent ab16be7 commit ea3b51d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
42 changes: 18 additions & 24 deletions src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*/
public class DockerFileUtil {

private static final String ARG_PATTERN_REGEX = "\\$(?:\\{(.*)\\}|(.*))";
private static final String ARG_PATTERN_REGEX = "\\$([\\w|\\-|\\.]+)|\\$\\{([\\w|\\-|\\.]+)\\}";

private DockerFileUtil() {}

Expand Down Expand Up @@ -169,35 +169,29 @@ static Map<String, String> extractArgsFromLines(List<String[]> argLines, Map<Str
return result;
}

static String resolveArgValueFromStrContainingArgKey(String argString, Map<String, String> args) {
Pattern argPattern = Pattern.compile(ARG_PATTERN_REGEX);
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));
static String resolveImageTagFromArgs(String imageTagString, Map<String, String> args) {
String resolvedImageString = imageTagString;
Set<String> foundArgs = findAllArgs(imageTagString);
for (String foundArg : foundArgs) {
if (args.containsKey(foundArg)) {
resolvedImageString = resolvedImageString.replaceFirst(String.format("\\$\\{*%s\\}*", foundArg),
args.get(foundArg));
}
}
return null;
return resolvedImageString;
}

private static String resolveImageTagFromArgs(String imageTagString, Map<String, String> args) {
if (imageTagString.startsWith("$")) { // FROM $IMAGE
String resolvedVal = resolveArgValueFromStrContainingArgKey(imageTagString, args);
if (resolvedVal != null) {
return resolvedVal;
}
} else { // FROM image:$TAG_ARG
String[] imageTagArr = imageTagString.split(":");
if (imageTagArr.length > 1) {
String tag = resolveArgValueFromStrContainingArgKey(imageTagArr[1], args);
if (tag != null) {
return imageTagArr[0] + ":" + tag;
}
static Set<String> findAllArgs(String imageTagString) {
Matcher m = Pattern.compile(ARG_PATTERN_REGEX).matcher(imageTagString);
Set<String> args = new HashSet<>();
while(m.find()){
if(m.group(1)!=null){
args.add(m.group(1));
}else if(m.group(2)!=null){
args.add(m.group(2));
}
}
return imageTagString;
return args;
}

private static Reader getFileReaderFromDir(File file) {
Expand Down
22 changes: 14 additions & 8 deletions src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.file.Files;
import java.util.*;

import com.google.common.collect.ImmutableSet;
import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
Expand All @@ -32,7 +33,6 @@
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 @@ -135,13 +135,19 @@ public void testMultiStageNamedWithDuplicates() throws Exception {

@Test
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()));
assertEquals("latest", DockerFileUtil.resolveImageTagFromArgs("$VERSION", Collections.singletonMap("VERSION", "latest")));
assertEquals("test", DockerFileUtil.resolveImageTagFromArgs("${project.scope}", Collections.singletonMap("project.scope", "test")));
assertEquals("test", DockerFileUtil.resolveImageTagFromArgs("$ad", Collections.singletonMap("ad", "test")));
assertEquals("blatest", DockerFileUtil.resolveImageTagFromArgs("bla$ad", Collections.singletonMap("ad", "test")));
assertEquals("testbar", DockerFileUtil.resolveImageTagFromArgs("${foo}bar", Collections.singletonMap("foo", "test")));
assertEquals("bartest", DockerFileUtil.resolveImageTagFromArgs("bar${foo}", Collections.singletonMap("foo", "test")));
assertEquals("$ad", DockerFileUtil.resolveImageTagFromArgs("$ad", Collections.emptyMap()));
}

@Test
public void testFindAllArgsDefinedInString() {
assertEquals(ImmutableSet.of("REPO_1", "IMAGE-1", "VERSION"), DockerFileUtil.findAllArgs("$REPO_1/bar${IMAGE-1}foo:$VERSION"));
assertEquals(Collections.emptySet(), DockerFileUtil.findAllArgs("${invalidArg"));
}

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

0 comments on commit ea3b51d

Please sign in to comment.