rafactoring for provs-desktop

This commit is contained in:
jem 2022-02-03 09:06:03 +01:00
parent cf5e00f833
commit 03cf8ee3ce
9 changed files with 56 additions and 63 deletions

View file

@ -86,7 +86,7 @@ dependencies {
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2' 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 //create a single Jar with all dependencies excl. Kotlin libs without version-number
task fatJarLatest(type: Jar) { task fatJarLatest(type: Jar) {
from { from {
@ -137,7 +137,7 @@ task uberjarDesktop(type: Jar) {
manifest { manifest {
attributes 'Implementation-Title': 'Uberjar of provs', attributes 'Implementation-Title': 'Uberjar of provs',
'Implementation-Version': project.version, 'Implementation-Version': project.version,
'Main-Class': 'org.domaindrivenarchitecture.provs.desktop.application.CliKt' 'Main-Class': 'org.domaindrivenarchitecture.provs.desktop.application.ApplicationKt'
} }
archiveFileName = 'provs-desktop.jar' archiveFileName = 'provs-desktop.jar'
} }

View file

@ -1,14 +1,39 @@
package org.domaindrivenarchitecture.provs.desktop.application package org.domaindrivenarchitecture.provs.desktop.application
import kotlinx.serialization.SerializationException
import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.desktop.domain.provisionWorkplace 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) { fun main(args: Array<String>) {
with (conf) {
prov.provisionWorkplace(type, ssh?.keyPair(), gpg?.keyPair(), gitUserName, gitEmail) 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"
)
} }
} }

View file

@ -6,7 +6,7 @@ import org.domaindrivenarchitecture.provs.framework.core.cli.CliTargetParser
import org.domaindrivenarchitecture.provs.framework.core.cli.TargetCliCommand 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( val configFileName by argument(
ArgType.String, ArgType.String,
@ -15,10 +15,10 @@ open class CliWorkplaceParser(name: String) : CliTargetParser(name) {
).optional() ).optional()
fun parseWorkplaceArguments(args: Array<String>): WorkplaceCliCommand { fun parseWorkplaceArguments(args: Array<String>): DesktopCliCommand {
super.parse(args) super.parse(args)
return WorkplaceCliCommand( return DesktopCliCommand(
configFileName ?: "WorkplaceConfig.yaml", configFileName ?: "WorkplaceConfig.yaml",
TargetCliCommand( TargetCliCommand(
localHost, localHost,

View file

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

View file

@ -3,7 +3,7 @@ package org.domaindrivenarchitecture.provs.desktop.application
import org.domaindrivenarchitecture.provs.framework.core.cli.TargetCliCommand 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 { fun isValid(): Boolean {
return configFile.isNotEmpty() && target.isValid() return configFile.isNotEmpty() && target.isValid()

View file

@ -5,7 +5,7 @@ import kotlinx.serialization.Serializable
@Serializable @Serializable
class WorkplaceConfig( class DesktopConfig(
val type: WorkplaceType = WorkplaceType.MINIMAL, val type: WorkplaceType = WorkplaceType.MINIMAL,
val ssh: KeyPairSource? = null, val ssh: KeyPairSource? = null,
val gpg: KeyPairSource? = null, val gpg: KeyPairSource? = null,

View file

@ -1,5 +1,8 @@
package org.domaindrivenarchitecture.provs.desktop.domain 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.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.git.provisionGit 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.keys.provisionKeys
import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.currentUserCanSudo import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.currentUserCanSudo
import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami 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. * Provisions software and configurations for a personal workplace.

View file

@ -3,7 +3,7 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure
import com.charleskorn.kaml.Yaml import com.charleskorn.kaml.Yaml
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.domaindrivenarchitecture.provs.framework.core.tags.Api 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.BufferedReader
import java.io.FileReader import java.io.FileReader
import java.io.FileWriter import java.io.FileWriter
@ -13,7 +13,7 @@ import java.io.FileWriter
* Returns WorkplaceConfig; data for config is read from specified file. * Returns WorkplaceConfig; data for config is read from specified file.
* Throws exceptions FileNotFoundException, SerializationException if file is not found resp. cannot be parsed. * 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 // read file
val inputAsString = BufferedReader(FileReader(filename)).use { it.readText() } val inputAsString = BufferedReader(FileReader(filename)).use { it.readText() }
@ -21,25 +21,25 @@ internal fun getConfig(filename: String = "WorkplaceConfig.yaml"): WorkplaceConf
// deserializing // deserializing
val config = val config =
if (filename.lowercase().endsWith(".yaml")) { if (filename.lowercase().endsWith(".yaml")) {
Yaml.default.decodeFromString(WorkplaceConfig.serializer(), inputAsString) Yaml.default.decodeFromString(DesktopConfig.serializer(), inputAsString)
} else { } else {
Json.decodeFromString(WorkplaceConfig.serializer(), inputAsString) Json.decodeFromString(DesktopConfig.serializer(), inputAsString)
} }
return config return config
} }
@Api @Api
internal fun writeConfig(config: WorkplaceConfig, fileName: String = "WorkplaceConfigExample.yaml") { internal fun writeConfig(config: DesktopConfig, fileName: String = "WorkplaceConfigExample.yaml") {
if (fileName.lowercase().endsWith(".yaml")) { if (fileName.lowercase().endsWith(".yaml")) {
FileWriter(fileName).use { FileWriter(fileName).use {
it.write( it.write(
Yaml.default.encodeToString( Yaml.default.encodeToString(
WorkplaceConfig.serializer(), DesktopConfig.serializer(),
config config
) )
) )
} }
} else { } else {
FileWriter(fileName).use { it.write(Json.encodeToString(WorkplaceConfig.serializer(), config)) } FileWriter(fileName).use { it.write(Json.encodeToString(DesktopConfig.serializer(), config)) }
} }
} }

View file

@ -9,7 +9,7 @@ import org.domaindrivenarchitecture.provs.framework.core.*
import org.domaindrivenarchitecture.provs.framework.core.cli.retrievePassword import org.domaindrivenarchitecture.provs.framework.core.cli.retrievePassword
import org.domaindrivenarchitecture.provs.framework.core.processors.PrintOnlyProcessor import org.domaindrivenarchitecture.provs.framework.core.processors.PrintOnlyProcessor
import org.domaindrivenarchitecture.provs.test.setRootLoggingLevel 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.WorkplaceType
import org.domaindrivenarchitecture.provs.desktop.domain.provisionWorkplace import org.domaindrivenarchitecture.provs.desktop.domain.provisionWorkplace
import org.domaindrivenarchitecture.provs.desktop.infrastructure.getConfig import org.domaindrivenarchitecture.provs.desktop.infrastructure.getConfig
@ -25,7 +25,7 @@ internal class CliWorkplaceKtTest {
companion object { companion object {
val printOnlyProv = Prov.newInstance(PrintOnlyProcessor()) 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 @BeforeAll
@JvmStatic @JvmStatic