diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileName.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileName.kt index 0ca0cee..7c3de6e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileName.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileName.kt @@ -1,6 +1,11 @@ package org.domaindrivenarchitecture.provs.configuration.domain +import java.io.File + class ConfigFileName(fileName: String) { val fileName = fileName.trim() + fun fullqualified() : String { + return File(fileName).absoluteFile.absolutePath + } } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileRepository.kt new file mode 100644 index 0000000..ad9eedf --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/domain/ConfigFileRepository.kt @@ -0,0 +1,5 @@ +package org.domaindrivenarchitecture.provs.configuration.domain + +interface ConfigFileRepository { + fun assertExists(configFileName: ConfigFileName?) +} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt new file mode 100644 index 0000000..7ce2000 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepository.kt @@ -0,0 +1,13 @@ +package org.domaindrivenarchitecture.provs.configuration.infrastructure + +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkLocalFile +import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName +import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileRepository + +class DefaultConfigFileRepository : ConfigFileRepository { + override fun assertExists(configFileName: ConfigFileName?) { + if (configFileName != null && !checkLocalFile(configFileName.fullqualified())) { + throw RuntimeException("Config file ${configFileName.fileName} not found. Please check if path is correct.") + } + } +} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt index 96a0f86..45aa883 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sService.kt @@ -1,5 +1,6 @@ package org.domaindrivenarchitecture.provs.server.domain.k3s +import org.domaindrivenarchitecture.provs.configuration.infrastructure.DefaultConfigFileRepository import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.server.domain.k8s_grafana_agent.GrafanaAgentConfigResolved import org.domaindrivenarchitecture.provs.server.domain.k8s_grafana_agent.provisionGrafanaAgent @@ -14,6 +15,7 @@ fun Prov.provisionK3sCommand(cli: K3sCliCommand) = task { if (cli.onlyModules == null ) { val k3sConfig: K3sConfig = getK3sConfig(cli.configFileName) DefaultApplicationFileRepository().assertExists(cli.applicationFileName) + DefaultConfigFileRepository().assertExists(cli.configFileName) val k3sConfigReprovision = k3sConfig.copy(reprovision = cli.reprovision || k3sConfig.reprovision) provisionK3s(k3sConfigReprovision, grafanaConfigResolved, cli.applicationFileName) diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepositoryKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepositoryKtTest.kt new file mode 100644 index 0000000..6319fdb --- /dev/null +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/configuration/infrastructure/DefaultConfigFileRepositoryKtTest.kt @@ -0,0 +1,41 @@ +package org.domaindrivenarchitecture.provs.configuration.infrastructure + +import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileName +import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileRepository +import org.domaindrivenarchitecture.provs.server.infrastructure.DefaultApplicationFileRepository +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.api.Assertions.assertEquals +import java.io.File + +internal class DefaultConfigFileRepositoryKtTest { + @Test + fun assertExistsThrowsRuntimeException() { + // when + val invalidFileName = ApplicationFileName("iDontExist") + val repo: ApplicationFileRepository = DefaultApplicationFileRepository() + + // then + val exception = assertThrows( + "Should not find the file." + ) { repo.assertExists(invalidFileName) } + + assertEquals( + "Application file iDontExist not found. Please check if path is correct.", + exception.message) + } + + @Test + fun assertExistsPasses() { + // given + val validFileName = "src/test/resources/existing_file" + + // when + val validFile = ApplicationFileName(File(validFileName).path) + val repo: ApplicationFileRepository = DefaultApplicationFileRepository() + repo.assertExists(validFile) + + // then + // no exception is thrown + } +} diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepositoryKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepositoryKtTest.kt index 86c1123..eb57bd6 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepositoryKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/DefaultApplicationFileRepositoryKtTest.kt @@ -1,10 +1,10 @@ package org.domaindrivenarchitecture.provs.server.infrastructure -import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileName -import org.domaindrivenarchitecture.provs.server.domain.k3s.ApplicationFileRepository +import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName +import org.domaindrivenarchitecture.provs.configuration.infrastructure.DefaultConfigFileRepository +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -import org.junit.jupiter.api.Assertions.assertEquals import java.io.File internal class DefaultApplicationFileRepositoryKtTest { @@ -12,8 +12,8 @@ internal class DefaultApplicationFileRepositoryKtTest { @Test fun assertExistsThrowsRuntimeException() { // when - val invalidFileName = ApplicationFileName("iDontExist") - val repo: ApplicationFileRepository = DefaultApplicationFileRepository() + val invalidFileName = ConfigFileName("iDontExist") + val repo = DefaultConfigFileRepository() // then val exception = assertThrows( @@ -21,7 +21,7 @@ internal class DefaultApplicationFileRepositoryKtTest { ) { repo.assertExists(invalidFileName) } assertEquals( - "Application file iDontExist not found. Please check if path is correct.", + "Config file iDontExist not found. Please check if path is correct.", exception.message) } @@ -31,8 +31,8 @@ internal class DefaultApplicationFileRepositoryKtTest { val validFileName = "src/test/resources/existing_file" // when - val validFile = ApplicationFileName(File(validFileName).path) - val repo: ApplicationFileRepository = DefaultApplicationFileRepository() + val validFile = ConfigFileName(File(validFileName).path) + val repo = DefaultConfigFileRepository() repo.assertExists(validFile) // then