From a6be9e36f7144018d1d7ebe42f55677bbcdc5709 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Sat, 26 Mar 2022 13:57:23 +0100 Subject: [PATCH] simplify configRepo code --- .../desktop/infrastructure/ConfigRepository.kt | 17 +++-------------- .../infrastructure/K3sConfigRepository.kt | 18 +++++++++++------- .../syspec/infrastructure/SyspecConfigRepo.kt | 16 ++-------------- 3 files changed, 16 insertions(+), 35 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepository.kt index 43264ad..1be7b57 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepository.kt @@ -1,8 +1,8 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure -import com.charleskorn.kaml.Yaml import org.domaindrivenarchitecture.provs.desktop.domain.DesktopConfig import org.domaindrivenarchitecture.provs.framework.core.readFromFile +import org.domaindrivenarchitecture.provs.framework.core.toYaml import org.domaindrivenarchitecture.provs.framework.core.yamlToType import java.io.FileWriter @@ -11,19 +11,8 @@ import java.io.FileWriter * Returns WorkplaceConfig; data for config is read from specified file. * Throws exceptions FileNotFoundException, SerializationException if file is not found resp. cannot be parsed. */ -internal fun getConfig(filename: String = "desktop-config.yaml"): DesktopConfig { - return readFromFile(filename).yamlToType() -} +internal fun getConfig(filename: String = "desktop-config.yaml"): DesktopConfig = readFromFile(filename).yamlToType() @Suppress("unused") -internal fun writeConfig(config: DesktopConfig, fileName: String = "desktop-config.yaml") { - FileWriter(fileName).use { - it.write( - Yaml.default.encodeToString( - DesktopConfig.serializer(), - config - ) - ) - } -} \ No newline at end of file +internal fun writeConfig(config: DesktopConfig, fileName: String = "desktop-config.yaml") = FileWriter(fileName).use { it.write(config.toYaml()) } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3sConfigRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3sConfigRepository.kt index 5c8a5b6..77a65ca 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3sConfigRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/K3sConfigRepository.kt @@ -9,17 +9,21 @@ import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig import org.domaindrivenarchitecture.provs.server.domain.k3s.Node import java.io.File + private const val DEFAULT_CONFIG_FILE = "server-config.yaml" + 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()) { - writeK3sConfig(ConfigFileName(filename), K3sConfig("localhost", Node("127.0.0.1"), echo = true)) + // create a default config + 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()) + diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt index 715f1bf..a68b3cb 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt @@ -1,8 +1,8 @@ package org.domaindrivenarchitecture.provs.syspec.infrastructure -import com.charleskorn.kaml.Yaml import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName 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.syspec.domain.CommandSpec import org.domaindrivenarchitecture.provs.syspec.domain.SpecConfig @@ -35,16 +35,4 @@ internal fun findSpecConfigFromResource(resourcePath: String): Result