Add JVM parameter to define the configuration directory

Add a new JVM parameter (-Dctbrec.config.dir) to define the directory
to save the config to / load it from. If this parameter is not set, the
system default directory determined by OS.getConfigDir() is used.
This commit is contained in:
0xboobface 2018-11-06 14:32:35 +01:00
parent 682f78bdac
commit 6b16a637f0
1 changed files with 7 additions and 2 deletions

View File

@ -29,9 +29,16 @@ public class Config {
private Settings settings; private Settings settings;
private String filename; private String filename;
private List<Site> sites; private List<Site> sites;
private File configDir;
private Config(List<Site> sites) throws FileNotFoundException, IOException { private Config(List<Site> sites) throws FileNotFoundException, IOException {
this.sites = sites; this.sites = sites;
if(System.getProperty("ctbrec.config.dir") != null) {
configDir = new File(System.getProperty("ctbrec.config.dir"));
} else {
configDir = OS.getConfigDir();
}
if(System.getProperty("ctbrec.config") != null) { if(System.getProperty("ctbrec.config") != null) {
filename = System.getProperty("ctbrec.config"); filename = System.getProperty("ctbrec.config");
} else { } else {
@ -45,7 +52,6 @@ public class Config {
.add(Model.class, new ModelJsonAdapter(sites)) .add(Model.class, new ModelJsonAdapter(sites))
.build(); .build();
JsonAdapter<Settings> adapter = moshi.adapter(Settings.class); JsonAdapter<Settings> adapter = moshi.adapter(Settings.class);
File configDir = OS.getConfigDir();
File configFile = new File(configDir, filename); File configFile = new File(configDir, filename);
LOG.debug("Loading config from {}", configFile.getAbsolutePath()); LOG.debug("Loading config from {}", configFile.getAbsolutePath());
if(configFile.exists()) { if(configFile.exists()) {
@ -86,7 +92,6 @@ public class Config {
.build(); .build();
JsonAdapter<Settings> adapter = moshi.adapter(Settings.class).indent(" "); JsonAdapter<Settings> adapter = moshi.adapter(Settings.class).indent(" ");
String json = adapter.toJson(settings); String json = adapter.toJson(settings);
File configDir = OS.getConfigDir();
File configFile = new File(configDir, filename); File configFile = new File(configDir, filename);
LOG.debug("Saving config to {}", configFile.getAbsolutePath()); LOG.debug("Saving config to {}", configFile.getAbsolutePath());
Files.createDirectories(configDir.toPath()); Files.createDirectories(configDir.toPath());