Skip to content

Commit a73c908

Browse files
committed
make this backward compatible
1 parent 5c0eca4 commit a73c908

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

application/src/main/java/org/togetherjava/tjbot/Application.java

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,44 @@ private Application() {
3636
}
3737

3838
private static final Logger logger = LoggerFactory.getLogger(Application.class);
39-
private static final String DEFAULT_CONFIG_PATH = "/config.json";
39+
private static final String DEFAULT_CONFIG_PATH = "config.json";
4040
private static final String DEFAULT_SECRETS_PATH = "secrets.json";
4141

4242
/**
4343
* Starts the application.
44+
* <p>
45+
* Note: By default the configuration file will be loaded from a config.json unless overridden
46+
* by either: 1. Setting the USE_INCLUDED_CONFIG environment variable to true, which will use
47+
* the config.json packed in the built jar. 2. Passing a program argument including the path to
48+
* the config file.
4449
*
4550
* @param args command line arguments - [the path to the configuration file (optional, by
4651
* default "config.json")]
4752
*/
4853
public static void main(final String[] args) {
49-
if (args.length > 1) {
50-
throw new IllegalArgumentException("Expected no or one argument but " + args.length
51-
+ " arguments were provided. The first argument is the path to the configuration file. If no argument was provided, '"
52-
+ DEFAULT_CONFIG_PATH + "' will be assumed.");
54+
boolean useIncludedConfig;
55+
try {
56+
useIncludedConfig = Boolean.parseBoolean(System.getenv("USE_INCLUDED_CONFIG"));
57+
logger.info("Using config.json included in jar");
58+
} catch (Exception _) {
59+
useIncludedConfig = false;
5360
}
5461

55-
String configPath = args.length == 1 ? args[0] : DEFAULT_CONFIG_PATH;
56-
Config config;
57-
58-
try (InputStream stream = Application.class.getResourceAsStream(configPath)) {
59-
if (stream == null) {
60-
throw new IOException("InputStream is null when loading " + configPath);
61-
}
62+
String configPath;
6263

63-
config = Config.load(new String(stream.readAllBytes(), StandardCharsets.UTF_8));
64+
if (args.length > 0) {
65+
configPath = args[0];
66+
} else if (useIncludedConfig) {
67+
configPath = "/" + DEFAULT_CONFIG_PATH;
68+
} else {
69+
configPath = DEFAULT_CONFIG_PATH;
70+
}
6471

72+
Config config;
73+
try {
74+
config = loadConfig(useIncludedConfig, configPath);
6575
} catch (IOException e) {
66-
logger.error("Unable to load the configuration file from path '{}'", configPath, e);
76+
logger.error("Unable to load the configuration file '{}'", configPath, e);
6777
return;
6878
}
6979

@@ -84,6 +94,47 @@ public static void main(final String[] args) {
8494
runBot(config, secrets);
8595
}
8696

97+
/**
98+
* Attempts to load the configuration file and return a new {@code Config}.
99+
*
100+
* @param useIncludedConfig if the config should be loaded from the resources' directory.
101+
* @param configPath the location of the config file.
102+
* @return a new {@code Config} object
103+
* @throws IOException if the configuration file could not be loaded.
104+
*/
105+
private static Config loadConfig(boolean useIncludedConfig, String configPath)
106+
throws IOException {
107+
return useIncludedConfig ? loadConfigFromResource(configPath)
108+
: loadConfigFromFile(Path.of(configPath));
109+
}
110+
111+
/**
112+
* Loads a configuration file from the application resources directory.
113+
*
114+
* @param configPath the location of the configuration file
115+
* @return a new {@code Config} object
116+
* @throws IOException if the configuration file could not be loaded
117+
*/
118+
private static Config loadConfigFromResource(String configPath) throws IOException {
119+
try (InputStream stream = Application.class.getResourceAsStream(configPath)) {
120+
if (stream == null) {
121+
throw new IOException("InputStream is null when loading " + configPath);
122+
}
123+
return Config.load(new String(stream.readAllBytes(), StandardCharsets.UTF_8));
124+
}
125+
}
126+
127+
/**
128+
* Loads a configuration file from a specified path.
129+
*
130+
* @param configPath the location of the configuration file
131+
* @return a new {@code Config} object
132+
* @throws IOException if the configuration file could not be loaded
133+
*/
134+
private static Config loadConfigFromFile(Path configPath) throws IOException {
135+
return Config.load(configPath);
136+
}
137+
87138
/**
88139
* Runs an instance of the bot, connecting to the given token and using the given database.
89140
*

application/src/main/java/org/togetherjava/tjbot/config/Config.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
77

88
import java.io.IOException;
9+
import java.nio.file.Path;
910
import java.util.Collections;
1011
import java.util.List;
1112
import java.util.Objects;
@@ -120,7 +121,7 @@ private Config(@JsonProperty(value = "databasePath", required = true) String dat
120121
}
121122

122123
/**
123-
* Loads the configuration from the given file.
124+
* Loads the configuration from the String payload.
124125
*
125126
* @param content the configuration file, as Stringified JSON object
126127
* @return the loaded configuration
@@ -131,6 +132,18 @@ public static Config load(String content) throws IOException {
131132
.readValue(content, Config.class);
132133
}
133134

135+
/**
136+
* Loads the configuration from the given file.
137+
*
138+
* @param path the configuration file
139+
* @return the loaded configuration
140+
* @throws IOException if the file could not be loaded
141+
*/
142+
public static Config load(Path path) throws IOException {
143+
return new ObjectMapper().registerModule(new JavaTimeModule())
144+
.readValue(path.toFile(), Config.class);
145+
}
146+
134147
/**
135148
* Gets the REGEX pattern used to identify the role assigned to muted users.
136149
*

0 commit comments

Comments
 (0)