Skip to content

Commit

Permalink
feat: Add FCLI_DEFAULT_* environment variable support for all positio…
Browse files Browse the repository at this point in the history
…nal parameters (closes #136)
  • Loading branch information
rsenden committed Mar 15, 2023
1 parent 837791f commit 67fcf85
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc-resources/asciidoc/versioned/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Environment variable lookups are based on the following rules:

* Command aliases are not taken into account when looking for environment variables; suppose we have a `+delete+` command with alias `+rm+`, you will need to use `+FCLI_DEFAULT_..._DELETE_...+` and not `+FCLI_DEFAULT_..._RM_...+`
* For options, fcli will use the longest option name when looking for environment variables; suppose we have an option with names `+-a+`, `+--ab+` and `+--abc+`, you will need to use `+FCLI_DEFAULT_..._ABC+` and not `+FCLI_DEFAULT_..._AB+` or `+FCLI_DEFAULT_..._A+`
* Currently, not all positional parameters support default values from environment variables; this will be improved over time. Please refer to the positional parameter description in help output or manual pages to identify what environment variable suffix should be used for a particular positional parameter.
* For positional parameters, the environment variable name will be based on the parameter label, converted to `SNAKE_CASE` with all special characters replaced by an underscore. For example, `<attributeDefinitionId>` will become `ATTRIBUTE_DEFINITION_ID`, and `HOST:PORT` will become `HOST_PORT`.

Although powerful, these environment variables for providing default option and parameter values should be used with some care to avoid unexpected results:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public String defaultValue(ArgSpec argSpec) {
final var optionSpec = (OptionSpec) argSpec;
envVarSuffix = getEnvVarSuffix(optionSpec.userObject(), optionSpec.longestName().replaceAll("^-+", ""));
} else {
envVarSuffix = getEnvVarSuffix(argSpec.userObject(), null);
envVarSuffix = getEnvVarSuffix(argSpec.userObject(), normalizeParamLabel(argSpec.paramLabel()));
}
return resolve(argSpec.command(), envVarSuffix);
}
Expand All @@ -46,6 +46,16 @@ private final String getEnvVarSuffix(Object userObject, String defaultValue) {
private final String getEnvVarName(CommandSpec command, String suffix) {
String qualifiedCommandName = command.qualifiedName("_");
String combinedName = String.format("%s_%s", qualifiedCommandName, suffix);
return combinedName.replace('-', '_').toUpperCase().replaceFirst("FCLI", envPrefix);
return combinedName.replace('-', '_')
.replaceAll("__", "_") // Replace duplicate underscores
.replaceAll("^_+|_+$", "") // Strip leading and trailing underscores
.toUpperCase().replaceFirst("FCLI", envPrefix);
}

private final String normalizeParamLabel(String paramLabel) {
return paramLabel
.replaceAll("\\s", "_") // Replace whitespace by underscores
.replaceAll("[^a-zA-Z\\d-_]", "_") // Replace all special characters by underscore
.replaceAll("(?<!^)([A-Z][a-z]|(?<=[a-z])[^a-z]|(?<=[A-Z])[0-9_])", "_$1"); // Convert to snake-case without splitting uppercase words; see https://stackoverflow.com/a/73485568
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
******************************************************************************/
package com.fortify.cli.common.session.cli.mixin;

import com.fortify.cli.common.cli.util.EnvSuffix;
import com.fortify.cli.common.util.StringUtils;

import io.micronaut.core.annotation.ReflectiveAccess;
Expand Down Expand Up @@ -66,7 +65,7 @@ public static class OptionalParameter extends AbstractSessionNameMixin {
private SessionNameArgGroup nameOptions = new SessionNameArgGroup();

static class SessionNameArgGroup {
@Parameters(arity="0..1", index="0", paramLabel="session-name") @EnvSuffix("SESSION")
@Parameters(arity="0..1", index="0", paramLabel="<session>")
private String sessionName;
}
@Override
Expand Down

0 comments on commit 67fcf85

Please sign in to comment.