Skip to content

Commit f17a428

Browse files
Copilotstalep
andcommitted
Move env var resolution from ProcessedOptionBuilder to ProcessedOption
Co-authored-by: stalep <[email protected]>
1 parent 65374c6 commit f17a428

File tree

2 files changed

+30
-50
lines changed

2 files changed

+30
-50
lines changed

aesh/src/main/java/org/aesh/command/impl/internal/ProcessedOption.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public void injectValueIntoField(Object instance, InvocationProviders invocation
392392
if(getValue() != null)
393393
field.set(instance, doConvert(getValue(), invocationProviders, instance, aeshContext, doValidation));
394394
else if(defaultValues.size() > 0) {
395-
field.set(instance, doConvert(defaultValues.get(0), invocationProviders, instance, aeshContext, doValidation));
395+
field.set(instance, doConvert(resolveEnvironmentVariable(defaultValues.get(0)), invocationProviders, instance, aeshContext, doValidation));
396396
}
397397
}
398398
else if(optionType == OptionType.LIST || optionType == OptionType.ARGUMENTS) {
@@ -403,7 +403,7 @@ else if(optionType == OptionType.LIST || optionType == OptionType.ARGUMENTS) {
403403
}
404404
else if(defaultValues.size() > 0) {
405405
for(String in : defaultValues)
406-
tmpSet.add(doConvert(in, invocationProviders, instance, aeshContext, doValidation));
406+
tmpSet.add(doConvert(resolveEnvironmentVariable(in), invocationProviders, instance, aeshContext, doValidation));
407407
}
408408

409409
field.set(instance, tmpSet);
@@ -512,6 +512,33 @@ boolean isTypeAssignableByResourcesOrFile() {
512512
Path.class.isAssignableFrom(type));
513513
}
514514

515+
/**
516+
* Resolves a single environment variable reference.
517+
* If the value matches the pattern $(ENV_VAR_NAME), it will be replaced
518+
* with the value of the environment variable. If the environment variable
519+
* is not set, the original value is returned.
520+
*
521+
* @param value the value to resolve
522+
* @return the resolved value
523+
*/
524+
private String resolveEnvironmentVariable(String value) {
525+
if (value == null) {
526+
return null;
527+
}
528+
// Check if the value matches the pattern $(ENV_VAR_NAME)
529+
// Minimum length is 4 for "$(X)" where X is at least one character
530+
if (value.length() > 3 && value.startsWith("$(") && value.endsWith(")")) {
531+
String envVarName = value.substring(2, value.length() - 1);
532+
if (!envVarName.isEmpty()) {
533+
String envValue = System.getenv(envVarName);
534+
if (envValue != null) {
535+
return envValue;
536+
}
537+
}
538+
}
539+
return value;
540+
}
541+
515542
@Override
516543
public String toString() {
517544
return "ProcessedOption{" +

aesh/src/main/java/org/aesh/command/impl/internal/ProcessedOptionBuilder.java

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -342,55 +342,8 @@ else if(hasMultipleValues)
342342
//if(renderer == null)
343343
// renderer = new NullOptionRenderer();
344344

345-
// Resolve environment variables in default values
346-
List<String> resolvedDefaultValues = resolveEnvironmentVariables(defaultValues);
347-
348345
return new ProcessedOption(shortName, name, description, argument, required,
349-
valueSeparator, askIfNotSet, selectorType, resolvedDefaultValues, type, fieldName, optionType, converter,
346+
valueSeparator, askIfNotSet, selectorType, defaultValues, type, fieldName, optionType, converter,
350347
completer, validator, activator, renderer, parser, overrideRequired);
351348
}
352-
353-
/**
354-
* Resolves environment variables in default values.
355-
* If a default value matches the pattern $(ENV_VAR_NAME), it will be replaced
356-
* with the value of the environment variable.
357-
*
358-
* @param values the list of default values
359-
* @return a new list with environment variables resolved, or the original list if null
360-
*/
361-
private List<String> resolveEnvironmentVariables(List<String> values) {
362-
if (values == null) {
363-
return null;
364-
}
365-
List<String> resolved = new ArrayList<>();
366-
for (String value : values) {
367-
resolved.add(resolveEnvironmentVariable(value));
368-
}
369-
return resolved;
370-
}
371-
372-
/**
373-
* Resolves a single environment variable reference.
374-
* If the value matches the pattern $(ENV_VAR_NAME), it will be replaced
375-
* with the value of the environment variable. If the environment variable
376-
* is not set, the original value is returned.
377-
*
378-
* @param value the value to resolve
379-
* @return the resolved value
380-
*/
381-
private String resolveEnvironmentVariable(String value) {
382-
if (value == null) {
383-
return null;
384-
}
385-
// Check if the value matches the pattern $(ENV_VAR_NAME)
386-
// Minimum length is 4 for "$(X)" where X is at least one character
387-
if (value.length() > 3 && value.startsWith("$(") && value.endsWith(")")) {
388-
String envVarName = value.substring(2, value.length() - 1);
389-
String envValue = System.getenv(envVarName);
390-
if (envValue != null) {
391-
return envValue;
392-
}
393-
}
394-
return value;
395-
}
396349
}

0 commit comments

Comments
 (0)