Make it possible to specicify where to find the config
Example: java -jar chipmunkbot.jar /etc/chipmunkbot/config.json
This commit is contained in:
parent
a67b45c4d6
commit
ada80cf431
1 changed files with 5 additions and 7 deletions
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue