mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
14.01.2021 8acbe1c3762eea533a15863f840ec452d9e561b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package org.micromata.borgbutler.config
 
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 {}
 
/**
 * Legacy functionality for older BorgButler installations.
 */
object Legacy  {
    /**
     * Backward compability
     */
    fun readOldJsonConfigFile(workingDir: File, jsonFilename: String): Configuration? {
        val jsonConfigFile = File(workingDir, jsonFilename)
        if (!jsonConfigFile.canRead()) {
            // Nothing to do
            return null
        }
        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))
        }
        return JsonUtils.fromJson(ConfigurationHandler.getConfigClazz(), json)
    }
}