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 a68b3cb..90e152e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepo.kt @@ -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 = 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 = runCatching { getSpecConfigFromFile(file) } - -internal fun getSpecConfigFromResource(resourcePath: String): SpecConfig { +internal fun findSpecConfigFromResource(resourcePath: String): Result = runCatching { 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) = FileWriter(fileName).use { it.write(config.toYaml()) } \ No newline at end of file +internal fun writeSpecConfigToFile(fileName: String = DEFAULT_CONFIG_FILE, config: SpecConfig) = + FileWriter(fileName).use { it.write(config.toYaml()) } \ No newline at end of file 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 097df7a..757f112 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepoKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/syspec/infrastructure/SyspecConfigRepoKtTest.kt @@ -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