[skip ci] refactor SyspecConfigRepo.kt using kotlin.Result

This commit is contained in:
ansgarz 2022-03-26 13:14:34 +01:00
parent ade20a22e0
commit 45223d4669
3 changed files with 34 additions and 55 deletions

View file

@ -9,24 +9,16 @@ import org.domaindrivenarchitecture.provs.syspec.infrastructure.verifySpecConfig
fun Prov.verifySpec(configFile: ConfigFileName? = null) = task {
val spec = findSpecConfigFromFile(configFile)
if (spec == null) {
ProvResult(false, "Could not read file: ${configFile?.fileName}")
} else {
val result = findSpecConfigFromFile(configFile)
val spec = result.getOrElse { return@task ProvResult(false, "Could not read file: ${configFile?.fileName} due to: ${result.exceptionOrNull()?.message}") }
verifySpecConfig(spec)
}
}
@Suppress("unused") // Api
fun Prov.verifySpecFromResource(resourceName: String) = task {
val spec = findSpecConfigFromResource(resourceName)
if (spec == null) {
ProvResult(false, "Could not read resource: ${resourceName}")
} else {
val result = findSpecConfigFromResource(resourceName)
val spec = result.getOrElse { return@task ProvResult(false, "Could not read resource: $resourceName due to: ${result.exceptionOrNull()?.message}") }
verifySpecConfig(spec)
}
}

View file

@ -8,10 +8,33 @@ 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"
// --------------------------------- read ----------------------------------
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): Result<SpecConfig> = runCatching { getSpecConfigFromFile(file) }
internal fun getSpecConfigFromResource(resourcePath: String): SpecConfig {
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
@ -25,39 +48,3 @@ internal fun writeSpecConfigToFile(
)
}
}
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
}
}
internal fun getSpecConfigFromResource(resourcePath: String): SpecConfig {
val resource = Thread.currentThread().contextClassLoader.getResource(resourcePath)
requireNotNull(resource) { "Resource $resourcePath not found" }
return resource.readText().yamlToType()
}
internal fun findSpecConfigFromResource(resourcePath: String): SpecConfig? {
return try {
val config = getSpecConfigFromResource(resourcePath)
config
} catch (e: IllegalArgumentException) {
println("Error: " + e.message)
null
}
}

View file

@ -35,7 +35,7 @@ internal class SyspecConfigRepoKtTest {
val res = findSpecConfigFromFile(ConfigFileName(filePath))
// then
assertEquals(listOf(CommandSpec("echo just_for_test", "just_for_test")), res?.command)
assertEquals(listOf(CommandSpec("echo just_for_test", "just_for_test")), res.getOrNull()?.command)
}
@Test
@ -44,7 +44,7 @@ internal class SyspecConfigRepoKtTest {
val res =findSpecConfigFromResource("syspec-config.yaml")
// then
assertEquals(listOf(CommandSpec("echo just_for_test", "just_for_test")), res?.command)
assertEquals(listOf(CommandSpec("echo just_for_test", "just_for_test")), res.getOrNull()?.command)
}
@Test
@ -53,7 +53,7 @@ internal class SyspecConfigRepoKtTest {
val res = findSpecConfigFromFile(ConfigFileName("dontexist"))
// then
assertNull(res)
assertNull(res.getOrNull())
}
@Test
@ -62,6 +62,6 @@ internal class SyspecConfigRepoKtTest {
val res = findSpecConfigFromResource("dontexist")
// then
assertNull(res)
assertNull(res.getOrNull())
}
}