Prepare for improve of input validation

We now check application and config files before starting provisioning
process. We also check serverType and target validity before starting.
This happens in unified manner in the main function.
This commit is contained in:
erik 2022-08-17 14:05:17 +02:00
parent 77351933fe
commit 6e2d669af5
6 changed files with 64 additions and 17 deletions

View file

@ -1,9 +1,11 @@
package org.domaindrivenarchitecture.provs.server.application package org.domaindrivenarchitecture.provs.server.application
import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance 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.ServerType
import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sCliCommand import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sCliCommand
import org.domaindrivenarchitecture.provs.server.domain.k3s.provisionK3s import org.domaindrivenarchitecture.provs.server.domain.k3s.provisionK3s
import org.domaindrivenarchitecture.provs.server.infrastructure.genericFileExistenceCheck
import kotlin.system.exitProcess import kotlin.system.exitProcess
@ -18,13 +20,26 @@ fun main(args: Array<String>) {
val checkedArgs = if (args.isEmpty()) arrayOf("-h") else args val checkedArgs = if (args.isEmpty()) arrayOf("-h") else args
val cmd = CliArgumentsParser("provs-server.jar subcommand target").parseCommand(checkedArgs) 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) 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) val prov = createProvInstance(cmd.target)
when(cmd.serverType) {
ServerType.K3S -> prov.provisionK3s(cmd as K3sCliCommand) if (!cmd.isValidServerType()) {
else -> { throw RuntimeException("Unknown serverType") } throw RuntimeException("Unknown serverType. Currently only k3s is accepted.")
} else {
prov.provisionK3s(cmd as K3sCliCommand)
} }
} }

View file

@ -2,6 +2,7 @@ package org.domaindrivenarchitecture.provs.server.domain
import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand
import org.domaindrivenarchitecture.provs.server.infrastructure.genericFileExistenceCheck
enum class ServerType { enum class ServerType {
K3D, K3S K3D, K3S
@ -12,7 +13,16 @@ open class ServerCliCommand(
val target: TargetCliCommand, val target: TargetCliCommand,
val configFileName: ConfigFileName?,) val configFileName: ConfigFileName?,)
{ {
fun isValid(): Boolean { fun isValidServerType(): Boolean {
return serverType == ServerType.K3S
}
fun isValidTarget(): Boolean {
return target.isValid() return target.isValid()
} }
fun isValidConfigFileName(): Boolean {
if (configFileName == null) {
return true
}
return genericFileExistenceCheck(configFileName.fileName)
}
} }

View file

@ -4,6 +4,7 @@ import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand
import org.domaindrivenarchitecture.provs.server.domain.ServerCliCommand import org.domaindrivenarchitecture.provs.server.domain.ServerCliCommand
import org.domaindrivenarchitecture.provs.server.domain.ServerType import org.domaindrivenarchitecture.provs.server.domain.ServerType
import org.domaindrivenarchitecture.provs.server.infrastructure.genericFileExistenceCheck
class K3sCliCommand( class K3sCliCommand(
serverType: ServerType, serverType: ServerType,
@ -12,7 +13,15 @@ class K3sCliCommand(
val applicationFileName: ApplicationFileName?, val applicationFileName: ApplicationFileName?,
val submodules: List<String>? = null, val submodules: List<String>? = null,
val reprovision: Reprovision = false val reprovision: Reprovision = false
) : ) : ServerCliCommand(
ServerCliCommand( serverType,
serverType, target, configFileName target,
) configFileName
) {
fun isValidApplicationFileName(): Boolean {
if (applicationFileName == null) {
return true
}
return genericFileExistenceCheck(applicationFileName.fileName)
}
}

View file

@ -8,7 +8,6 @@ fun Prov.provisionServerCliConvenience() = task {
} }
fun Prov.provisionKubectlCompletionAndAlias(): ProvResult = task { fun Prov.provisionKubectlCompletionAndAlias(): ProvResult = task {
cmd("kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null") cmd("kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null")
cmd("echo 'alias k=kubectl' >>~/.bashrc") cmd("echo 'alias k=kubectl' >>~/.bashrc")
cmd("echo 'complete -o default -F __start_kubectl k' >>~/.bashrc") cmd("echo 'complete -o default -F __start_kubectl k' >>~/.bashrc")

View file

@ -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
}

View file

@ -19,8 +19,9 @@ internal class CliArgumentParserTest {
val result = parser.parseCommand(args = arrayOf("k3s", "local", "-c", "config.yaml")) val result = parser.parseCommand(args = arrayOf("k3s", "local", "-c", "config.yaml"))
// then // then
assertTrue(result.isValid()) assertTrue(result.isValidServerType())
assertEquals(ServerType.K3S, result.serverType) assertTrue(result.isValidTarget())
assertTrue(result.isValidConfigFileName())
} }
@Test @Test
@ -32,8 +33,9 @@ internal class CliArgumentParserTest {
val result: K3sCliCommand = parser.parseCommand(args = arrayOf("k3s", "local", "-o", "grafana")) as K3sCliCommand val result: K3sCliCommand = parser.parseCommand(args = arrayOf("k3s", "local", "-o", "grafana")) as K3sCliCommand
// then // then
assertTrue(result.isValid()) assertTrue(result.isValidServerType())
assertEquals(ServerType.K3S, result.serverType) assertTrue(result.isValidTarget())
assertTrue(result.isValidConfigFileName())
assertEquals(listOf("grafana"), result.submodules) assertEquals(listOf("grafana"), result.submodules)
assertEquals(TargetCliCommand("local"), result.target) 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 val result: K3sCliCommand = parser.parseCommand(args = arrayOf("k3s", "user@host.com", "-a", "app.yaml")) as K3sCliCommand
// then // then
assertTrue(result.isValid()) assertTrue(result.isValidTarget())
assertEquals(ServerType.K3S, result.serverType) assertTrue(result.isValidServerType())
assertEquals(ApplicationFileName("app.yaml"), result.applicationFileName) assertEquals(ApplicationFileName("app.yaml"), result.applicationFileName)
assertEquals(TargetCliCommand("user@host.com"), result.target) assertEquals(TargetCliCommand("user@host.com"), result.target)
} }