From 8323b84bbe3b69cf9ebd9422382f3548dbc94bdb Mon Sep 17 00:00:00 2001 From: az Date: Fri, 12 May 2023 14:13:23 +0200 Subject: [PATCH] use default config file if no config was specified and default config file exists --- .../provs/desktop/application/Application.kt | 18 +++++++++++++----- .../infrastructure/DesktopConfigRepository.kt | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) 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 86f3de3..b08f513 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/application/Application.kt @@ -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.quit import java.io.FileNotFoundException +import java.nio.file.Files +import kotlin.io.path.Path import kotlin.system.exitProcess /** @@ -21,22 +23,28 @@ fun main(args: Array) { 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 { - getConfig(cmd.configFile.fileName) + getConfig(configFileName) } catch (e: SerializationException) { 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 } catch (e: FileNotFoundException) { println( - "Error: File\u001b[31m ${cmd.configFile.fileName} \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 " + + "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 ${configFileName} \u001B[0m " + "and change the content according to your needs." ) null } + } if (config == null) { println("No suitable config found.") diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DesktopConfigRepository.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DesktopConfigRepository.kt index efaf236..b35ee88 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DesktopConfigRepository.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DesktopConfigRepository.kt @@ -11,7 +11,7 @@ import java.io.FileWriter * Returns DesktopConfig; data for config is read from specified file. * 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")