Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2102 PropertiesDefaultProvider at first try to load properties from … #2107

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -18972,11 +18972,29 @@ private static Properties loadProperties(CommandSpec commandSpec) {
if (commandSpec == null) { return null; }
Properties p = System.getProperties();
for (String name : commandSpec.names()) {
String propertiesFileName = "." + name + ".properties";
String path = p.getProperty("picocli.defaults." + name + ".path");
File defaultPath = new File(p.getProperty("user.home"), "." + name + ".properties");
File defaultPath = new File(p.getProperty("user.home"), propertiesFileName);
File file = path == null ? defaultPath : new File(path);
if (file.canRead()) {
return createProperties(file, commandSpec);
} else {
Object userObject = commandSpec.userObject();
rimuln marked this conversation as resolved.
Show resolved Hide resolved
if (userObject == null) {
userObject = commandSpec.commandLine;
}
URL resource = userObject.getClass().getClassLoader().getResource(propertiesFileName);
Tracer tracer = CommandLine.tracer();
if (resource != null) {
file = new File(resource.getFile());
if (file.canRead()) {
return createProperties(file, commandSpec);
} else {
tracer.warn("could not read defaults from %s", file.getAbsolutePath());
}
} else {
tracer.warn("defaults configuration file %s doesn not exists on classpath", propertiesFileName);
}
}
}
return loadProperties(commandSpec.parent());
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/picocli/PropertiesDefaultProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ static class Subcommand {
@Parameters(index = "0", paramLabel = "qqq", descriptionKey = "xxx") int xxx;
}

@Test
public void testLoadFromResourceClasspathIfPropertySpecified() throws IOException {
Properties expected = new Properties();
expected.setProperty("aaa", "111");
expected.setProperty("bbb", "222");
expected.setProperty("ppp", "333");
expected.setProperty("xxx", "444");

MyApp myApp = new MyApp();
assertEquals(myApp.aaa, 0);
assertEquals(myApp.bbb, 0);
assertEquals(myApp.ppp, 0);
assertEquals(myApp.xxx, 0);
new CommandLine(myApp).parseArgs();

assertEquals(myApp.aaa, 111);
assertEquals(myApp.bbb, 222);
assertEquals(myApp.ppp, 333);
assertEquals(myApp.xxx, 444);
}

@Test
public void testLoadFromUserHomeCommandNameByDefault() throws IOException {
File f = new File(System.getProperty("user.home"), ".providertest.properties");
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/.providertest.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#exported from test
#Mon Sep 18 12:37:21 CEST 2023
aaa=111
ppp=333
bbb=222
xxx=444