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.
merge-requests/2/head
erik 2 years ago
parent 77351933fe
commit 6e2d669af5

@ -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<String>) {
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)
}
}

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

@ -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<String>? = null,
val reprovision: Reprovision = false
) :
ServerCliCommand(
serverType, target, configFileName
)
) : ServerCliCommand(
serverType,
target,
configFileName
) {
fun isValidApplicationFileName(): Boolean {
if (applicationFileName == null) {
return true
}
return genericFileExistenceCheck(applicationFileName.fileName)
}
}

@ -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")

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

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

Loading…
Cancel
Save