Created
November 13, 2020 00:29
-
-
Save ttddyy/6c6069de65e1a91ec962de7118c3e09d to your computer and use it in GitHub Desktop.
MyImageNameSubstitutor.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
@Slf4j | |
public class MyImageNameSubstitutor extends ImageNameSubstitutor { | |
public static final String ENV_VAR_KEY = "MY_TESTCONTAINERS_REGISTRY"; | |
public static final String SYSTEM_PROP_KEY = "my.testcontainers.registry"; | |
public static final String DEFAULT_REGISTRY = "sample.com/"; | |
protected String registryUrl; | |
public MyRegistryImageNameSubstitutor() { | |
this.registryUrl = appendSlashIfNecessary(determineRegistry()); | |
} | |
@Override | |
public DockerImageName apply(DockerImageName original) { | |
log.debug("Applying My image name substitution to {}", original.asCanonicalNameString()); | |
String registry = original.getRegistry(); | |
if (StringUtils.hasLength(registry)) { | |
log.info("Image already has registry specified. Skipping substitution. registry={}", registry); | |
return original; | |
} | |
return DockerImageName.parse(this.registryUrl + original.asCanonicalNameString()); | |
} | |
@Override | |
protected String getDescription() { | |
return String.format("Add registry prefix: %s", this.registryUrl); | |
} | |
protected String determineRegistry() { | |
String registry = System.getProperty(SYSTEM_PROP_KEY); | |
if (StringUtils.hasText(registry)) { | |
log.info("Registry resolved from system property. registry={}", registry); | |
return registry; | |
} | |
registry = System.getenv().get(ENV_VAR_KEY); | |
if (StringUtils.hasText(registry)) { | |
log.info("Registry resolved from environment variable. registry={}", registry); | |
return registry; | |
} | |
return getDefaultRegistry(); | |
} | |
protected String appendSlashIfNecessary(String registry) { | |
if (registry.charAt(registry.length() - 1) == '/') { | |
return registry; | |
} | |
return registry + "/"; | |
} | |
protected String getDefaultRegistry() { | |
return DEFAULT_REGISTRY; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment