Simplify config file loading

This commit is contained in:
kageru 2019-07-17 22:51:36 +02:00
parent c400ab7369
commit d6320bb5de
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

View File

@ -17,15 +17,17 @@ class RawConfig(
fun readFromString(tomlContent: String): RawConfig = Toml().read(tomlContent).to(RawConfig::class.java)
private fun getFile(path: String): File {
val file = File(path)
if (file.isFile) {
return file
}
println("Config not found, falling back to defaults...")
return File(this::class.java.classLoader.getResource(path)!!.toURI())
}
fun read(path: String = DEFAULT_CONFIG_PATH): RawConfig {
val toml: Toml = Toml().read(run {
val file = File(path)
if (file.isFile) {
return@run file
}
println("Config not found, falling back to defaults...")
File(this::class.java.classLoader.getResource(path)!!.toURI())
})
val toml: Toml = Toml().read(getFile(path))
return toml.to(RawConfig::class.java)
}
}