rafactoring for provs-desktop
This commit is contained in:
parent
cf5e00f833
commit
03cf8ee3ce
9 changed files with 56 additions and 63 deletions
|
@ -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'
|
||||
}
|
||||
|
|
|
@ -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<String>) {
|
||||
|
||||
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"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String>): WorkplaceCliCommand {
|
||||
fun parseWorkplaceArguments(args: Array<String>): DesktopCliCommand {
|
||||
super.parse(args)
|
||||
|
||||
return WorkplaceCliCommand(
|
||||
return DesktopCliCommand(
|
||||
configFileName ?: "WorkplaceConfig.yaml",
|
||||
TargetCliCommand(
|
||||
localHost,
|
|
@ -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"
|
||||
)
|
||||
}
|
||||
}
|
|
@ -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()
|
|
@ -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,
|
|
@ -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.
|
|
@ -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)) }
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue