@@ -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 *
0 commit comments