[skip ci] refactor SyspecConfigRepo.kt using kotlin.Result
This commit is contained in:
parent
ade20a22e0
commit
45223d4669
3 changed files with 34 additions and 55 deletions
|
@ -9,24 +9,16 @@ import org.domaindrivenarchitecture.provs.syspec.infrastructure.verifySpecConfig
|
||||||
|
|
||||||
|
|
||||||
fun Prov.verifySpec(configFile: ConfigFileName? = null) = task {
|
fun Prov.verifySpec(configFile: ConfigFileName? = null) = task {
|
||||||
val spec = findSpecConfigFromFile(configFile)
|
val result = findSpecConfigFromFile(configFile)
|
||||||
|
val spec = result.getOrElse { return@task ProvResult(false, "Could not read file: ${configFile?.fileName} due to: ${result.exceptionOrNull()?.message}") }
|
||||||
if (spec == null) {
|
|
||||||
ProvResult(false, "Could not read file: ${configFile?.fileName}")
|
|
||||||
} else {
|
|
||||||
verifySpecConfig(spec)
|
verifySpecConfig(spec)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Suppress("unused") // Api
|
@Suppress("unused") // Api
|
||||||
fun Prov.verifySpecFromResource(resourceName: String) = task {
|
fun Prov.verifySpecFromResource(resourceName: String) = task {
|
||||||
val spec = findSpecConfigFromResource(resourceName)
|
val result = findSpecConfigFromResource(resourceName)
|
||||||
|
val spec = result.getOrElse { return@task ProvResult(false, "Could not read resource: $resourceName due to: ${result.exceptionOrNull()?.message}") }
|
||||||
if (spec == null) {
|
|
||||||
ProvResult(false, "Could not read resource: ${resourceName}")
|
|
||||||
} else {
|
|
||||||
verifySpecConfig(spec)
|
verifySpecConfig(spec)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -8,10 +8,33 @@ import org.domaindrivenarchitecture.provs.syspec.domain.CommandSpec
|
||||||
import org.domaindrivenarchitecture.provs.syspec.domain.SpecConfig
|
import org.domaindrivenarchitecture.provs.syspec.domain.SpecConfig
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileWriter
|
import java.io.FileWriter
|
||||||
import java.io.IOException
|
|
||||||
|
|
||||||
private const val DEFAULT_CONFIG_FILE = "syspec-config.yaml"
|
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(
|
internal fun writeSpecConfigToFile(
|
||||||
fileName: String = DEFAULT_CONFIG_FILE,
|
fileName: String = DEFAULT_CONFIG_FILE,
|
||||||
config: SpecConfig
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ internal class SyspecConfigRepoKtTest {
|
||||||
val res = findSpecConfigFromFile(ConfigFileName(filePath))
|
val res = findSpecConfigFromFile(ConfigFileName(filePath))
|
||||||
|
|
||||||
// then
|
// 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
|
@Test
|
||||||
|
@ -44,7 +44,7 @@ internal class SyspecConfigRepoKtTest {
|
||||||
val res =findSpecConfigFromResource("syspec-config.yaml")
|
val res =findSpecConfigFromResource("syspec-config.yaml")
|
||||||
|
|
||||||
// then
|
// 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
|
@Test
|
||||||
|
@ -53,7 +53,7 @@ internal class SyspecConfigRepoKtTest {
|
||||||
val res = findSpecConfigFromFile(ConfigFileName("dontexist"))
|
val res = findSpecConfigFromFile(ConfigFileName("dontexist"))
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertNull(res)
|
assertNull(res.getOrNull())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -62,6 +62,6 @@ internal class SyspecConfigRepoKtTest {
|
||||||
val res = findSpecConfigFromResource("dontexist")
|
val res = findSpecConfigFromResource("dontexist")
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertNull(res)
|
assertNull(res.getOrNull())
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue