From 67fcf85867d4c054eb34f4f03cff50cebe80f7a9 Mon Sep 17 00:00:00 2001 From: Ruud Senden <8635138+rsenden@users.noreply.github.com> Date: Wed, 15 Mar 2023 17:43:14 +0100 Subject: [PATCH] feat: Add FCLI_DEFAULT_* environment variable support for all positional parameters (closes #136) --- doc-resources/asciidoc/versioned/index.adoc | 2 +- .../cli/util/FortifyCLIDefaultValueProvider.java | 14 ++++++++++++-- .../common/session/cli/mixin/SessionNameMixin.java | 3 +-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/doc-resources/asciidoc/versioned/index.adoc b/doc-resources/asciidoc/versioned/index.adoc index fa8a01b832..f0337cc76d 100644 --- a/doc-resources/asciidoc/versioned/index.adoc +++ b/doc-resources/asciidoc/versioned/index.adoc @@ -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, `` 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: diff --git a/fcli-common/src/main/java/com/fortify/cli/common/cli/util/FortifyCLIDefaultValueProvider.java b/fcli-common/src/main/java/com/fortify/cli/common/cli/util/FortifyCLIDefaultValueProvider.java index bb053f452a..cbea12a1c9 100644 --- a/fcli-common/src/main/java/com/fortify/cli/common/cli/util/FortifyCLIDefaultValueProvider.java +++ b/fcli-common/src/main/java/com/fortify/cli/common/cli/util/FortifyCLIDefaultValueProvider.java @@ -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); } @@ -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("(?") private String sessionName; } @Override