Skip to content

Commit

Permalink
Allow to specify settings as environment variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
haumacher committed Nov 15, 2024
1 parent 4b59726 commit 3e10ba0
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion mjsip-util/src/main/java/org/mjsip/config/OptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.kohsuke.args4j.ClassParser;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.NamedOptionDef;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.ParserProperties;
import org.kohsuke.args4j.spi.OptionHandler;
import org.slf4j.LoggerFactory;
import org.zoolu.util.ConfigFile;

Expand All @@ -34,9 +38,36 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O
}

try {
List<String> envArgs = new ArrayList<>();
for (OptionHandler<?> option : parser.getOptions()) {
OptionDef optionDef = option.option;
if (optionDef instanceof NamedOptionDef) {
NamedOptionDef namedOption = (NamedOptionDef) optionDef;

String name = namedOption.name();
String value = getValueFromEnvironment(name);
if (value == null) {
for (String alias : namedOption.aliases()) {
value = getValueFromEnvironment(alias);
if (value != null) {
break;
}
}
}

if (value != null) {
envArgs.add(name);
envArgs.add(value);
}
}
}

CmdLineException problem = null;
try {
parser.parseArgument(args);
// First try, use options form environment and command line.
Collection<String> arguments = new ArrayList<>(envArgs);
arguments.addAll(Arrays.asList(args));
parser.parseArgument(arguments);
} catch (CmdLineException ex) {
// Happens, when required configurations are not given on the command line.
problem = ex;
Expand Down Expand Up @@ -96,4 +127,17 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O
return metaConfig;
}

public static String getValueFromEnvironment(String optionName) {
if (!optionName.startsWith("--")) {
return null;
}

String env = optionName.substring(2).replace('-', '_').toUpperCase();
String value = System.getenv(env);
if (value != null) {
LOG.info("Using '" + optionName + "' from environment: " + env + "=" + value);
}
return value;
}

}

0 comments on commit 3e10ba0

Please sign in to comment.