mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-07-04 03:14:40 -05:00
Make sure "UserConfig" is up-to-date (#590)
Currently, the "UserConfig" was created in case it was missing. But in case settings changed (added/removed), an already existing "UserConfig" never reflected these changes and thus, was out of date
This commit is contained in:
@@ -13,6 +13,7 @@ import com.typesafe.config.ConfigFactory
|
|||||||
import com.typesafe.config.ConfigRenderOptions
|
import com.typesafe.config.ConfigRenderOptions
|
||||||
import com.typesafe.config.ConfigValue
|
import com.typesafe.config.ConfigValue
|
||||||
import com.typesafe.config.ConfigValueFactory
|
import com.typesafe.config.ConfigValueFactory
|
||||||
|
import com.typesafe.config.parser.ConfigDocument
|
||||||
import com.typesafe.config.parser.ConfigDocumentFactory
|
import com.typesafe.config.parser.ConfigDocumentFactory
|
||||||
import mu.KotlinLogging
|
import mu.KotlinLogging
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -43,6 +44,12 @@ open class ConfigManager {
|
|||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
fun <T : ConfigModule> module(type: Class<T>): T = loadedModules[type] as T
|
fun <T : ConfigModule> module(type: Class<T>): T = loadedModules[type] as T
|
||||||
|
|
||||||
|
private fun getUserConfig(): Config {
|
||||||
|
return userConfigFile.let {
|
||||||
|
ConfigFactory.parseFile(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load configs
|
* Load configs
|
||||||
*/
|
*/
|
||||||
@@ -58,10 +65,7 @@ open class ConfigManager {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Load user config
|
// Load user config
|
||||||
val userConfig =
|
val userConfig = getUserConfig()
|
||||||
userConfigFile.let {
|
|
||||||
ConfigFactory.parseFile(it)
|
|
||||||
}
|
|
||||||
|
|
||||||
val config = ConfigFactory.empty()
|
val config = ConfigFactory.empty()
|
||||||
.withFallback(baseConfig)
|
.withFallback(baseConfig)
|
||||||
@@ -105,6 +109,35 @@ open class ConfigManager {
|
|||||||
updateUserConfigFile(path, configValue)
|
updateUserConfigFile(path, configValue)
|
||||||
internalConfig = internalConfig.withValue(path, configValue)
|
internalConfig = internalConfig.withValue(path, configValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes sure the "UserConfig" is up-to-date.
|
||||||
|
*
|
||||||
|
* - adds missing settings
|
||||||
|
* - removes outdated settings
|
||||||
|
*/
|
||||||
|
fun updateUserConfig() {
|
||||||
|
val serverConfigFile = File(javaClass.classLoader.getResource("server-reference.conf")?.file ?: return)
|
||||||
|
val serverConfig = ConfigFactory.parseFile(serverConfigFile)
|
||||||
|
val userConfig = getUserConfig()
|
||||||
|
|
||||||
|
val hasMissingSettings = serverConfig.entrySet().any { !userConfig.hasPath(it.key) }
|
||||||
|
val hasOutdatedSettings = userConfig.entrySet().any { !serverConfig.hasPath(it.key) }
|
||||||
|
val isUserConfigOutdated = hasMissingSettings || hasOutdatedSettings
|
||||||
|
if (!isUserConfigOutdated) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug { "user config is out of date, updating... (missingSettings= $hasMissingSettings, outdatedSettings= $hasOutdatedSettings" }
|
||||||
|
|
||||||
|
val serverConfigDoc = ConfigDocumentFactory.parseFile(serverConfigFile)
|
||||||
|
userConfigFile.writeText(serverConfigDoc.render())
|
||||||
|
|
||||||
|
var newUserConfigDoc: ConfigDocument = serverConfigDoc
|
||||||
|
userConfig.entrySet().filter { serverConfig.hasPath(it.key) }.forEach { newUserConfigDoc = newUserConfigDoc.withValue(it.key, it.value) }
|
||||||
|
|
||||||
|
userConfigFile.writeText(newUserConfigDoc.render())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object GlobalConfigManager : ConfigManager()
|
object GlobalConfigManager : ConfigManager()
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ fun applicationSetup() {
|
|||||||
// start app
|
// start app
|
||||||
androidCompat.startApp(App())
|
androidCompat.startApp(App())
|
||||||
|
|
||||||
// create conf file if doesn't exist
|
// create or update conf file if doesn't exist
|
||||||
try {
|
try {
|
||||||
val dataConfFile = File("${applicationDirs.dataRoot}/server.conf")
|
val dataConfFile = File("${applicationDirs.dataRoot}/server.conf")
|
||||||
if (!dataConfFile.exists()) {
|
if (!dataConfFile.exists()) {
|
||||||
@@ -113,6 +113,9 @@ fun applicationSetup() {
|
|||||||
input.copyTo(output)
|
input.copyTo(output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// make sure the user config file is up-to-date
|
||||||
|
GlobalConfigManager.updateUserConfig()
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logger.error("Exception while creating initial server.conf", e)
|
logger.error("Exception while creating initial server.conf", e)
|
||||||
|
|||||||
Reference in New Issue
Block a user