Skip to content

Commit

Permalink
Make sure "UserConfig" is up-to-date (#590)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
schroda authored Jul 2, 2023
1 parent 5372ef8 commit b4d37f9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigRenderOptions
import com.typesafe.config.ConfigValue
import com.typesafe.config.ConfigValueFactory
import com.typesafe.config.parser.ConfigDocument
import com.typesafe.config.parser.ConfigDocumentFactory
import mu.KotlinLogging
import java.io.File
Expand Down Expand Up @@ -43,6 +44,12 @@ open class ConfigManager {
@Suppress("UNCHECKED_CAST")
fun <T : ConfigModule> module(type: Class<T>): T = loadedModules[type] as T

private fun getUserConfig(): Config {
return userConfigFile.let {
ConfigFactory.parseFile(it)
}
}

/**
* Load configs
*/
Expand All @@ -58,10 +65,7 @@ open class ConfigManager {
)

// Load user config
val userConfig =
userConfigFile.let {
ConfigFactory.parseFile(it)
}
val userConfig = getUserConfig()

val config = ConfigFactory.empty()
.withFallback(baseConfig)
Expand Down Expand Up @@ -105,6 +109,35 @@ open class ConfigManager {
updateUserConfigFile(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()
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fun applicationSetup() {
// start app
androidCompat.startApp(App())

// create conf file if doesn't exist
// create or update conf file if doesn't exist
try {
val dataConfFile = File("${applicationDirs.dataRoot}/server.conf")
if (!dataConfFile.exists()) {
Expand All @@ -113,6 +113,9 @@ fun applicationSetup() {
input.copyTo(output)
}
}
} else {
// make sure the user config file is up-to-date
GlobalConfigManager.updateUserConfig()
}
} catch (e: Exception) {
logger.error("Exception while creating initial server.conf", e)
Expand Down

0 comments on commit b4d37f9

Please sign in to comment.