Skip to content

Commit e505b23

Browse files
committed
feat: Add support for custom config file path.
1 parent a6b5a56 commit e505b23

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed

Backend/src/main/java/de/presti/ree6/backend/Server.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.jagrosh.jdautilities.oauth2.OAuth2Client;
1111
import de.presti.ree6.backend.bot.BotWorker;
1212
import de.presti.ree6.backend.bot.version.BotVersion;
13+
import de.presti.ree6.backend.utils.OptionParser;
1314
import de.presti.ree6.backend.utils.data.*;
1415
import de.presti.ree6.sql.DatabaseTyp;
1516
import de.presti.ree6.sql.SQLSession;
@@ -22,8 +23,6 @@
2223
import lombok.extern.slf4j.Slf4j;
2324

2425
import java.time.Duration;
25-
import java.util.Arrays;
26-
import java.util.List;
2726
import java.util.Optional;
2827

2928
/**
@@ -94,8 +93,10 @@ public Server(String[] args) {
9493
public void load(String[] args) {
9594
log.info("Starting Backend {}", backendVersion);
9695

96+
OptionParser options = new OptionParser(args, true);
97+
9798
// Create Config Instance.
98-
config = new Config();
99+
config = new Config(options.getValueOrDefault("config", "config.yml"));
99100

100101
// Initialize the Config.
101102
config.init();
@@ -138,15 +139,13 @@ public void load(String[] args) {
138139

139140
// Create a new JDA Session.
140141
try {
141-
List<String> argList = Arrays.stream(args).map(String::toLowerCase).toList();
142-
143142
int shards = instance.config.getConfiguration().getInt("discord.bot.client.shards", 1);
144143

145144
BotVersion version = BotVersion.RELEASE;
146145

147-
if (argList.contains("--dev")) {
146+
if (options.isEnabled("dev")) {
148147
version = BotVersion.DEVELOPMENT_BUILD;
149-
} else if (argList.contains("--beta")) {
148+
} else if (options.isEnabled("beta")) {
150149
version = BotVersion.BETA_BUILD;
151150
}
152151

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package de.presti.ree6.backend.utils;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Class used to parse options.
7+
* @author CodeManDev
8+
* <a href="https://github.com/DuzeyYT/RiseFreeLauncher/blob/master/src/main/java/cc/fish/rfl/api/utils/OptionParser.java">Source</a>
9+
*/
10+
public class OptionParser {
11+
private final HashMap<String, String> options;
12+
13+
/**
14+
* Constructor to parse the options.
15+
* @param args the CLI Arguments.
16+
* @param addEnv if environment values should be added to the list.
17+
*/
18+
public OptionParser(String[] args, boolean addEnv) {
19+
this.options = new HashMap<>();
20+
21+
for (String arg : args) {
22+
if (arg.startsWith("--")) {
23+
String key = arg.substring(2);
24+
String value = "";
25+
if (key.contains("=")) {
26+
key = key.split("=")[0];
27+
value = key.substring(key.indexOf("=") + 1);
28+
}
29+
this.options.put(key, value);
30+
}
31+
}
32+
33+
if (addEnv) {
34+
options.putAll(System.getenv());
35+
}
36+
}
37+
38+
/**
39+
* Check if an option is present.
40+
* @param name the name of the option.
41+
* @return true, if present.
42+
*/
43+
public boolean isEnabled(String name) {
44+
return this.options.containsKey(name);
45+
}
46+
47+
/**
48+
* Get the value of an option.
49+
* @param name the name of the option.
50+
* @return value.
51+
*/
52+
public String getValue(String name) {
53+
return this.options.get(name);
54+
}
55+
56+
/**
57+
* Get the value of an option or a default.
58+
* @param name the name of the option.
59+
* @param defaultValue default value if the option isn't set.
60+
* @return value.
61+
*/
62+
public String getValueOrDefault(String name, String defaultValue) {
63+
return this.options.getOrDefault(name, defaultValue);
64+
}
65+
}

Backend/src/main/java/de/presti/ree6/backend/utils/data/Config.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ public class Config {
2121
*/
2222
private YamlFile yamlFile;
2323

24+
25+
/**
26+
* The Config Location.
27+
*/
28+
private String configFileLocation = "config.yml";
29+
30+
/**
31+
* Create an Instance of the Config.
32+
*/
33+
public Config() {
34+
}
35+
36+
/**
37+
* Create an Instance of the Config.
38+
* @param configFileLocation the Config Location.
39+
*/
40+
public Config(String configFileLocation) {
41+
this.configFileLocation = configFileLocation;
42+
}
43+
2444
/**
2545
* Initialize the Configuration.
2646
*/
@@ -230,7 +250,7 @@ public YamlFile getConfiguration() {
230250
* @return The Configuration File as {@link File}.
231251
*/
232252
public File getFile() {
233-
return new File("config.yml");
253+
return new File(configFileLocation);
234254
}
235255

236256
}

0 commit comments

Comments
 (0)