Skip to content

Commit

Permalink
Update DockerConnectionDetector,VolumeBindingUtil with home OS env
Browse files Browse the repository at this point in the history
+ Update DockerConnectionDetector,VolumeBindingUtil with updated
  precedence for home OS environment variable
+ Move HOME OS env variable resolving logic to EnvUtil
  • Loading branch information
rohanKanojia committed Apr 4, 2021
1 parent c203542 commit 8a20007
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.fabric8.maven.docker.access.util.LocalSocketUtil;
import io.fabric8.maven.docker.util.*;

import static io.fabric8.maven.docker.util.EnvUtil.getUserHome;

/**
* Detector for determining the Docker access mechanism
*/
Expand Down Expand Up @@ -167,7 +169,7 @@ private void initCertPath(String certPath) throws IOException {
this.certPath = certPath != null ? certPath : System.getenv("DOCKER_CERT_PATH");
// Try default locations as last resort
if (this.certPath == null) {
File dockerHome = new File(System.getProperty("user.home") + "/.docker");
File dockerHome = new File(getUserHome() + "/.docker");
if (dockerHome.isDirectory()) {
String[] entries = dockerHome.list(SuffixFileFilter.PEM_FILTER);
if (entries == null) {
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -36,6 +35,8 @@
import io.fabric8.maven.docker.assembly.DockerAssemblyConfigurationSource;
import org.yaml.snakeyaml.Yaml;

import static io.fabric8.maven.docker.util.EnvUtil.getUserHome;


/**
* Utility class for dealing with dockerfiles
Expand All @@ -45,8 +46,6 @@
public class DockerFileUtil {

private static final String ARG_PATTERN_REGEX = "\\$(?:\\{(.*)\\}|(.*))";
// injection point for unit tests
private static UnaryOperator<String> systemGetEnv = System::getenv;

private DockerFileUtil() {}

Expand Down Expand Up @@ -252,11 +251,7 @@ public static Map<String,?> readKubeConfig() {
}

private static File getHomeDir() {
String homeDir = systemGetEnv.apply("HOME");
if (homeDir == null) {
homeDir = System.getProperty("user.home");
}
return new File(homeDir);
return new File(getUserHome());
}

private static void updateMapWithArgValue(Map<String, String> result, Map<String, String> args, String argString) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/fabric8/maven/docker/util/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -37,6 +38,9 @@ public class EnvUtil {

public static final String PROPERTY_COMBINE_POLICY_SUFFIX = "_combine";

// injection point for unit tests
private static UnaryOperator<String> systemGetEnv = System::getenv;

private EnvUtil() {}

// Convert docker host URL to an HTTP(s) URL
Expand Down Expand Up @@ -510,4 +514,16 @@ public static boolean isMaven350OrLater(MavenSession mavenSession) {
String mavenVersion = mavenSession.getSystemProperties().getProperty("maven.version", "3");
return greaterOrEqualsVersion(mavenVersion, "3.5.0");
}

/**
* Get User's HOME directory path
* @return a String value for user's home directory
*/
public static String getUserHome() {
String homeDir = systemGetEnv.apply("HOME");
if (homeDir == null) {
homeDir = System.getProperty("user.home");
}
return homeDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.regex.Pattern;

import static io.fabric8.maven.docker.util.DockerPathUtil.resolveAbsolutely;
import static io.fabric8.maven.docker.util.EnvUtil.getUserHome;

/**
* Utility methods for working with Docker volume bindings.
Expand Down Expand Up @@ -169,7 +170,7 @@ public static String resolveRelativeVolumeBinding(File baseDir, String bindingSt
if (isRelativePath(localPath)) {
File resolvedFile;
if (isUserHomeRelativePath(localPath)) {
resolvedFile = resolveAbsolutely(prepareUserHomeRelativePath(localPath), System.getProperty("user.home"));
resolvedFile = resolveAbsolutely(prepareUserHomeRelativePath(localPath), getUserHome());
} else {
if (!baseDir.isAbsolute()) {
throw new IllegalArgumentException("Base directory '" + baseDir + "' must be absolute.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private void executeWithTempHomeDir(HomeDirExecutor executor) throws IOException
String userHome = System.getProperty("user.home");
environmentVariables.clear("KUBECONFIG");
try {
Field envField = DockerFileUtil.class.getDeclaredField("systemGetEnv");
Field envField = EnvUtil.class.getDeclaredField("systemGetEnv");
ReflectionAccessor.getInstance().makeAccessible(envField);
@SuppressWarnings("unchecked")
UnaryOperator<String> origEnv = (UnaryOperator<String>) envField.get(null);
Expand Down

0 comments on commit 8a20007

Please sign in to comment.