From ada80cf43137b57472df6f17a434a6c9b160361c Mon Sep 17 00:00:00 2001 From: Chip <65827213+ChipmunkMC@users.noreply.github.com> Date: Fri, 13 Jan 2023 15:25:02 -0500 Subject: [PATCH] Make it possible to specicify where to find the config Example: java -jar chipmunkbot.jar /etc/chipmunkbot/config.json --- src/main/java/land/chipmunk/chipmunkbot/Main.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkbot/Main.java b/src/main/java/land/chipmunk/chipmunkbot/Main.java index df79145..c97dbad 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Main.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Main.java @@ -21,10 +21,8 @@ import com.google.gson.JsonParser; import com.google.gson.Gson; public class Main { - private static final File CONFIG_FILE = new File("config.json"); - - private static JsonObject getConfig () throws Exception { - if (!CONFIG_FILE.exists()) { + private static JsonObject getConfig (File file) throws Exception { + if (!file.exists()) { // Read the default config InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default_config.json"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); @@ -36,7 +34,7 @@ public class Main { String defaultConfig = stringBuilder.toString(); // Write the default config - BufferedWriter configWriter = new BufferedWriter(new FileWriter(CONFIG_FILE)); + BufferedWriter configWriter = new BufferedWriter(new FileWriter(file)); configWriter.write(defaultConfig); configWriter.close(); @@ -46,7 +44,7 @@ public class Main { return JsonParser.parseString(defaultConfig).getAsJsonObject(); } - InputStream opt = new FileInputStream(CONFIG_FILE); + InputStream opt = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(opt)); return JsonParser.parseReader(reader).getAsJsonObject(); @@ -64,7 +62,7 @@ public class Main { JsonObject config = null; try { - config = getConfig(); + config = getConfig(new File(arguments.length > 0 ? arguments[0] : "config.json")); } catch (Exception exception) { exception.printStackTrace(); System.exit(1);