diff --git a/.gitignore b/.gitignore index 0af9901..1e0c0f1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /.idea/ /My* /my* +/server-config.yaml diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/YamlUtils.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/YamlUtils.kt index 9f83cc7..ccdf605 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/YamlUtils.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/YamlUtils.kt @@ -5,6 +5,7 @@ import com.charleskorn.kaml.YamlConfiguration import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.serializer import java.io.BufferedReader +import java.io.File import java.io.FileReader @@ -12,6 +13,10 @@ fun readFromFile(fileName: String): String { return BufferedReader(FileReader(fileName)).use { it.readText() } } +fun writeToFile(fileName: String, text: String) { + File(fileName).writeText(text) +} + @OptIn(InternalSerializationApi::class) inline fun String.yamlToType() = Yaml(configuration = YamlConfiguration(strictMode = false)).decodeFromString( @@ -21,7 +26,7 @@ inline fun String.yamlToType() = Yaml(configuration = YamlConf @OptIn(InternalSerializationApi::class) -inline fun T.toYaml() = Yaml(configuration = YamlConfiguration(strictMode = false)).encodeToString( +inline fun T.toYaml() = Yaml(configuration = YamlConfiguration(strictMode = false, encodeDefaults = false)).encodeToString( T::class.serializer(), this ) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/k3s/ConfigRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/k3s/ConfigRepository.kt index 1382987..f75d684 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/k3s/ConfigRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/k3s/ConfigRepository.kt @@ -2,6 +2,8 @@ package org.domaindrivenarchitecture.provs.server.infrastructure.k3s 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.writeToFile import org.domaindrivenarchitecture.provs.framework.core.yamlToType import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig import org.domaindrivenarchitecture.provs.server.domain.k3s.Node @@ -9,11 +11,18 @@ import java.io.File private const val DEFAULT_CONFIG_FILE = "server-config.yaml" -fun getK3sConfig(fileName: ConfigFileName?): K3sConfig { +fun getK3sConfig(fileName: ConfigFileName? = null): K3sConfig { val filename = fileName?.fileName ?: DEFAULT_CONFIG_FILE - return if (File(filename).exists() || (filename != DEFAULT_CONFIG_FILE)) { - readFromFile(filename).yamlToType() - } else { - K3sConfig("localhost", Node("127.0.0.1"), apple = true) + + if ((filename.substringAfterLast("/") == DEFAULT_CONFIG_FILE) && !File(filename).exists()) { + writeK3sConfig(ConfigFileName(filename), K3sConfig("localhost", Node("127.0.0.1"), apple = true)) } + return readFromFile(filename).yamlToType() } + +fun writeK3sConfig(fileName: ConfigFileName, config: K3sConfig) { + writeToFile(fileName.fileName, config.toYaml()) +} +fun main() { + getK3sConfig() +} \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/YamlUtilsKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/YamlUtilsKtTest.kt index 9e3d5a7..9c99c20 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/YamlUtilsKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/YamlUtilsKtTest.kt @@ -34,13 +34,7 @@ internal class YamlUtilsKtTest { fqdn: "host" node: ipv4: "1.2.3.4" - ipv6: null - loopback: - ipv4: "192.168.5.1" - ipv6: "fc00::5:1" - certmanager: null - apple: null - reprovision: false""".trimIndent() + """.trimIndent() assertEquals(expected, yaml) }