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;
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);