simplify SyspecConfigRepo.kt

This commit is contained in:
ansgarz 2022-03-26 18:17:04 +01:00
parent a475290312
commit 8bb574ece7
2 changed files with 8 additions and 32 deletions

View file

@ -12,27 +12,23 @@ import java.io.FileWriter
private const val DEFAULT_CONFIG_FILE = "syspec-config.yaml"
// --------------------------------- read ----------------------------------
internal fun getSpecConfigFromFile(file: ConfigFileName? = null): SpecConfig {
val filename = file?.fileName ?: DEFAULT_CONFIG_FILE
if ((filename.substringAfterLast("/") == DEFAULT_CONFIG_FILE) && !File(filename).exists()) {
internal fun findSpecConfigFromFile(file: ConfigFileName? = null): Result<SpecConfig> = runCatching {
val filePath = file?.fileName ?: DEFAULT_CONFIG_FILE
if ((filePath == DEFAULT_CONFIG_FILE) && !File(filePath).exists()) {
// provide default config
writeSpecConfigToFile(filename, SpecConfig(listOf(CommandSpec("echo just_for_demo", "just_for_demo"))))
writeSpecConfigToFile(filePath, SpecConfig(listOf(CommandSpec("echo just_for_demo", "just_for_demo"))))
}
return readFromFile(filename).yamlToType()
readFromFile(filePath).yamlToType()
}
internal fun findSpecConfigFromFile(file: ConfigFileName? = null): Result<SpecConfig> = runCatching { getSpecConfigFromFile(file) }
internal fun getSpecConfigFromResource(resourcePath: String): SpecConfig {
internal fun findSpecConfigFromResource(resourcePath: String): Result<SpecConfig> = runCatching {
val resource = Thread.currentThread().contextClassLoader.getResource(resourcePath)
requireNotNull(resource) { "Resource $resourcePath not found" }
return resource.readText().yamlToType()
}
internal fun findSpecConfigFromResource(resourcePath: String): Result<SpecConfig> = runCatching { getSpecConfigFromResource(resourcePath) }
// --------------------------------- write ----------------------------------
internal fun writeSpecConfigToFile(fileName: String = DEFAULT_CONFIG_FILE, config: SpecConfig) = FileWriter(fileName).use { it.write(config.toYaml()) }
internal fun writeSpecConfigToFile(fileName: String = DEFAULT_CONFIG_FILE, config: SpecConfig) =
FileWriter(fileName).use { it.write(config.toYaml()) }

View file

@ -7,26 +7,6 @@ import org.junit.jupiter.api.Test
internal class SyspecConfigRepoKtTest {
@Test
fun getSpecConfigFromFile_success() {
// when
@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") // null would reveal test error
val filePath = javaClass.classLoader.getResource("syspec-config.yaml").file
val res = getSpecConfigFromFile(ConfigFileName(filePath))
// then
assertEquals(listOf(CommandSpec("echo just_for_test", "just_for_test")), res.command)
}
@Test
fun getSpecConfigFromResource_success() {
// when
val res = getSpecConfigFromResource("syspec-config.yaml")
// then
assertEquals(listOf(CommandSpec("echo just_for_test", "just_for_test")), res.command)
}
@Test
fun findSpecConfigFromFile_success() {
// when