From 03cf8ee3ce0481c859d62940f89fa885a1546dcf Mon Sep 17 00:00:00 2001 From: jem Date: Thu, 3 Feb 2022 09:06:03 +0100 Subject: [PATCH] rafactoring for provs-desktop --- build.gradle | 4 +- .../provs/desktop/application/Application.kt | 35 +++++++++++++--- ...rkplaceParser.kt => CliArgumentsParser.kt} | 6 +-- .../provs/desktop/application/CliWorkplace.kt | 41 ------------------- .../CliWorkplaceCommand.kt | 2 +- .../{WorkplaceConfig.kt => DesktopConfig.kt} | 2 +- ...rovisionWorkplace.kt => DesktopService.kt} | 11 ++++- .../infrastructure/ConfigRepository.kt | 14 +++---- .../desktop/application/CliWorkplaceKtTest.kt | 4 +- 9 files changed, 56 insertions(+), 63 deletions(-) rename src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/{CliWorkplaceParser.kt => CliArgumentsParser.kt} (81%) delete mode 100644 src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplace.kt rename src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/{application => domain}/CliWorkplaceCommand.kt (74%) rename src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/{WorkplaceConfig.kt => DesktopConfig.kt} (94%) rename src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/{ProvisionWorkplace.kt => DesktopService.kt} (89%) diff --git a/build.gradle b/build.gradle index 01d4828..286b857 100644 --- a/build.gradle +++ b/build.gradle @@ -86,7 +86,7 @@ dependencies { testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2' } - +// todo: for fatjars there is no need??? //create a single Jar with all dependencies excl. Kotlin libs without version-number task fatJarLatest(type: Jar) { from { @@ -137,7 +137,7 @@ task uberjarDesktop(type: Jar) { manifest { attributes 'Implementation-Title': 'Uberjar of provs', 'Implementation-Version': project.version, - 'Main-Class': 'org.domaindrivenarchitecture.provs.desktop.application.CliKt' + 'Main-Class': 'org.domaindrivenarchitecture.provs.desktop.application.ApplicationKt' } archiveFileName = 'provs-desktop.jar' } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt index 3a055ea..8ab39f8 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt @@ -1,14 +1,39 @@ package org.domaindrivenarchitecture.provs.desktop.application +import kotlinx.serialization.SerializationException import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.desktop.domain.provisionWorkplace -import org.domaindrivenarchitecture.provs.desktop.domain.WorkplaceConfig +import org.domaindrivenarchitecture.provs.desktop.domain.DesktopConfig +import org.domaindrivenarchitecture.provs.desktop.domain.provisionDesktop +import org.domaindrivenarchitecture.provs.desktop.infrastructure.getConfig +import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance +import java.io.FileNotFoundException +import kotlin.system.exitProcess /** - * Use case for provisioning a workplace + * Provisions a workplace locally or on a remote machine. Use option -h for help. */ -fun provision(prov: Prov, conf: WorkplaceConfig) { - with (conf) { - prov.provisionWorkplace(type, ssh?.keyPair(), gpg?.keyPair(), gitUserName, gitEmail) +fun main(args: Array) { + + val cmd = CliArgumentsParser("java -jar provs-desktop.jar").parseWorkplaceArguments(args) + if (!cmd.isValid()) { + println("Arguments are not valid, pls try option -h for help.") + exitProcess(1) + } + + val prov = createProvInstance(cmd.target, remoteHostSetSudoWithoutPasswordRequired = true) + + try { + provisionDesktop(prov, cmd) + } catch (e: SerializationException) { + println( + "Error: File \"${cmd.configFile}\" has an invalid format and or invalid data.\n" + ) + } catch (e: FileNotFoundException) { + println( + "Error: File\u001b[31m ${cmd.configFile} \u001b[0m was not found.\n" + + "Pls copy file \u001B[31m WorkplaceConfigExample.yaml \u001B[0m to file \u001B[31m ${cmd.configFile} \u001B[0m " + + "and change the content according to your needs.\n" + ) } } diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplaceParser.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt similarity index 81% rename from src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplaceParser.kt rename to src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt index 30cadb3..6ee5028 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplaceParser.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliArgumentsParser.kt @@ -6,7 +6,7 @@ import org.domaindrivenarchitecture.provs.framework.core.cli.CliTargetParser import org.domaindrivenarchitecture.provs.framework.core.cli.TargetCliCommand -open class CliWorkplaceParser(name: String) : CliTargetParser(name) { +open class CliArgumentsParser(name: String) : CliTargetParser(name) { val configFileName by argument( ArgType.String, @@ -15,10 +15,10 @@ open class CliWorkplaceParser(name: String) : CliTargetParser(name) { ).optional() - fun parseWorkplaceArguments(args: Array): WorkplaceCliCommand { + fun parseWorkplaceArguments(args: Array): DesktopCliCommand { super.parse(args) - return WorkplaceCliCommand( + return DesktopCliCommand( configFileName ?: "WorkplaceConfig.yaml", TargetCliCommand( localHost, diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplace.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplace.kt deleted file mode 100644 index ba2dd5b..0000000 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplace.kt +++ /dev/null @@ -1,41 +0,0 @@ -package org.domaindrivenarchitecture.provs.desktop.application - -import kotlinx.serialization.SerializationException -import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance -import org.domaindrivenarchitecture.provs.desktop.infrastructure.getConfig -import java.io.FileNotFoundException -import kotlin.system.exitProcess - - -/** - * Provisions a workplace locally or on a remote machine. Use option -h for help. - */ -fun main(args: Array) { - - val cmd = CliWorkplaceParser("java -jar provs.jar").parseWorkplaceArguments(args) - - if (!cmd.isValid()) { - println("Arguments are not valid, pls try option -h for help.") - exitProcess(1) - } - - try { - // retrieve config - val conf = getConfig(cmd.configFile) - - // create - val prov = createProvInstance(cmd.target, remoteHostSetSudoWithoutPasswordRequired = true) - provision(prov, conf) - - } catch (e: SerializationException) { - println( - "Error: File \"${cmd.configFile}\" has an invalid format and or invalid data.\n" - ) - } catch (e: FileNotFoundException) { - println( - "Error: File\u001b[31m ${cmd.configFile} \u001b[0m was not found.\n" + - "Pls copy file \u001B[31m WorkplaceConfigExample.yaml \u001B[0m to file \u001B[31m ${cmd.configFile} \u001B[0m " + - "and change the content according to your needs.\n" - ) - } -} diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplaceCommand.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/CliWorkplaceCommand.kt similarity index 74% rename from src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplaceCommand.kt rename to src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/CliWorkplaceCommand.kt index 1ad5c62..16ccea3 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplaceCommand.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/CliWorkplaceCommand.kt @@ -3,7 +3,7 @@ package org.domaindrivenarchitecture.provs.desktop.application import org.domaindrivenarchitecture.provs.framework.core.cli.TargetCliCommand -class WorkplaceCliCommand(val configFile: String, val target: TargetCliCommand) { +class DesktopCliCommand(val configFile: String, val target: TargetCliCommand) { fun isValid(): Boolean { return configFile.isNotEmpty() && target.isValid() diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/WorkplaceConfig.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopConfig.kt similarity index 94% rename from src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/WorkplaceConfig.kt rename to src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopConfig.kt index 1632869..2be3152 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/WorkplaceConfig.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopConfig.kt @@ -5,7 +5,7 @@ import kotlinx.serialization.Serializable @Serializable -class WorkplaceConfig( +class DesktopConfig( val type: WorkplaceType = WorkplaceType.MINIMAL, val ssh: KeyPairSource? = null, val gpg: KeyPairSource? = null, diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/ProvisionWorkplace.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt similarity index 89% rename from src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/ProvisionWorkplace.kt rename to src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt index 5c74c1e..65df9f6 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/ProvisionWorkplace.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/domain/DesktopService.kt @@ -1,5 +1,8 @@ package org.domaindrivenarchitecture.provs.desktop.domain +import org.domaindrivenarchitecture.provs.desktop.application.DesktopCliCommand +import org.domaindrivenarchitecture.provs.desktop.infrastructure.* +import org.domaindrivenarchitecture.provs.desktop.infrastructure.getConfig import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.core.ProvResult import org.domaindrivenarchitecture.provs.framework.ubuntu.git.provisionGit @@ -11,8 +14,14 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.base.gpgFingerpr import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.provisionKeys import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.currentUserCanSudo import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami -import org.domaindrivenarchitecture.provs.desktop.infrastructure.* +fun provisionDesktop(prov: Prov, cmd: DesktopCliCommand) { + // retrieve config + val conf = getConfig(cmd.configFile) + with (conf) { + prov.provisionWorkplace(type, ssh?.keyPair(), gpg?.keyPair(), gitUserName, gitEmail) + } +} /** * Provisions software and configurations for a personal workplace. diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepository.kt index 81ef257..f7f977a 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/ConfigRepository.kt @@ -3,7 +3,7 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure import com.charleskorn.kaml.Yaml import kotlinx.serialization.json.Json import org.domaindrivenarchitecture.provs.framework.core.tags.Api -import org.domaindrivenarchitecture.provs.desktop.domain.WorkplaceConfig +import org.domaindrivenarchitecture.provs.desktop.domain.DesktopConfig import java.io.BufferedReader import java.io.FileReader import java.io.FileWriter @@ -13,7 +13,7 @@ import java.io.FileWriter * Returns WorkplaceConfig; data for config is read from specified file. * Throws exceptions FileNotFoundException, SerializationException if file is not found resp. cannot be parsed. */ -internal fun getConfig(filename: String = "WorkplaceConfig.yaml"): WorkplaceConfig { +internal fun getConfig(filename: String = "WorkplaceConfig.yaml"): DesktopConfig { // read file val inputAsString = BufferedReader(FileReader(filename)).use { it.readText() } @@ -21,25 +21,25 @@ internal fun getConfig(filename: String = "WorkplaceConfig.yaml"): WorkplaceConf // deserializing val config = if (filename.lowercase().endsWith(".yaml")) { - Yaml.default.decodeFromString(WorkplaceConfig.serializer(), inputAsString) + Yaml.default.decodeFromString(DesktopConfig.serializer(), inputAsString) } else { - Json.decodeFromString(WorkplaceConfig.serializer(), inputAsString) + Json.decodeFromString(DesktopConfig.serializer(), inputAsString) } return config } @Api -internal fun writeConfig(config: WorkplaceConfig, fileName: String = "WorkplaceConfigExample.yaml") { +internal fun writeConfig(config: DesktopConfig, fileName: String = "WorkplaceConfigExample.yaml") { if (fileName.lowercase().endsWith(".yaml")) { FileWriter(fileName).use { it.write( Yaml.default.encodeToString( - WorkplaceConfig.serializer(), + DesktopConfig.serializer(), config ) ) } } else { - FileWriter(fileName).use { it.write(Json.encodeToString(WorkplaceConfig.serializer(), config)) } + FileWriter(fileName).use { it.write(Json.encodeToString(DesktopConfig.serializer(), config)) } } } \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplaceKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplaceKtTest.kt index ea60ed0..f3b5bbb 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplaceKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/application/CliWorkplaceKtTest.kt @@ -9,7 +9,7 @@ import org.domaindrivenarchitecture.provs.framework.core.* import org.domaindrivenarchitecture.provs.framework.core.cli.retrievePassword import org.domaindrivenarchitecture.provs.framework.core.processors.PrintOnlyProcessor import org.domaindrivenarchitecture.provs.test.setRootLoggingLevel -import org.domaindrivenarchitecture.provs.desktop.domain.WorkplaceConfig +import org.domaindrivenarchitecture.provs.desktop.domain.DesktopConfig import org.domaindrivenarchitecture.provs.desktop.domain.WorkplaceType import org.domaindrivenarchitecture.provs.desktop.domain.provisionWorkplace import org.domaindrivenarchitecture.provs.desktop.infrastructure.getConfig @@ -25,7 +25,7 @@ internal class CliWorkplaceKtTest { companion object { val printOnlyProv = Prov.newInstance(PrintOnlyProcessor()) - val testConfig = WorkplaceConfig(WorkplaceType.MINIMAL, gitUserName = "gittestuser", gitEmail = "git@test.mail") + val testConfig = DesktopConfig(WorkplaceType.MINIMAL, gitUserName = "gittestuser", gitEmail = "git@test.mail") @BeforeAll @JvmStatic