More complex argument parsing to support both inputs

This commit is contained in:
Tamás Lustyik committed 2018-05-13 16:28:05 +02:00
1 parent a418d8fe64
commit 886e4b2c81
1 file changed
+68 -24
+68 -24
View File
@@ -10,37 +10,81 @@ import Foundation
var verbose = false;
func printUsage() {
let command = URL(fileURLWithPath: CommandLine.arguments.first!).lastPathComponent;
print("Generate appcast from a directory of Sparkle updates\n",
"Usage: \(command) < -f private key path | -k keychain_path -n key_name > <directory with update archives>\n",
" e.g. \(command) -f dsa_priv.pem archives/\n",
" Appcast files and deltas will be written to the archives directory.\n",
" Note that pkg-based updates are not supported.\n"
)
}
func main() {
let args = CommandLine.arguments;
if args.count < 3 {
let command = URL(fileURLWithPath: args[0]).lastPathComponent;
print("Generate appcast from a directory of Sparkle updates\nUsage: \(command) <private key path> <directory with update archives>\n",
" e.g. \(command) dsa_priv.pem archives/\n",
" Appcast files and deltas will be written to the archives directory.\n",
" Note that pkg-based updates are not supported.\n"
)
exit(1);
}
let privateKeyURL = URL(fileURLWithPath: args[1]);
let archivesSourceDir = URL(fileURLWithPath: args[2], isDirectory:true);
let firstOption = args[1]
let privateKey: SecKey
if firstOption == "-f" && args.count == 4 {
// private key specified by filename
let privateKeyURL = URL(fileURLWithPath: args[2])
do {
privateKey = try loadPrivateKey(at: privateKeyURL)
} catch {
print("Unable to load DSA private key from", privateKeyURL.path, "\n", error)
exit(1)
}
}
else if (firstOption == "-n" || firstOption == "-k") && args.count == 6 {
// private key specified by keychain + key name
let keyName: String
let keychainURL: URL
if firstOption == "-n" {
if args[3] != "-k" {
printUsage()
exit(1)
}
keyName = args[2]
keychainURL = URL(fileURLWithPath: args[4])
}
else {
if args[3] != "-n" {
printUsage()
exit(1)
}
keyName = args[4]
keychainURL = URL(fileURLWithPath: args[2])
}
do {
privateKey = try loadPrivateKey(named: keyName, fromKeychainAt: keychainURL)
} catch {
print("Unable to load DSA private key '\(keyName)' from keychain at", keychainURL.path, "\n", error)
exit(1)
}
}
else {
printUsage()
exit(1)
}
let archivesSourceDir = URL(fileURLWithPath: args.last!, isDirectory: true)
do {
let privateKey = try loadPrivateKey(at: privateKeyURL);
do {
let allUpdates = try makeAppcast(archivesSourceDir: archivesSourceDir, privateKey: privateKey, verbose:verbose);
for (appcastFile, updates) in allUpdates {
let appcastDestPath = URL(fileURLWithPath: appcastFile, relativeTo: archivesSourceDir);
try writeAppcast(appcastDestPath:appcastDestPath, updates:updates);
print("Written", appcastDestPath.path, "based on", updates.count, "updates");
}
} catch {
print("Error generating appcast from directory", archivesSourceDir.path, "\n", error);
exit(1);
let allUpdates = try makeAppcast(archivesSourceDir: archivesSourceDir, privateKey: privateKey, verbose:verbose);
for (appcastFile, updates) in allUpdates {
let appcastDestPath = URL(fileURLWithPath: appcastFile, relativeTo: archivesSourceDir);
try writeAppcast(appcastDestPath:appcastDestPath, updates:updates);
print("Written", appcastDestPath.path, "based on", updates.count, "updates");
}
} catch {
print("Unable to load DSA private key from", privateKeyURL.path, "\n", error);
print("Error generating appcast from directory", archivesSourceDir.path, "\n", error);
exit(1);
}
}