cli with submodules

This commit is contained in:
jem 2022-01-21 15:38:19 +01:00
parent ea9188dfdf
commit e9f79ed0a3
11 changed files with 132 additions and 150 deletions

View file

@ -1,13 +1,24 @@
package org.domaindrivenarchitecture.provs.server.application
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.server.domain.installK3sServer
import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance
import org.domaindrivenarchitecture.provs.server.domain.provisionServer
import kotlin.system.exitProcess
/**
* Performs use case of provisioning a k3s server
* Provisions a server, either locally or on a remote machine depending on the given arguments.
* Depending on the cli parameter "type" it will install the k3s server as standalone or as a container.
*
* Get help with option -h
*/
fun Prov.provisionK3s() = task {
installK3sServer()
}
fun main(args: Array<String>) {
val cmd = CliArgumentsParser("java -jar provs-server.jar").parseCommand(args)
if (!cmd.isValid()) {
println("Arguments are not valid, pls try -h for help.")
exitProcess(1)
}
val prov = createProvInstance(cmd.target)
provisionServer(prov, cmd)
}

View file

@ -0,0 +1,58 @@
package org.domaindrivenarchitecture.provs.server.application
import kotlinx.cli.ArgType
import kotlinx.cli.Subcommand
import kotlinx.cli.default
import org.domaindrivenarchitecture.provs.desktop.application.WorkplaceCliCommand
import org.domaindrivenarchitecture.provs.framework.core.cli.CliTargetParser
import org.domaindrivenarchitecture.provs.framework.core.cli.TargetCliCommand
import org.domaindrivenarchitecture.provs.server.domain.ServerCliCommand
import org.domaindrivenarchitecture.provs.server.domain.ServerType
class CliArgumentsParser(
name: String
) : CliTargetParser(name) {
val modules: List<ServerSubcommand> = listOf(K3s(), K3d())
init {
subcommands(*modules.toTypedArray())
}
fun parseCommand(args: Array<String>): ServerCliCommand {
super.parse(args)
val module = modules.first { it.parsed }
return ServerCliCommand(
ServerType.valueOf(module.name.uppercase()),
TargetCliCommand(
localHost,
remoteHost,
userName,
sshWithPasswordPrompt,
sshWithGopassPath,
sshWithKey
)
)
}
abstract class ServerSubcommand(name: String, description: String): Subcommand(name, description) {
var parsed = false
}
class K3s: ServerSubcommand("k3s", "the k3s module") {
override fun execute() {
parsed = true
}
}
class K3d: ServerSubcommand("k3d", "the k3s module") {
override fun execute() {
TODO("Not yet implemented")
}
}
}

View file

@ -1,27 +0,0 @@
package org.domaindrivenarchitecture.provs.server.application
import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance
import org.domaindrivenarchitecture.provs.server.domain.installK3sAsContainers
import kotlin.system.exitProcess
/**
* Provisions a server, either locally or on a remote machine depending on the given arguments.
* Depending on the cli parameter "type" it will install the k3s server as standalone or as a container.
*
* Get help with option -h
*/
fun main(args: Array<String>) {
val cmd = parseServerArguments("java -jar provs-server.jar", args)
if (!cmd.isValid()) {
println("Arguments are not valid, pls try -h for help.")
exitProcess(1)
}
val prov = createProvInstance(cmd.target)
when (cmd.type()) {
CliServerArgumentsParser.K3sType.K3S -> prov.provisionK3s()
CliServerArgumentsParser.K3sType.K3D -> prov.installK3sAsContainers()
}
}

View file

@ -1,19 +0,0 @@
package org.domaindrivenarchitecture.provs.server.application
import kotlinx.cli.ArgType
import kotlinx.cli.default
import org.domaindrivenarchitecture.provs.framework.core.cli.CliTargetParser
class CliServerArgumentsParser(name: String) : CliTargetParser(name) {
enum class K3sType {
K3S, K3D
}
val type by option(
ArgType.String,
"type",
"t",
"either k3s (for standalone) or k3d for k3s running in a container"
).default("k3s")
}

View file

