diff --git a/Sources/Compiler/Config.swift b/Sources/Compiler/Config.swift index c3fa830..0848b66 100644 --- a/Sources/Compiler/Config.swift +++ b/Sources/Compiler/Config.swift @@ -16,19 +16,31 @@ public struct Config: Codable { public let additionalImports: [String]? public let tableNamePattern: String? - struct NotFoundError: Error, CustomStringConvertible { - var description: String { "Config does not exist" } + enum ConfigError: Error, CustomStringConvertible { + case invalidURL(String) + case notFound(searchPath: String) + + var description: String { + switch self { + case .invalidURL(let url): + "Invalid URL '\(url)'" + case .notFound(let searchPath): + "Config does not exist in '\(searchPath)'" + } + } } public init(at path: String) throws { - var url = URL(fileURLWithPath: path) + guard var url = URL(string: path) else { + throw ConfigError.invalidURL(path) + } if url.lastPathComponent != "puresql.yaml" { url.appendPathComponent("puresql.yaml") } guard FileManager.default.fileExists(atPath: url.path) else { - throw NotFoundError() + throw ConfigError.notFound(searchPath: url.path) } let data = try Data(contentsOf: url) @@ -37,8 +49,10 @@ public struct Config: Codable { self = try decoder.decode(Config.self, from: data) } - public func project(at path: String) -> Project { - let url = URL(fileURLWithPath: path) + public func project(at path: String) throws -> Project { + guard let url = URL(string: path) else { + throw ConfigError.invalidURL(path) + } return Project( generatedOutputFile: url.appendingPathComponent(output ?? "Queries.swift"), diff --git a/Sources/PureSQLCLI/GenerateCommand.swift b/Sources/PureSQLCLI/GenerateCommand.swift index acf999f..fb3b2bf 100644 --- a/Sources/PureSQLCLI/GenerateCommand.swift +++ b/Sources/PureSQLCLI/GenerateCommand.swift @@ -36,7 +36,7 @@ struct GenerateCommand: AsyncParsableCommand { mutating func run() async throws { let config = try Config(at: path) - var project = config.project(at: path) + var project = try config.project(at: path) if let overrideOutput, let url = URL(string: overrideOutput) { project.generatedOutputFile = url diff --git a/Sources/PureSQLCLI/MigrationsCommand.swift b/Sources/PureSQLCLI/MigrationsCommand.swift index d402781..c70473e 100644 --- a/Sources/PureSQLCLI/MigrationsCommand.swift +++ b/Sources/PureSQLCLI/MigrationsCommand.swift @@ -23,7 +23,7 @@ struct MigrationsCommand: ParsableCommand { func run() throws { let config = try Config(at: path) - let project = config.project(at: path) + let project = try config.project(at: path) try project.addMigration() } } diff --git a/Sources/PureSQLCLI/QueriesCommand.swift b/Sources/PureSQLCLI/QueriesCommand.swift index dd2b02f..38c5348 100644 --- a/Sources/PureSQLCLI/QueriesCommand.swift +++ b/Sources/PureSQLCLI/QueriesCommand.swift @@ -25,7 +25,7 @@ struct QueriesCommand: ParsableCommand { func run() throws { let config = try Config(at: path) - let project = config.project(at: path) + let project = try config.project(at: path) guard !project.doesQueryExist(withName: name) else { throw SQLError.queryAlreadyExists(fileName: name)