forked from fabric8io/docker-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This replaces fabric8io#876 as its a bit more flexible and does not require a new configuration option. You can use now a prefix `%z` for an empty string
- Loading branch information
Showing
9 changed files
with
192 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/io/fabric8/maven/docker/util/FormatParameterReplacer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.fabric8.maven.docker.util; | ||
|
||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.maven.project.MavenProject; | ||
|
||
/** | ||
* @author roland | ||
* @since 04.11.17 | ||
*/ | ||
public class FormatParameterReplacer { | ||
|
||
// Detect format elements within the name | ||
private Pattern FORMAT_IDENTIFIER_PATTERN = Pattern.compile("^(.*?)%([^a-zA-Z]*)([a-zA-Z])(.*)$"); | ||
|
||
private final Map<String, Lookup> lookupMap; | ||
|
||
public FormatParameterReplacer(Map<String, Lookup> lookupMap) { | ||
this.lookupMap = lookupMap; | ||
} | ||
|
||
public String replace(String input) { | ||
StringBuilder ret = new StringBuilder(); | ||
while (true) { | ||
Matcher matcher = FORMAT_IDENTIFIER_PATTERN.matcher(input); | ||
if (!matcher.matches()) { | ||
ret.append(input); | ||
return ret.toString(); | ||
} | ||
ret.append(matcher.group(1)); | ||
ret.append(formatElement(matcher.group(2),matcher.group(3))); | ||
input = matcher.group(4); | ||
} | ||
} | ||
|
||
private String formatElement(String options, String what) { | ||
FormatParameterReplacer.Lookup lookup = lookupMap.get(what); | ||
if (lookup == null) { | ||
throw new IllegalArgumentException(String.format("No image name format element '%%%s' known", what) ); | ||
} | ||
String val = lookup.lookup(); | ||
return String.format("%" + (options != null ? options : "") + "s",val); | ||
} | ||
|
||
|
||
// Lookup abstraction | ||
public interface Lookup { | ||
String lookup(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/test/java/io/fabric8/maven/docker/log/LogOutputSpecFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.fabric8.maven.docker.log; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import io.fabric8.maven.docker.config.ImageConfiguration; | ||
import io.fabric8.maven.docker.config.LogConfiguration; | ||
import io.fabric8.maven.docker.config.RunImageConfiguration; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author roland | ||
* @since 04.11.17 | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class LogOutputSpecFactoryTest { | ||
|
||
private static String ALIAS = "fcn"; | ||
private static String NAME = "rhuss/fcn:1.0"; | ||
private static String CONTAINER_ID = "1234567890"; | ||
|
||
@Parameterized.Parameters(name = "{index}: format \"{0}\" --> \"{1}\"") | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][] { | ||
{ "%z", "" }, | ||
{ null, ALIAS + "> "}, | ||
{ "%c", CONTAINER_ID.substring(0,6) }, | ||
{ "%C: ", CONTAINER_ID + ": " }, | ||
{ "%n -- ", NAME + " -- " }, | ||
{ "%z%c%n%C %a", CONTAINER_ID.substring(0,6) + NAME + CONTAINER_ID + " " + ALIAS } | ||
}); | ||
} | ||
|
||
@Parameterized.Parameter(0) | ||
public String prefixFormat; | ||
|
||
@Parameterized.Parameter(1) | ||
public String expectedPrefix; | ||
|
||
@Test | ||
public void prefix() { | ||
LogOutputSpec spec = createSpec(prefixFormat); | ||
assertEquals(expectedPrefix, spec.getPrompt(false, null)); | ||
} | ||
|
||
private LogOutputSpec createSpec(String prefix) { | ||
LogOutputSpecFactory factory = new LogOutputSpecFactory(false, false, null); | ||
LogConfiguration logConfig = new LogConfiguration.Builder().prefix(prefix).build(); | ||
RunImageConfiguration runConfig = new RunImageConfiguration.Builder().log(logConfig).build(); | ||
ImageConfiguration imageConfiguration = new ImageConfiguration.Builder().alias(ALIAS).name(NAME).runConfig(runConfig).build(); | ||
return factory.createSpec(CONTAINER_ID,imageConfiguration); | ||
} | ||
} |