add creation of default server-config.yaml if not existing
This commit is contained in:
parent
c52a367dc8
commit
31c60931dd
4 changed files with 22 additions and 13 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -6,3 +6,4 @@
|
||||||
/.idea/
|
/.idea/
|
||||||
/My*
|
/My*
|
||||||
/my*
|
/my*
|
||||||
|
/server-config.yaml
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.charleskorn.kaml.YamlConfiguration
|
||||||
import kotlinx.serialization.InternalSerializationApi
|
import kotlinx.serialization.InternalSerializationApi
|
||||||
import kotlinx.serialization.serializer
|
import kotlinx.serialization.serializer
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
|
import java.io.File
|
||||||
import java.io.FileReader
|
import java.io.FileReader
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,6 +13,10 @@ fun readFromFile(fileName: String): String {
|
||||||
return BufferedReader(FileReader(fileName)).use { it.readText() }
|
return BufferedReader(FileReader(fileName)).use { it.readText() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun writeToFile(fileName: String, text: String) {
|
||||||
|
File(fileName).writeText(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@OptIn(InternalSerializationApi::class)
|
@OptIn(InternalSerializationApi::class)
|
||||||
inline fun <reified T : Any> String.yamlToType() = Yaml(configuration = YamlConfiguration(strictMode = false)).decodeFromString(
|
inline fun <reified T : Any> String.yamlToType() = Yaml(configuration = YamlConfiguration(strictMode = false)).decodeFromString(
|
||||||
|
@ -21,7 +26,7 @@ inline fun <reified T : Any> String.yamlToType() = Yaml(configuration = YamlConf
|
||||||
|
|
||||||
|
|
||||||
@OptIn(InternalSerializationApi::class)
|
@OptIn(InternalSerializationApi::class)
|
||||||
inline fun <reified T : Any> T.toYaml() = Yaml(configuration = YamlConfiguration(strictMode = false)).encodeToString(
|
inline fun <reified T : Any> T.toYaml() = Yaml(configuration = YamlConfiguration(strictMode = false, encodeDefaults = false)).encodeToString(
|
||||||
T::class.serializer(),
|
T::class.serializer(),
|
||||||
this
|
this
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,6 +2,8 @@ package org.domaindrivenarchitecture.provs.server.infrastructure.k3s
|
||||||
|
|
||||||
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.writeToFile
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.yamlToType
|
import org.domaindrivenarchitecture.provs.framework.core.yamlToType
|
||||||
import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig
|
import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig
|
||||||
import org.domaindrivenarchitecture.provs.server.domain.k3s.Node
|
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"
|
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
|
val filename = fileName?.fileName ?: DEFAULT_CONFIG_FILE
|
||||||
return if (File(filename).exists() || (filename != DEFAULT_CONFIG_FILE)) {
|
|
||||||
readFromFile(filename).yamlToType()
|
if ((filename.substringAfterLast("/") == DEFAULT_CONFIG_FILE) && !File(filename).exists()) {
|
||||||
} else {
|
writeK3sConfig(ConfigFileName(filename), K3sConfig("localhost", Node("127.0.0.1"), apple = true))
|
||||||
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()
|
||||||
|
}
|
|
@ -34,13 +34,7 @@ internal class YamlUtilsKtTest {
|
||||||
fqdn: "host"
|
fqdn: "host"
|
||||||
node:
|
node:
|
||||||
ipv4: "1.2.3.4"
|
ipv4: "1.2.3.4"
|
||||||
ipv6: null
|
""".trimIndent()
|
||||||
loopback:
|
|
||||||
ipv4: "192.168.5.1"
|
|
||||||
ipv6: "fc00::5:1"
|
|
||||||
certmanager: null
|
|
||||||
apple: null
|
|
||||||
reprovision: false""".trimIndent()
|
|
||||||
|
|
||||||
assertEquals(expected, yaml)
|
assertEquals(expected, yaml)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue