diff --git a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/cli/mixin/CommonOptionMixins.java b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/cli/mixin/CommonOptionMixins.java index a07527ee9b..46a5ad1686 100644 --- a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/cli/mixin/CommonOptionMixins.java +++ b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/cli/mixin/CommonOptionMixins.java @@ -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; @@ -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)); } diff --git a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/EnvHelper.java b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/EnvHelper.java index e826e69fe4..9086548613 100644 --- a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/EnvHelper.java +++ b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/EnvHelper.java @@ -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"); @@ -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 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 diff --git a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/FcliDataHelper.java b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/FcliDataHelper.java index a1793d3918..81d82b2852 100644 --- a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/FcliDataHelper.java +++ b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/util/FcliDataHelper.java @@ -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() { diff --git a/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/cli/cmd/AbstractToolInstallCommand.java b/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/cli/cmd/AbstractToolInstallCommand.java index 8873764943..9cd3e9d5a1 100644 --- a/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/cli/cmd/AbstractToolInstallCommand.java +++ b/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/cli/cmd/AbstractToolInstallCommand.java @@ -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; @@ -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; }