@ -1,36 +0,0 @@
package org.domaindrivenarchitecture.provs.server.application
import org.domaindrivenarchitecture.provs.framework.core.cli.TargetCliCommand
class ServerCliCommand(private val k3sType: String, val target: TargetCliCommand) {
fun isValid(): Boolean {
return target.isValid() && hasValidK3sType()
}
private fun hasValidK3sType(): Boolean {
return CliServerArgumentsParser.K3sType.values().map { it.name }.contains(k3sType.uppercase())
}
fun type() = CliServerArgumentsParser.K3sType.valueOf(k3sType.uppercase())
}
fun parseServerArguments(
programName: String = "java -jar provs.jar",
args: Array<String>
): ServerCliCommand {
val parser = CliServerArgumentsParser(programName)
parser.parse(args)
return ServerCliCommand(
parser.type,
TargetCliCommand(
parser.localHost,
parser.remoteHost,
parser.userName,
parser.sshWithPasswordPrompt,
parser.sshWithGopassPath,
parser.sshWithKey
)
)
}

View file

@ -0,0 +1,7 @@
package org.domaindrivenarchitecture.provs.server.domain
import org.domaindrivenarchitecture.provs.framework.core.Prov
fun provisionServer(prov: Prov, cmd: ServerCliCommand) {
}

View file

@ -0,0 +1,13 @@
package org.domaindrivenarchitecture.provs.server.application
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.server.domain.installK3sServer
/**
* Performs use case of provisioning a k3s server
*/
fun Prov.provisionK3s() = task {
installK3sServer()
}

View file

@ -0,0 +1,16 @@
package org.domaindrivenarchitecture.provs.server.domain
import org.domaindrivenarchitecture.provs.framework.core.cli.TargetCliCommand
enum class ServerType {
K3D, K3S
}
class ServerCliCommand(
val serverType: ServerType,
val target: TargetCliCommand)
{
fun isValid(): Boolean {
return target.isValid()
}
}

View file

@ -0,0 +1,21 @@
package org.domaindrivenarchitecture.provs.server.application
import org.domaindrivenarchitecture.provs.server.domain.ServerType
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
internal class CliArgumentParserTest {
@Test
fun test_parseServerArguments_are_valid_for_k3s() {
// given
val parser = CliArgumentsParser("test")
// when
val result = parser.parseCommand(args = arrayOf("k3s", "-l"))
// then
assertTrue(result.isValid())
assertEquals(ServerType.K3S, result.serverType)
}
}

View file

@ -1,47 +0,0 @@
package org.domaindrivenarchitecture.provs.server.application
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
internal class CliServerCommandKtTest {
@Test
fun test_parseServerArguments_are_valid_for_k3s() {
// when
val cmd = parseServerArguments(args = arrayOf("-l", "-t", "k3s"))
// then
assertTrue(cmd.isValid())
assertEquals(CliServerArgumentsParser.K3sType.K3S, cmd.type())
}
@Test
fun test_parseServerArguments_are_invalid_without_target() {
// when
val cmd = parseServerArguments(args = arrayOf("-t", "k3s"))
// then
assertFalse(cmd.isValid())
assertEquals(CliServerArgumentsParser.K3sType.K3S, cmd.type())
}
@Test
fun test_parseServerArguments_has_default_type_k3s() {
// when
val cmd = parseServerArguments(args = arrayOf("-l"))
// then
assertTrue(cmd.isValid())
assertEquals(CliServerArgumentsParser.K3sType.K3S, cmd.type())
}
@Test
fun test_parseServerArguments_are_valid_for_k3d() {
// when
val cmd = parseServerArguments(args = arrayOf("-l", "-t", "k3d"))
// then
assertTrue(cmd.isValid())
assertEquals(CliServerArgumentsParser.K3sType.K3D, cmd.type())
}
}

View file

@ -1,15 +0,0 @@
package org.domaindrivenarchitecture.provs.server.application
import org.domaindrivenarchitecture.provs.server.application.main
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
internal class CliServerKtTest {
@Test
@Disabled // run manually -- todo mock execution
fun provision_remotely() {
main(arrayOf("-r", "192.168.56.141", "-u", "user", "-i"))
}
}