diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/domain/SyspecService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/domain/SyspecService.kt index dd9ef83..f394dae 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/domain/SyspecService.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/domain/SyspecService.kt @@ -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) } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt index 9168544..715f1bf 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt @@ -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 = 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 = 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 - } -} diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepoKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepoKtTest.kt index d4f6755..097df7a 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepoKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepoKtTest.kt @@ -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()) } } \ No newline at end of file