Skip to content

Commit a2ee678

Browse files
committed
#5 - Need to filter non property command line args - IllegalArgumentException: Expecting only yaml or properties file but got [ort]
1 parent 95c132c commit a2ee678

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/main/java/io/avaje/config/load/Loader.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,32 @@ private void loadViaCommandLineArgs() {
127127
}
128128
}
129129

130-
private void loadViaCommandLine(String[] args) {
130+
void loadViaCommandLine(String[] args) {
131131
for (int i = 0; i < args.length; i++) {
132132
String arg = args[i];
133133
if (arg.startsWith("-P") || arg.startsWith("-p")) {
134134
if (arg.length() == 2 && i < args.length - 1) {
135135
// next argument expected to be a properties file paths
136136
i++;
137-
loadViaPaths(args[i]);
137+
loadCommandLineArg(args[i]);
138138
} else {
139139
// no space between -P and properties file paths
140-
loadViaPaths(arg.substring(2));
140+
loadCommandLineArg(arg.substring(2));
141141
}
142142
}
143143
}
144144
}
145145

146+
private void loadCommandLineArg(String arg) {
147+
if (isValidExtension(arg)) {
148+
loadViaPaths(arg);
149+
}
150+
}
151+
152+
private boolean isValidExtension(String arg) {
153+
return arg.endsWith(".yaml") || arg.endsWith(".yml") || arg.endsWith(".properties");
154+
}
155+
146156
/**
147157
* Provides a way to override properties when running via main() locally.
148158
*/
@@ -193,6 +203,10 @@ private void loadViaPaths(String paths) {
193203
}
194204
}
195205

206+
int size() {
207+
return loadContext.size();
208+
}
209+
196210
String[] splitPaths(String location) {
197211
return SPLIT_PATHS.split(location);
198212
}

src/test/java/io/avaje/config/load/LoaderTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,27 @@ public void splitPaths() {
8787
assertThat(loader.splitPaths("one;two;three")).contains("one", "two", "three");
8888
assertThat(loader.splitPaths("one two,three;four,five six")).contains("one", "two", "three", "four", "five", "six");
8989
}
90+
91+
@Test
92+
public void loadViaCommandLine_whenNotValid() {
93+
Loader loader = new Loader();
94+
loader.loadViaCommandLine(new String[]{"-p","8765"});
95+
assertEquals(0, loader.size());
96+
loader.loadViaCommandLine(new String[]{"-port","8765"});
97+
assertEquals(0, loader.size());
98+
99+
loader.loadViaCommandLine(new String[]{"-port"});
100+
loader.loadViaCommandLine(new String[]{"-p","ort"});
101+
assertEquals(0, loader.size());
102+
103+
loader.loadViaCommandLine(new String[]{"-p","doesNotExist.yaml"});
104+
assertEquals(0, loader.size());
105+
}
106+
107+
@Test
108+
public void loadViaCommandLine_localFile() {
109+
Loader loader = new Loader();
110+
loader.loadViaCommandLine(new String[]{"-p","test-dummy2.yaml"});
111+
assertEquals(1, loader.size());
112+
}
90113
}

0 commit comments

Comments
 (0)