diff --git a/SiteGenerator/Sources/SiteGenerator/main.swift b/SiteGenerator/Sources/SiteGenerator/main.swift index 41bbdf8..a978011 100644 --- a/SiteGenerator/Sources/SiteGenerator/main.swift +++ b/SiteGenerator/Sources/SiteGenerator/main.swift @@ -5,6 +5,7 @@ // Created by Sami Samhuri on 2019-12-01. // +import Darwin import Foundation func main(sourcePath: String, targetPath: String) throws { @@ -18,9 +19,32 @@ func main(sourcePath: String, targetPath: String) throws { try generator.generate(targetURL: targetURL) } +guard CommandLine.arguments.count >= 3 else { + let name = CommandLine.arguments[0] + fputs("Usage: \(name) ", stderr) + exit(1) +} + let sourcePath = CommandLine.arguments[1] +var isDir: ObjCBool = false +let sourceExists = FileManager.default.fileExists(atPath: sourcePath, isDirectory: &isDir) +guard sourceExists, isDir.boolValue else { + fputs("error: Site path \(sourcePath) does not exist or is not a directory", stderr) + exit(2) +} + let targetPath = CommandLine.arguments[2] +let targetExists = FileManager.default.fileExists(atPath: targetPath) +guard !targetExists else { + print("error: Refusing to clobber existing target \(targetPath)") + exit(2) +} -#warning("TODO: validate args") - -try! main(sourcePath: sourcePath, targetPath: targetPath) +do { + try main(sourcePath: sourcePath, targetPath: targetPath) + exit(0) +} +catch { + fputs("error: \(error.localizedDescription)", stderr) + exit(-1) +}