simplify SyspecConfigRepo.kt
This commit is contained in:
parent
a475290312
commit
8bb574ece7
2 changed files with 8 additions and 32 deletions
|
@ -12,27 +12,23 @@ import java.io.FileWriter
|
||||||
private const val DEFAULT_CONFIG_FILE = "syspec-config.yaml"
|
private const val DEFAULT_CONFIG_FILE = "syspec-config.yaml"
|
||||||
|
|
||||||
// --------------------------------- read ----------------------------------
|
// --------------------------------- read ----------------------------------
|
||||||
internal fun getSpecConfigFromFile(file: ConfigFileName? = null): SpecConfig {
|
internal fun findSpecConfigFromFile(file: ConfigFileName? = null): Result<SpecConfig> = runCatching {
|
||||||
val filename = file?.fileName ?: DEFAULT_CONFIG_FILE
|
val filePath = file?.fileName ?: DEFAULT_CONFIG_FILE
|
||||||
|
if ((filePath == DEFAULT_CONFIG_FILE) && !File(filePath).exists()) {
|
||||||
if ((filename.substringAfterLast("/") == DEFAULT_CONFIG_FILE) && !File(filename).exists()) {
|
|
||||||
// provide default config
|
// 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<SpecConfig> = runCatching { getSpecConfigFromFile(file) }
|
|
||||||
|
|
||||||
|
internal fun findSpecConfigFromResource(resourcePath: String): Result<SpecConfig> = runCatching {
|
||||||
internal fun getSpecConfigFromResource(resourcePath: String): SpecConfig {
|
|
||||||
val resource = Thread.currentThread().contextClassLoader.getResource(resourcePath)
|
val resource = Thread.currentThread().contextClassLoader.getResource(resourcePath)
|
||||||
requireNotNull(resource) { "Resource $resourcePath not found" }
|
requireNotNull(resource) { "Resource $resourcePath not found" }
|
||||||
return resource.readText().yamlToType()
|
return resource.readText().yamlToType()
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun findSpecConfigFromResource(resourcePath: String): Result<SpecConfig> = runCatching { getSpecConfigFromResource(resourcePath) }
|
|
||||||
|
|
||||||
|
|
||||||
// --------------------------------- write ----------------------------------
|
// --------------------------------- write ----------------------------------
|
||||||
internal fun writeSpecConfigToFile(fileName: String = DEFAULT_CONFIG_FILE, config: SpecConfig) = FileWriter(fileName).use { it.write(config.toYaml()) }
|
internal fun writeSpecConfigToFile(fileName: String = DEFAULT_CONFIG_FILE, config: SpecConfig) =
|
||||||
|
FileWriter(fileName).use { it.write(config.toYaml()) }
|
|
@ -7,26 +7,6 @@ import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
internal class SyspecConfigRepoKtTest {
|
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
|
@Test
|
||||||
fun findSpecConfigFromFile_success() {
|
fun findSpecConfigFromFile_success() {
|
||||||
// when
|
// when
|
||||||
|
|
Loading…
Reference in a new issue