You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
provs/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt

48 lines
1.5 KiB
Kotlin

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.yamlToType
import org.domaindrivenarchitecture.provs.syspec.domain.CommandSpec
import org.domaindrivenarchitecture.provs.syspec.domain.SpecConfig
import java.io.File
import java.io.FileWriter
import java.io.IOException
private const val DEFAULT_CONFIG_FILE = "syspec-config.yaml"
internal fun writeSpecConfigToFile(
fileName: String = DEFAULT_CONFIG_FILE,
config: SpecConfig
) {
FileWriter(fileName).use {
it.write(
Yaml.default.encodeToString(
SpecConfig.serializer(),
config
)
)
}
}
internal fun getSpecConfigFromFile(file: ConfigFileName? = null): SpecConfig {
val filename = file?.fileName ?: DEFAULT_CONFIG_FILE
if ((filename.substringAfterLast("/") == DEFAULT_CONFIG_FILE) && !File(filename).exists()) {
// provide default config
writeSpecConfigToFile(filename, SpecConfig(listOf(CommandSpec("echo just_for_demo", "just_for_demo"))))
}
return readFromFile(filename).yamlToType()
}
internal fun findSpecConfigFromFile(file: ConfigFileName? = null): SpecConfig? {
return try {
val config = getSpecConfigFromFile(file)
config
} catch (e: IOException) {
println("Error: " + e.message)
null
}
}