Skip to content

Commit

Permalink
Save plugin config during migration
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvisser committed Sep 11, 2023
1 parent 2d4f9f1 commit e6bb0f7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.rabobank.ret.configuration

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import io.quarkus.logging.Log
import io.rabobank.ret.util.OsUtils
import jakarta.annotation.PostConstruct
import jakarta.enterprise.context.Dependent
Expand Down Expand Up @@ -34,6 +35,7 @@ open class BasePluginConfig : Configurable {
fun migrateOldConfig() {
val keysToMigrate = keysToMigrate()
if (keysToMigrate.isNotEmpty()) {
Log.info("Migrating old configuration to plugin specific configuration")
keysToMigrate.forEach { (oldKey, newKey) ->
val oldValue = retConfig[oldKey]
if (oldValue != null) {
Expand All @@ -43,6 +45,7 @@ open class BasePluginConfig : Configurable {
}
}

config.save()
retConfig.save()
}
}
Expand All @@ -57,19 +60,22 @@ open class BasePluginConfig : Configurable {
open fun keysToMigrate() = emptyList<Pair<String, String>>()
}

class PluginConfig(pluginName: String, objectMapper: ObjectMapper, osUtils: OsUtils) {
private val config: MutableMap<String, Any?>
class PluginConfig(pluginName: String, private val objectMapper: ObjectMapper, osUtils: OsUtils) {
private val pluginFile = osUtils.getPluginConfig(pluginName).toFile()
val config = runCatching { objectMapper.readValue<Map<String, Any?>>(pluginFile) }
.getOrDefault(emptyMap())
.toMutableMap()

init {
config = runCatching { objectMapper.readValue<Map<String, Any?>>(osUtils.getPluginConfig(pluginName).toFile()) }
.getOrDefault(emptyMap())
.toMutableMap()
inline operator fun <reified T> get(key: String): T? {
val value = config[key]
return if (value is T?) value else error("The config value cannot be cast to ${T::class.java}")
}

@Suppress("UNCHECKED_CAST")
operator fun <T> get(key: String): T? = config[key] as T?

operator fun set(key: String, value: Any?) {
config[key] = value
}

fun save() {
objectMapper.writeValue(pluginFile, config)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.rabobank.ret.commands
import io.quarkus.runtime.annotations.RegisterForReflection
import io.rabobank.ret.RetContext
import io.rabobank.ret.configuration.RetConfig
import io.rabobank.ret.util.Logged
import picocli.CommandLine.Command
import picocli.CommandLine.Model.CommandSpec
import picocli.CommandLine.Spec
Expand All @@ -13,6 +14,7 @@ import java.io.File
hidden = true,
)
@RegisterForReflection(targets = [RetContext::class])
@Logged
class ConfigureProjectCommand(
private val retConfig: RetConfig,
) : Runnable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.quarkus.logging.Log
import io.rabobank.ret.RetConsole
import io.rabobank.ret.configuration.Config
import io.rabobank.ret.configuration.ConfigurationProperty
import io.rabobank.ret.util.Logged
import picocli.CommandLine.Command
import picocli.CommandLine.Model.CommandSpec
import picocli.CommandLine.Spec
Expand All @@ -23,6 +24,7 @@ import picocli.CommandLine.Spec
ConfigureProjectCommand::class,
],
)
@Logged
class PluginConfigureCommand(
private val config: Config,
private val retConsole: RetConsole,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import io.quarkus.logging.Log
import io.rabobank.ret.RetConsole
import io.rabobank.ret.util.IntrospectionUtil
import io.rabobank.ret.util.Logged
import io.rabobank.ret.util.OsUtils
import picocli.CommandLine.Command
import picocli.CommandLine.Model.CommandSpec
Expand All @@ -23,6 +24,7 @@ import kotlin.io.path.createDirectories
name = "initialize",
hidden = true,
)
@Logged
class PluginInitializeCommand(
private val retConsole: RetConsole,
private val objectMapper: ObjectMapper,
Expand Down

0 comments on commit e6bb0f7

Please sign in to comment.