2019-07-13 15:39:50 +02:00
package moe.kageru.kagebot.config
2019-11-11 18:10:28 +01:00
import arrow.core.Either
import kotlinx.coroutines.runBlocking
2019-07-13 15:39:50 +02:00
import moe.kageru.kagebot.Globals
2019-10-06 16:14:01 +02:00
import moe.kageru.kagebot.config.SystemSpec.serverId
2019-07-14 17:14:23 +02:00
import java.io.File
2019-07-13 15:39:50 +02:00
object ConfigParser {
2019-10-18 21:56:31 +02:00
internal const val DEFAULT _CONFIG _PATH = " config.toml "
val configFile : File = File ( DEFAULT _CONFIG _PATH )
2019-07-14 17:14:23 +02:00
2019-11-11 18:10:28 +01:00
fun initialLoad ( file : String ) = runBlocking {
Either . catch {
val configFile = getFile ( file )
val config = Config . systemSpec . file ( configFile )
Config . system = config
Config . server = Globals . api . getServerById ( config [ serverId ] )
. orElseThrow { IllegalArgumentException ( " Invalid server configured. " ) }
Config . localization = Config . localeSpec . file ( configFile )
Config . featureConfig = Config . featureSpec . file ( configFile )
Config . commandConfig = Config . commandSpec . file ( configFile )
}
2019-07-13 15:39:50 +02:00
}
2019-10-18 21:56:31 +02:00
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 ( ) )
2019-07-13 15:39:50 +02:00
}
}