simplify configRepo code
This commit is contained in:
parent
45223d4669
commit
a6be9e36f7
3 changed files with 16 additions and 35 deletions
|
@ -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
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
internal fun writeConfig(config: DesktopConfig, fileName: String = "desktop-config.yaml") = FileWriter(fileName).use { it.write(config.toYaml()) }
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
return readFromFile(filename).yamlToType()
|
||||
// create a default config
|
||||
if ((filePath == DEFAULT_CONFIG_FILE) && !File(filePath).exists()) {
|
||||
writeK3sConfig(filePath, K3sConfig("localhost", Node("127.0.0.1"), echo = true))
|
||||
}
|
||||
|
||||
fun writeK3sConfig(fileName: ConfigFileName, config: K3sConfig) {
|
||||
writeToFile(fileName.fileName, config.toYaml())
|
||||
return readFromFile(filePath).yamlToType()
|
||||
}
|
||||
|
||||
|
||||
fun writeK3sConfig(filePath: String, config: K3sConfig) = writeToFile(filePath, config.toYaml())
|
||||
|
||||
|
|
|
@ -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<SpecConfig
|
|||
|
||||
|
||||
// --------------------------------- write ----------------------------------
|
||||
internal fun writeSpecConfigToFile(
|
||||
fileName: String = DEFAULT_CONFIG_FILE,
|
||||
config: SpecConfig
|
||||
) {
|
||||
FileWriter(fileName).use {
|
||||
it.write(
|
||||
Yaml.default.encodeToString(
|
||||
SpecConfig.serializer(),
|
||||
config
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
internal fun writeSpecConfigToFile(fileName: String = DEFAULT_CONFIG_FILE, config: SpecConfig) = FileWriter(fileName).use { it.write(config.toYaml()) }
|
Loading…
Reference in a new issue