diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/Application.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/Application.kt index a44de64..467307e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/Application.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/Application.kt @@ -1,9 +1,11 @@ package org.domaindrivenarchitecture.provs.server.application import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance +import org.domaindrivenarchitecture.provs.server.domain.ServerCliCommand import org.domaindrivenarchitecture.provs.server.domain.ServerType import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sCliCommand import org.domaindrivenarchitecture.provs.server.domain.k3s.provisionK3s +import org.domaindrivenarchitecture.provs.server.infrastructure.genericFileExistenceCheck import kotlin.system.exitProcess @@ -18,13 +20,26 @@ fun main(args: Array) { val checkedArgs = if (args.isEmpty()) arrayOf("-h") else args val cmd = CliArgumentsParser("provs-server.jar subcommand target").parseCommand(checkedArgs) - if (!cmd.isValid()) { - println("Arguments are not valid, pls try -h for help.") + + // input validation + if (!cmd.isValidTarget()) { + println("Remote or localhost not valid, please try -h for help.") + exitProcess(1) + } + if (!cmd.isValidConfigFileName()) { + println("Config file not found. Please check if path is correct.") exitProcess(1) } + if (!(cmd as K3sCliCommand).isValidApplicationFileName()) { + println("Application file not found. Please check if path is correct.") + exitProcess(1) + } + val prov = createProvInstance(cmd.target) - when(cmd.serverType) { - ServerType.K3S -> prov.provisionK3s(cmd as K3sCliCommand) - else -> { throw RuntimeException("Unknown serverType") } + + if (!cmd.isValidServerType()) { + throw RuntimeException("Unknown serverType. Currently only k3s is accepted.") + } else { + prov.provisionK3s(cmd as K3sCliCommand) } } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerCliCommand.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerCliCommand.kt index a02ea3f..c619725 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerCliCommand.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerCliCommand.kt @@ -2,6 +2,7 @@ package org.domaindrivenarchitecture.provs.server.domain import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand +import org.domaindrivenarchitecture.provs.server.infrastructure.genericFileExistenceCheck enum class ServerType { K3D, K3S @@ -12,7 +13,16 @@ open class ServerCliCommand( val target: TargetCliCommand, val configFileName: ConfigFileName?,) { - fun isValid(): Boolean { + fun isValidServerType(): Boolean { + return serverType == ServerType.K3S + } + fun isValidTarget(): Boolean { return target.isValid() } + fun isValidConfigFileName(): Boolean { + if (configFileName == null) { + return true + } + return genericFileExistenceCheck(configFileName.fileName) + } } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sCliCommand.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sCliCommand.kt index 1cca7f6..d4526b9 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sCliCommand.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/k3s/K3sCliCommand.kt @@ -4,6 +4,7 @@ import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand import org.domaindrivenarchitecture.provs.server.domain.ServerCliCommand import org.domaindrivenarchitecture.provs.server.domain.ServerType +import org.domaindrivenarchitecture.provs.server.infrastructure.genericFileExistenceCheck class K3sCliCommand( serverType: ServerType, @@ -12,7 +13,15 @@ class K3sCliCommand( val applicationFileName: ApplicationFileName?, val submodules: List? = null, val reprovision: Reprovision = false -) : - ServerCliCommand( - serverType, target, configFileName - ) \ No newline at end of file +) : ServerCliCommand( + serverType, + target, + configFileName +) { + fun isValidApplicationFileName(): Boolean { + if (applicationFileName == null) { + return true + } + return genericFileExistenceCheck(applicationFileName.fileName) + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/CliConvenience.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/CliConvenience.kt index 060f893..25206f7 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/CliConvenience.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/CliConvenience.kt @@ -8,7 +8,6 @@ fun Prov.provisionServerCliConvenience() = task { } fun Prov.provisionKubectlCompletionAndAlias(): ProvResult = task { - cmd("kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null") cmd("echo 'alias k=kubectl' >>~/.bashrc") cmd("echo 'complete -o default -F __start_kubectl k' >>~/.bashrc") diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/localFiles.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/localFiles.kt new file mode 100644 index 0000000..35c37e4 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/localFiles.kt @@ -0,0 +1,12 @@ +package org.domaindrivenarchitecture.provs.server.infrastructure + +import java.io.File + +fun genericFileExistenceCheck(fileName: String): Boolean { + if (fileName.isEmpty()) { + return false + } else if ((!File(fileName).exists())) { + return false + } + return true +} diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt index a6cac4f..017b182 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt @@ -19,8 +19,9 @@ internal class CliArgumentParserTest { val result = parser.parseCommand(args = arrayOf("k3s", "local", "-c", "config.yaml")) // then - assertTrue(result.isValid()) - assertEquals(ServerType.K3S, result.serverType) + assertTrue(result.isValidServerType()) + assertTrue(result.isValidTarget()) + assertTrue(result.isValidConfigFileName()) } @Test @@ -32,8 +33,9 @@ internal class CliArgumentParserTest { val result: K3sCliCommand = parser.parseCommand(args = arrayOf("k3s", "local", "-o", "grafana")) as K3sCliCommand // then - assertTrue(result.isValid()) - assertEquals(ServerType.K3S, result.serverType) + assertTrue(result.isValidServerType()) + assertTrue(result.isValidTarget()) + assertTrue(result.isValidConfigFileName()) assertEquals(listOf("grafana"), result.submodules) assertEquals(TargetCliCommand("local"), result.target) } @@ -47,8 +49,8 @@ internal class CliArgumentParserTest { val result: K3sCliCommand = parser.parseCommand(args = arrayOf("k3s", "user@host.com", "-a", "app.yaml")) as K3sCliCommand // then - assertTrue(result.isValid()) - assertEquals(ServerType.K3S, result.serverType) + assertTrue(result.isValidTarget()) + assertTrue(result.isValidServerType()) assertEquals(ApplicationFileName("app.yaml"), result.applicationFileName) assertEquals(TargetCliCommand("user@host.com"), result.target) }