Discontinue config.yml file in favor of class mapping
continuous-integration/drone/push Build is passing Details

The configuration entries are now mapped in an enum that transfers very well to SnakeYaml's YAML parsing. This is better because we no longer run the risk of entries getting mistyped in classes, or renamed without replacing them everywhere...
This commit is contained in:
Bea 2022-11-22 23:40:44 +01:00
parent ae6647a51e
commit 6480795368
3 changed files with 34 additions and 15 deletions

View File

@ -0,0 +1,24 @@
package wtf.beatrice.hidekobot.datasource;
public enum ConfigurationEntry
{
BOT_TOKEN("bot-token", "MTAxMjUzNzI5MTMwODI4NjAyMw.GWeNuh.00000000000000000000000000000000000000"),
BOT_OWNER_ID("bot-owner-id", 100000000000000000L),
BOT_COLOR("bot-color", "PINK"),
HEARTBEAT_LINK("heartbeat-link", "https://your-heartbeat-api.com/api/push/apikey?status=up&msg=OK&ping="),
;
private String path;
private Object defaultValue;
ConfigurationEntry(String path, Object defaultValue)
{
this.path = path;
this.defaultValue = defaultValue;
}
public String getPath() { return path; }
public Object getDefaultValue() { return defaultValue; }
}

View File

@ -25,19 +25,18 @@ public class ConfigurationSource
public void initConfig()
{
// load the YAML file from the archive's resources folder
Yaml internalConfigYaml = new Yaml();
LinkedHashMap<String, Object> internalConfigContents = null; // map holding all file entries
try (InputStream internalConfigStream = getClass()
.getClassLoader()
.getResourceAsStream("config.yml"))
{ internalConfigContents = internalConfigYaml.load(internalConfigStream); }
catch (IOException e) {
logger.log(e.getMessage());
HidekoBot.shutdown();
return;
/*
* note: this is no longer technically a YAML file, but we are using a very similar structure
* to what SnakeYaml does, so that it can map all entries directly to a YAML file itself.
* we used to have a config.yml file in the "resources" folder, but that is no longer necessary.
*/
LinkedHashMap<String, Object> internalConfigContents = new LinkedHashMap<>(); // map holding all file entries
for(ConfigurationEntry entry : ConfigurationEntry.values())
{
internalConfigContents.put(entry.getPath(), entry.getDefaultValue());
}
if(internalConfigContents == null)
if(internalConfigContents.isEmpty())
{
logger.log("Error reading internal configuration!");
HidekoBot.shutdown();

View File

@ -1,4 +0,0 @@
bot-token: 'MTAxMjUzNzI5MTMwODI4NjAyMw.GWeNuh.00000000000000000000000000000000000000'
bot-owner-id: 000000000000000000
bot-color: 'PINK'
heartbeat-link: 'https://your-heartbeat-api.com/api/push/apikey?status=up&msg=OK&ping='