From e9f79ed0a3fb35fd1b0085d5567f058404437018 Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 21 Jan 2022 15:38:19 +0100 Subject: [PATCH] cli with submodules --- .../provs/server/application/Application.kt | 23 ++++++-- .../server/application/CliArgumentsParser.kt | 58 +++++++++++++++++++ .../provs/server/application/CliServer.kt | 27 --------- .../application/CliServerArgumentsParser.kt | 19 ------ .../server/application/CliServerCommand.kt | 36 ------------ .../provs/server/domain/ApplicationService.kt | 7 +++ .../server/domain/ServerApplicationService.kt | 13 +++++ .../provs/server/domain/ServerCliCommand.kt | 16 +++++ .../application/CliArgumentParserTest.kt | 21 +++++++ .../application/CliServerCommandKtTest.kt | 47 --------------- .../server/application/CliServerKtTest.kt | 15 ----- 11 files changed, 132 insertions(+), 150 deletions(-) create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentsParser.kt delete mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServer.kt delete mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerArgumentsParser.kt delete mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerCommand.kt create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ApplicationService.kt create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerApplicationService.kt create mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerCliCommand.kt create mode 100644 src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt delete mode 100644 src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerCommandKtTest.kt delete mode 100644 src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerKtTest.kt 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 8414db2..824742c 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/Application.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/Application.kt @@ -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) { + 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) + +} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentsParser.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentsParser.kt new file mode 100644 index 0000000..3d743be --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentsParser.kt @@ -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 = listOf(K3s(), K3d()) + init { + subcommands(*modules.toTypedArray()) + } + + fun parseCommand(args: Array): 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") + } + } + + + +} + diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServer.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServer.kt deleted file mode 100644 index 7134649..0000000 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServer.kt +++ /dev/null @@ -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) { - - 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() - } -} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerArgumentsParser.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerArgumentsParser.kt deleted file mode 100644 index b31a84a..0000000 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerArgumentsParser.kt +++ /dev/null @@ -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") -} \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerCommand.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerCommand.kt deleted file mode 100644 index d3d2827..0000000 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerCommand.kt +++ /dev/null @@ -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 -): 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 - ) - ) -} - - diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ApplicationService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ApplicationService.kt new file mode 100644 index 0000000..fdec3ef --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ApplicationService.kt @@ -0,0 +1,7 @@ +package org.domaindrivenarchitecture.provs.server.domain + +import org.domaindrivenarchitecture.provs.framework.core.Prov + +fun provisionServer(prov: Prov, cmd: ServerCliCommand) { + +} \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerApplicationService.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerApplicationService.kt new file mode 100644 index 0000000..8414db2 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerApplicationService.kt @@ -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() +} + diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerCliCommand.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerCliCommand.kt new file mode 100644 index 0000000..a5305a8 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/server/domain/ServerCliCommand.kt @@ -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() + } +} diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt new file mode 100644 index 0000000..755597f --- /dev/null +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliArgumentParserTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerCommandKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerCommandKtTest.kt deleted file mode 100644 index 4dd1cb6..0000000 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerCommandKtTest.kt +++ /dev/null @@ -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()) - } -} \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerKtTest.kt deleted file mode 100644 index db209ae..0000000 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/application/CliServerKtTest.kt +++ /dev/null @@ -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")) - } -} \ No newline at end of file