Skip to content

Commit

Permalink
fix: Work-around for user.home in Docker images
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Sep 4, 2024
1 parent 2f19804 commit 9c6a56c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.commons.io.IOUtils;

import com.fortify.cli.common.util.EnvHelper;
import com.fortify.cli.common.util.PicocliSpecHelper;
import com.fortify.cli.common.util.StringUtils;

Expand Down Expand Up @@ -120,7 +121,7 @@ public static final String resolve(String source) {
private static final String resolveFile(String file) {
// As '~' will not be resolved by the shell due to the 'file:'
// prefix, we resolve this manually to user home directory.
file = file.replaceFirst("^~", System.getProperty("user.home"));
file = file.replaceFirst("^~", EnvHelper.getUserHome());
return Files.readString(Path.of(file));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
*******************************************************************************/
package com.fortify.cli.common.util;

import java.util.function.Supplier;

public final class EnvHelper {
private static final String PFX = "FCLI";
private EnvHelper() {}

public static final String getUserHome() {
return envOrDefault(PFX+"_USER_HOME", ()->System.getProperty("user.home"));
}

public static final void checkSecondaryWithoutPrimary(String secondaryEnvName, String primaryEnvName) {
if ( env(primaryEnvName)==null && env(secondaryEnvName)!=null ) {
throw new IllegalStateException("Environment variable "+secondaryEnvName+" requires "+primaryEnvName+" to be set as well");
Expand Down Expand Up @@ -51,6 +57,11 @@ public static final String envName(String productEnvId, String suffix) {
: String.format("%s_%s_%s", PFX, productEnvId, suffix);
}

public static final String envOrDefault(String name, Supplier<String> defaultSupplier) {
var value = env(name);
return StringUtils.isNotBlank(value) ? value : defaultSupplier.get();
}

/**
* Get the value of the environment variable with the given name.
* This method allows environment variables to be overridden through
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static final Path getFortifyHomePath() {
String fortifyData = EnvHelper.env(ENVNAME_FORTIFY_DATA_DIR);
return StringUtils.isNotBlank(fortifyData)
? Path.of(fortifyData).toAbsolutePath()
: Path.of(System.getProperty("user.home"), DEFAULT_FORTIFY_DIR_NAME).toAbsolutePath();
: Path.of(EnvHelper.getUserHome(), DEFAULT_FORTIFY_DIR_NAME).toAbsolutePath();
}

public static final Path getFcliHomePath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.fortify.cli.common.progress.cli.mixin.ProgressWriterFactoryMixin;
import com.fortify.cli.common.util.DisableTest;
import com.fortify.cli.common.util.DisableTest.TestType;
import com.fortify.cli.common.util.EnvHelper;
import com.fortify.cli.common.util.FileUtils;
import com.fortify.cli.common.util.StringUtils;
import com.fortify.cli.tool._common.helper.ToolInstallationDescriptor;
Expand Down Expand Up @@ -129,7 +130,7 @@ private final Path getBasePath() {
? null
: installOrBaseDirArgGroup.baseDir.toPath();
if ( getInstallPath()==null && basePath==null ) {
basePath = Path.of(System.getProperty("user.home"),"fortify", "tools");
basePath = Path.of(EnvHelper.getUserHome(), "fortify", "tools");
}
return basePath;
}
Expand Down

0 comments on commit 9c6a56c

Please sign in to comment.