simplify configRepo code

This commit is contained in:
ansgarz 2022-03-26 13:57:23 +01:00
parent 45223d4669
commit a6be9e36f7
3 changed files with 16 additions and 35 deletions

View file

@ -1,8 +1,8 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure package org.domaindrivenarchitecture.provs.desktop.infrastructure
import com.charleskorn.kaml.Yaml
import org.domaindrivenarchitecture.provs.desktop.domain.DesktopConfig import org.domaindrivenarchitecture.provs.desktop.domain.DesktopConfig
import org.domaindrivenarchitecture.provs.framework.core.readFromFile import org.domaindrivenarchitecture.provs.framework.core.readFromFile
import org.domaindrivenarchitecture.provs.framework.core.toYaml
import org.domaindrivenarchitecture.provs.framework.core.yamlToType import org.domaindrivenarchitecture.provs.framework.core.yamlToType
import java.io.FileWriter import java.io.FileWriter
@ -11,19 +11,8 @@ import java.io.FileWriter
* Returns WorkplaceConfig; data for config is read from specified file. * Returns WorkplaceConfig; data for config is read from specified file.
* Throws exceptions FileNotFoundException, SerializationException if file is not found resp. cannot be parsed. * Throws exceptions FileNotFoundException, SerializationException if file is not found resp. cannot be parsed.
*/ */
internal fun getConfig(filename: String = "desktop-config.yaml"): DesktopConfig { internal fun getConfig(filename: String = "desktop-config.yaml"): DesktopConfig = readFromFile(filename).yamlToType()
return readFromFile(filename).yamlToType()
}
@Suppress("unused") @Suppress("unused")
internal fun writeConfig(config: DesktopConfig, fileName: String = "desktop-config.yaml") { internal fun writeConfig(config: DesktopConfig, fileName: String = "desktop-config.yaml") = FileWriter(fileName).use { it.write(config.toYaml()) }
FileWriter(fileName).use {
it.write(
Yaml.default.encodeToString(
DesktopConfig.serializer(),
config
)
)
}
}

View file

@ -9,17 +9,21 @@ import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig
import org.domaindrivenarchitecture.provs.server.domain.k3s.Node import org.domaindrivenarchitecture.provs.server.domain.k3s.Node
import java.io.File import java.io.File
private const val DEFAULT_CONFIG_FILE = "server-config.yaml" private const val DEFAULT_CONFIG_FILE = "server-config.yaml"
fun getK3sConfig(fileName: ConfigFileName? = null): K3sConfig { fun getK3sConfig(fileName: ConfigFileName? = null): K3sConfig {
val filename = fileName?.fileName ?: DEFAULT_CONFIG_FILE val filePath = fileName?.fileName ?: DEFAULT_CONFIG_FILE
if ((filename.substringAfterLast("/") == DEFAULT_CONFIG_FILE) && !File(filename).exists()) { // create a default config
writeK3sConfig(ConfigFileName(filename), K3sConfig("localhost", Node("127.0.0.1"), echo = true)) if ((filePath == DEFAULT_CONFIG_FILE) && !File(filePath).exists()) {
writeK3sConfig(filePath, K3sConfig("localhost", Node("127.0.0.1"), echo = true))
} }
return readFromFile(filename).yamlToType()
return readFromFile(filePath).yamlToType()
} }
fun writeK3sConfig(fileName: ConfigFileName, config: K3sConfig) {
writeToFile(fileName.fileName, config.toYaml()) fun writeK3sConfig(filePath: String, config: K3sConfig) = writeToFile(filePath, config.toYaml())
}

View file

@ -1,8 +1,8 @@
package org.domaindrivenarchitecture.provs.syspec.infrastructure package org.domaindrivenarchitecture.provs.syspec.infrastructure
import com.charleskorn.kaml.Yaml
import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.framework.core.readFromFile import org.domaindrivenarchitecture.provs.framework.core.readFromFile
import org.domaindrivenarchitecture.provs.framework.core.toYaml
import org.domaindrivenarchitecture.provs.framework.core.yamlToType import org.domaindrivenarchitecture.provs.framework.core.yamlToType
import org.domaindrivenarchitecture.provs.syspec.domain.CommandSpec import org.domaindrivenarchitecture.provs.syspec.domain.CommandSpec
import org.domaindrivenarchitecture.provs.syspec.domain.SpecConfig import org.domaindrivenarchitecture.provs.syspec.domain.SpecConfig
@ -35,16 +35,4 @@ internal fun findSpecConfigFromResource(resourcePath: String): Result<SpecConfig
// --------------------------------- write ---------------------------------- // --------------------------------- write ----------------------------------
internal fun writeSpecConfigToFile( internal fun writeSpecConfigToFile(fileName: String = DEFAULT_CONFIG_FILE, config: SpecConfig) = FileWriter(fileName).use { it.write(config.toYaml()) }
fileName: String = DEFAULT_CONFIG_FILE,
config: SpecConfig
) {
FileWriter(fileName).use {
it.write(
Yaml.default.encodeToString(
SpecConfig.serializer(),
config
)
)
}
}