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

merge-requests/1/merge
ansgarz 2 years ago
parent ade20a22e0
commit 45223d4669

@ -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 {
verifySpecConfig(spec)
}
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 {
verifySpecConfig(spec)
}
val result = findSpecConfigFromResource(resourceName)
val spec = result.getOrElse { return@task ProvResult(false, "Could not read resource: $resourceName due to: ${result.exceptionOrNull()?.message}") }
verifySpecConfig(spec)
}

@ -8,24 +8,10 @@ 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
)
)
}
}
// --------------------------------- read ----------------------------------
internal fun getSpecConfigFromFile(file: ConfigFileName? = null): SpecConfig {
val filename = file?.fileName ?: DEFAULT_CONFIG_FILE
@ -36,15 +22,8 @@ internal fun getSpecConfigFromFile(file: ConfigFileName? = null): SpecConfig {
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 findSpecConfigFromFile(file: ConfigFileName? = null): Result<SpecConfig> = runCatching { getSpecConfigFromFile(file) }
internal fun getSpecConfigFromResource(resourcePath: String): SpecConfig {
val resource = Thread.currentThread().contextClassLoader.getResource(resourcePath)
@ -52,12 +31,20 @@ internal fun getSpecConfigFromResource(resourcePath: String): SpecConfig {
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
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(
Yaml.default.encodeToString(
SpecConfig.serializer(),
config
)
)
}
}

@ -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())
}
}
Loading…
Cancel
Save