From 7152346a865582a183a4539acd8c70d6bb2e05c7 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Sun, 7 May 2023 21:07:30 +0200 Subject: [PATCH] Exit if configuration is not found --- Sources/App/Config.swift | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/Sources/App/Config.swift b/Sources/App/Config.swift index ef2c364..a5f72a0 100644 --- a/Sources/App/Config.swift +++ b/Sources/App/Config.swift @@ -31,12 +31,11 @@ extension Config { init(loadFrom directory: URL) { let configFileUrl = Config.file(in: directory) - if FileManager.default.fileExists(atPath: configFileUrl.path) { - self.init(loadAt: configFileUrl) - } else { - self.init(standardIn: directory) - write(to: configFileUrl) + guard FileManager.default.fileExists(atPath: configFileUrl.path) else { + print("No configuration found at \(configFileUrl.path)") + exit(-1) } + self.init(loadAt: configFileUrl) } private init(loadAt url: URL) { @@ -45,23 +44,7 @@ extension Config { self = try JSONDecoder().decode(Config.self, from: configData) } catch { print("Failed to load configuration from \(url.path): \(error)") - print("Using default configuration") - self.init(standardIn: url.deletingLastPathComponent()) - } - } - - private init(standardIn directory: URL) { - let defaultLogPath = directory.appendingPathComponent("logs").path - self.init(port: 8000, maxBodySize: "2mb", logPath: defaultLogPath, serveFiles: true, writers: []) - } - - private func write(to url: URL) { - do { - let configData = try JSONEncoder().encode(self) - try configData.write(to: url) - print("Configuration written at \(url.path)") - } catch { - print("Failed to write default configuration to \(url.path)") + exit(-1) } } }