Skip to content

Commit

Permalink
#5 - Need to filter non property command line args - IllegalArgumentE…
Browse files Browse the repository at this point in the history
…xception: Expecting only yaml or properties file but got [ort]
  • Loading branch information
rbygrave committed Apr 14, 2020
1 parent 95c132c commit a2ee678
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/main/java/io/avaje/config/load/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,32 @@ private void loadViaCommandLineArgs() {
}
}

private void loadViaCommandLine(String[] args) {
void loadViaCommandLine(String[] args) {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (arg.startsWith("-P") || arg.startsWith("-p")) {
if (arg.length() == 2 && i < args.length - 1) {
// next argument expected to be a properties file paths
i++;
loadViaPaths(args[i]);
loadCommandLineArg(args[i]);
} else {
// no space between -P and properties file paths
loadViaPaths(arg.substring(2));
loadCommandLineArg(arg.substring(2));
}
}
}
}

private void loadCommandLineArg(String arg) {
if (isValidExtension(arg)) {
loadViaPaths(arg);
}
}

private boolean isValidExtension(String arg) {
return arg.endsWith(".yaml") || arg.endsWith(".yml") || arg.endsWith(".properties");
}

/**
* Provides a way to override properties when running via main() locally.
*/
Expand Down Expand Up @@ -193,6 +203,10 @@ private void loadViaPaths(String paths) {
}
}

int size() {
return loadContext.size();
}

String[] splitPaths(String location) {
return SPLIT_PATHS.split(location);
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/io/avaje/config/load/LoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,27 @@ public void splitPaths() {
assertThat(loader.splitPaths("one;two;three")).contains("one", "two", "three");
assertThat(loader.splitPaths("one two,three;four,five six")).contains("one", "two", "three", "four", "five", "six");
}

@Test
public void loadViaCommandLine_whenNotValid() {
Loader loader = new Loader();
loader.loadViaCommandLine(new String[]{"-p","8765"});
assertEquals(0, loader.size());
loader.loadViaCommandLine(new String[]{"-port","8765"});
assertEquals(0, loader.size());

loader.loadViaCommandLine(new String[]{"-port"});
loader.loadViaCommandLine(new String[]{"-p","ort"});
assertEquals(0, loader.size());

loader.loadViaCommandLine(new String[]{"-p","doesNotExist.yaml"});
assertEquals(0, loader.size());
}

@Test
public void loadViaCommandLine_localFile() {
Loader loader = new Loader();
loader.loadViaCommandLine(new String[]{"-p","test-dummy2.yaml"});
assertEquals(1, loader.size());
}
}

0 comments on commit a2ee678

Please sign in to comment.