Make it possible to specicify where to find the config

Example: java -jar chipmunkbot.jar /etc/chipmunkbot/config.json
This commit is contained in:
chipmunk 2023-01-13 15:25:02 -05:00
parent a67b45c4d6
commit ada80cf431

View file

@ -21,10 +21,8 @@ import com.google.gson.JsonParser;
import com.google.gson.Gson; import com.google.gson.Gson;
public class Main { public class Main {
private static final File CONFIG_FILE = new File("config.json"); private static JsonObject getConfig (File file) throws Exception {
if (!file.exists()) {
private static JsonObject getConfig () throws Exception {
if (!CONFIG_FILE.exists()) {
// Read the default config // Read the default config
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default_config.json"); InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default_config.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); BufferedReader reader = new BufferedReader(new InputStreamReader(is));
@ -36,7 +34,7 @@ public class Main {
String defaultConfig = stringBuilder.toString(); String defaultConfig = stringBuilder.toString();
// Write the default config // Write the default config
BufferedWriter configWriter = new BufferedWriter(new FileWriter(CONFIG_FILE)); BufferedWriter configWriter = new BufferedWriter(new FileWriter(file));
configWriter.write(defaultConfig); configWriter.write(defaultConfig);
configWriter.close(); configWriter.close();
@ -46,7 +44,7 @@ public class Main {
return JsonParser.parseString(defaultConfig).getAsJsonObject(); return JsonParser.parseString(defaultConfig).getAsJsonObject();
} }
InputStream opt = new FileInputStream(CONFIG_FILE); InputStream opt = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(opt)); BufferedReader reader = new BufferedReader(new InputStreamReader(opt));
return JsonParser.parseReader(reader).getAsJsonObject(); return JsonParser.parseReader(reader).getAsJsonObject();
@ -64,7 +62,7 @@ public class Main {
JsonObject config = null; JsonObject config = null;
try { try {
config = getConfig(); config = getConfig(new File(arguments.length > 0 ? arguments[0] : "config.json"));
} catch (Exception exception) { } catch (Exception exception) {
exception.printStackTrace(); exception.printStackTrace();
System.exit(1); System.exit(1);