mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-03-25 09:05:47 +00:00
Validate command line args
This commit is contained in:
parent
4a03060c8c
commit
9173a09d88
1 changed files with 27 additions and 3 deletions
|
|
@ -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) <site dir> <target dir>", 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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue