use default config file if no config was specified and default config file exists

This commit is contained in:
az 2023-05-12 14:13:23 +02:00
parent aef96be7f3
commit 8323b84bbe
2 changed files with 14 additions and 6 deletions

View file

@ -8,6 +8,8 @@ import org.domaindrivenarchitecture.provs.desktop.infrastructure.getConfig
import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance
import org.domaindrivenarchitecture.provs.framework.core.cli.quit import org.domaindrivenarchitecture.provs.framework.core.cli.quit
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.nio.file.Files
import kotlin.io.path.Path
import kotlin.system.exitProcess import kotlin.system.exitProcess
/** /**
@ -21,22 +23,28 @@ fun main(args: Array<String>) {
exitProcess(1) exitProcess(1)
} }
val config = if (cmd.configFile == null) DesktopConfig() else val defaultConfigFileName = "desktop-config.yaml"
val config = if ((cmd.configFile == null) && !Files.isRegularFile(Path(defaultConfigFileName))) {
println("ATTENTION: No config provided => using an empty config.")
DesktopConfig()
} else {
val configFileName = cmd.configFile?.fileName ?: defaultConfigFileName
try { try {
getConfig(cmd.configFile.fileName) getConfig(configFileName)
} catch (e: SerializationException) { } catch (e: SerializationException) {
println( println(
"Error: File \"${cmd.configFile.fileName}\" has an invalid format and or invalid data." "Error: File \"${configFileName}\" has an invalid format and or invalid data."
) )
null null
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
println( println(
"Error: File\u001b[31m ${cmd.configFile.fileName} \u001b[0m was not found.\n" + "Error: File\u001b[31m ${configFileName} \u001b[0m was not found.\n" +
"Pls copy file \u001B[31m desktop-config-example.yaml \u001B[0m to file \u001B[31m ${cmd.configFile.fileName} \u001B[0m " + "Pls copy file \u001B[31m desktop-config-example.yaml \u001B[0m to file \u001B[31m ${configFileName} \u001B[0m " +
"and change the content according to your needs." "and change the content according to your needs."
) )
null null
} }
}
if (config == null) { if (config == null) {
println("No suitable config found.") println("No suitable config found.")

View file

@ -11,7 +11,7 @@ import java.io.FileWriter
* Returns DesktopConfig; data for config is read from specified file. * Returns DesktopConfig; 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.
*/ */
fun getConfig(filename: String = "desktop-config.yaml"): DesktopConfig = readFromFile(filename).yamlToType() fun getConfig(filename: String): DesktopConfig = readFromFile(filename).yamlToType()
@Suppress("unused") @Suppress("unused")