Skip to content

Commit

Permalink
Merge pull request #19 from hduelme/improve-diagnose-for-invalide-env…
Browse files Browse the repository at this point in the history
…ironment/command-line-arguments

improve diagnose for invalide environment/command line arguments
  • Loading branch information
haumacher authored Nov 22, 2024
2 parents 3e10ba0 + 9bb3532 commit 3d92bad
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions mjsip-util/src/main/java/org/mjsip/config/OptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O
}

CmdLineException problem = null;
boolean invalidValueGiven = false;
try {
// First try, use options form environment and command line.
Collection<String> arguments = new ArrayList<>(envArgs);
Expand All @@ -71,13 +72,18 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O
} catch (CmdLineException ex) {
// Happens, when required configurations are not given on the command line.
problem = ex;
String message = ex.getMessage();
invalidValueGiven = message != null && message.contains(" is not a valid value for ");
if (invalidValueGiven) {
LOG.warn("{}. Reading from environment/command line aborted", message);
}
}

String argFile = metaConfig.configFile;

File file;
if (argFile != null && !argFile.isBlank()) {
if ("none".equals(argFile.toLowerCase())) {
if ("none".equalsIgnoreCase(argFile)) {
file = null;
} else {
file = new File(argFile);
Expand All @@ -87,7 +93,7 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O
}
}
} else if (defaultConfigFile != null) {
String fileName = System.getProperty("user.home") + "/" + defaultConfigFile;
String fileName = System.getProperty("user.home") + File.pathSeparator + defaultConfigFile;
file = new File(fileName);
if (!file.exists()) {
file = null;
Expand All @@ -97,13 +103,21 @@ public static MetaConfig parseOptions(String[] args, String defaultConfigFile, O
}

if (file!= null) {
LOG.debug("Reading options from: " + file.getAbsolutePath());
if (invalidValueGiven) {
LOG.warn("Reading options from: {}", file.getAbsolutePath());
} else {
LOG.debug("Reading options from: {}", file.getAbsolutePath());
}

ConfigFile configFile = new ConfigFile(file);

// Parse all arguments again to check for required arguments not given but give
// precedence to arguments given on the command line.
// Parse all arguments again to check for required arguments not given and to
// allow overwriting configuration file arguments with environment and command
// line.
// Use the following precedence order: Arguments in the configuration file (lowest),
// arguments from the environment, arguments given on the command line (highest).
Collection<String> arguments = new ArrayList<>(configFile.toArguments());
arguments.addAll(envArgs);
arguments.addAll(Arrays.asList(args));

parser.parseArgument(arguments);
Expand Down Expand Up @@ -135,7 +149,7 @@ public static String getValueFromEnvironment(String optionName) {
String env = optionName.substring(2).replace('-', '_').toUpperCase();
String value = System.getenv(env);
if (value != null) {
LOG.info("Using '" + optionName + "' from environment: " + env + "=" + value);
LOG.info("Using '{}' from environment: {}={}", optionName, env, value);
}
return value;
}
Expand Down

0 comments on commit 3d92bad

Please sign in to comment.