Skip to content

Commit

Permalink
fix: Pass non-default session name to fcli: action statements (fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Jun 28, 2024
1 parent b16c245 commit 8b762e2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.fortify.cli.common.progress.helper.ProgressWriterType;
import com.fortify.cli.common.util.DisableTest;
import com.fortify.cli.common.util.DisableTest.TestType;
import com.fortify.cli.common.util.EnvHelper;

import lombok.SneakyThrows;
import picocli.CommandLine.Mixin;
Expand Down Expand Up @@ -68,7 +69,26 @@ public final Integer call() {
private Callable<Integer> run(ActionRunner actionRunner, IProgressWriterI18n progressWriter) {
actionRunner.getSpelEvaluator().configure(context->configure(actionRunner, context));
progressWriter.writeProgress("Executing action %s", actionRunner.getAction().getMetadata().getName());
return actionRunner.run(actionArgs);
// We need to set the FCLI_DEFAULT_<module>_SESSION environment variable to allow fcli: statements to
// pick up the current session name, and (although probably not needed currently), reset the default
// session name to the previous value once the action completes.
var sessionEnvName = String.format("%s_%s_SESSION", System.getProperty("fcli.env.default.prefix", "FCLI_DEFAULT"), getType().toUpperCase());
var sessionPropertyName = EnvHelper.envSystemPropertyName(sessionEnvName);
var sessionEnvOrgValue = EnvHelper.env(sessionEnvName);
try {
setOrClearSystemProperty(sessionPropertyName, getSessionName());
return actionRunner.run(actionArgs);
} finally {
setOrClearSystemProperty(sessionPropertyName, sessionEnvOrgValue);
}
}

private void setOrClearSystemProperty(String name, String value) {
if ( value==null ) {
System.clearProperty(name);
} else {
System.setProperty(name, value);
}
}

private ParameterException onValidationErrors(OptionsParseResult optionsParseResult) {
Expand All @@ -79,5 +99,6 @@ private ParameterException onValidationErrors(OptionsParseResult optionsParseRes
}

protected abstract String getType();
protected abstract String getSessionName();
protected abstract void configure(ActionRunner actionRunner, SimpleEvaluationContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public static final String envName(String productEnvId, String suffix) {
* other purposes.
*/
public static final String env(String name) {
return System.getProperty("fcli.env."+name, System.getenv(name));
return System.getProperty(envSystemPropertyName(name), System.getenv(name));
}

public static String envSystemPropertyName(String envName) {
return "fcli.env."+envName;
}

public static final Boolean asBoolean(String s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ protected final String getType() {
return "FoD";
}

@Override
protected String getSessionName() {
return unirestInstanceSupplier.getSessionName();
}

@Override
protected void configure(ActionRunner templateRunner, SimpleEvaluationContext context) {
templateRunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ protected final String getType() {
return "SSC";
}

@Override
protected String getSessionName() {
return unirestInstanceSupplier.getSessionName();
}

@Override
protected void configure(ActionRunner templateRunner, SimpleEvaluationContext context) {
templateRunner
Expand Down

0 comments on commit 8b762e2

Please sign in to comment.