From 8acbe1c3762eea533a15863f840ec452d9e561b9 Mon Sep 17 00:00:00 2001
From: Kai Reinhard <K.Reinhard@micromata.de>
Date: Wed, 14 Apr 2021 14:01:56 +0000
Subject: [PATCH] WIP, doc
---
borgbutler-core/src/main/kotlin/org/micromata/borgbutler/config/ConfigurationHandler.kt | 50 ++++++++++++++++++--------------------------------
1 files changed, 18 insertions(+), 32 deletions(-)
diff --git a/borgbutler-core/src/main/kotlin/org/micromata/borgbutler/config/ConfigurationHandler.kt b/borgbutler-core/src/main/kotlin/org/micromata/borgbutler/config/ConfigurationHandler.kt
index 24183fe..af3dbf4 100644
--- a/borgbutler-core/src/main/kotlin/org/micromata/borgbutler/config/ConfigurationHandler.kt
+++ b/borgbutler-core/src/main/kotlin/org/micromata/borgbutler/config/ConfigurationHandler.kt
@@ -2,13 +2,15 @@
import de.micromata.borgbutler.config.Definitions
import de.micromata.borgbutler.json.JsonUtils
+import mu.KotlinLogging
import org.apache.commons.io.FileUtils
-import org.slf4j.LoggerFactory
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*
+private val log = KotlinLogging.logger {}
+
/**
* Reads and writes config file borgbutler-config.json/borgbutler-config.yaml
*/
@@ -23,13 +25,22 @@
private fun read() {
if (configFile.canRead()) {
log.info("Reading config file '" + configFile.absolutePath + "'")
+ val jsonConfigFile = File(workingDir, CONFIG_FILENAME)
+ if (jsonConfigFile.canRead()) {
+ val yaml = FileUtils.readFileToString(jsonConfigFile, Definitions.STD_CHARSET)
+ configuration = YamlUtils.fromYaml(configClazz, yaml)
+ }
} else {
- readOldJson()
+ Legacy.readOldJsonConfigFile(workingDir, OLD_JSON_CONFIG_FILENAME)?.let {
+ configuration = it
+ save()
+ }
}
try {
if (configuration == null) {
try {
configuration = configClazz.getDeclaredConstructor().newInstance()
+ save()
} catch (ex: Exception) {
log.error(
"Internal error: Can't instantiate object of type '" + configClazz + "': " + ex.message,
@@ -48,35 +59,6 @@
}
}
- /**
- * Backward compability
- */
- private fun readOldJson() {
- val jsonConfigFile = File(workingDir, OLD_JSON_CONFIG_FILENAME)
- if (!jsonConfigFile.canRead()) {
- // Nothing to do
- return
- }
- var json: String? = null
- if (jsonConfigFile.exists()) {
- json = FileUtils.readFileToString(jsonConfigFile, Definitions.STD_CHARSET)
- // Migrate from first version:
- if (json.contains("repo-configs")) {
- json = json.replace("repo-configs", "repoConfigs")
- json = json.replace("display_name", "displayName")
- }
- val formatter = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss")
- val backupFilename = "${formatter.format(Date())}-old-${jsonConfigFile.name}"
- log.info("Migrating old json config file to yaml file. Renaming old json file '${jsonConfigFile.absolutePath}' to '$backupFilename'.")
- FileUtils.moveFile(jsonConfigFile, File(jsonConfigFile.parent, backupFilename))
- }
- val newConfig = JsonUtils.fromJson(configClazz, json)
- ?: // Nothing to do
- return
- configuration = newConfig
- save()
- }
-
fun save() {
configuration?.getRepoConfigs()?.filter { !it.passphrase.isNullOrBlank() }?.forEach { repoConfig ->
log.info("Removing password command from config because password command is given: " + repoConfig.passwordCommand)
@@ -103,7 +85,6 @@
}
companion object {
- private val log = LoggerFactory.getLogger(ConfigurationHandler::class.java)
private var instance: ConfigurationHandler? = null
private const val BUTLER_HOME_DIR = ".borgbutler"
private const val OLD_JSON_CONFIG_FILENAME = "borgbutler-config.json"
@@ -134,6 +115,11 @@
fun setConfigClazz(configClazz: Class<out Configuration>) {
Companion.configClazz = configClazz
}
+
+ @kotlin.jvm.JvmStatic
+ fun getConfigClazz(): Class<out Configuration> {
+ return configClazz
+ }
}
init {
--
Gitblit v1.10.0