Use a struct to simplify command line argument parsing

Changed parseCommandLineOptions so that it returns a struct that contains possible argument values, rather than a tuple.

Also unified the “help” and “usage” messages into a single message.
This commit is contained in:
Brad Andalman committed 2020-09-02 15:57:05 -07:00
1 parent 7c84c537a5
commit 4fec1b7301
1 file changed
+85 -84
+85 -84
View File
@@ -10,34 +10,45 @@ import Foundation
var verbose = false
func printUsage() {
let command = URL(fileURLWithPath: CommandLine.arguments.first!).lastPathComponent
print("Generate appcast from a directory of Sparkle update archives\n",
"Usage:\n",
" \(command) <directory with update files>\n",
" e.g. \(command) ./my-app-release-zipfiles/\n",
"\nOR for old apps that have a DSA keys (deprecated):\n",
" \(command) <private DSA key path> <directory with update files>\n",
" e.g. \(command) dsa_priv.pem archives/\n",
"\n",
"Appcast files and deltas will be written to the archives directory.\n",
"Note that pkg-based updates are not supported.\n"
)
// Enum that contains keys for command-line arguments
struct CommandLineArguments {
var privateDSAKey : SecKey?
var privateEdString : String?
var downloadURLPrefix : URL?
var releaseNotesURLPrefix: URL?
var outputPathURL: URL?
var archivesSourceDir: URL?
}
func printHelp() {
func printUsage() {
let command = URL(fileURLWithPath: CommandLine.arguments.first!).lastPathComponent
print(
"Usage: \(command) [OPTIONS] [ARCHIVES_FOLDER]\n",
"Options:\n",
"\t-f: provide the path to the private DSA key\n",
"\t-n: provide the name of the private DSA key. This option has to be used together with `-k`\n",
"\t-k: provide the name of the keychain. This option has to be used together with `-n`\n",
"\t-s: provide the path to the private EdDSA key\n",
"\t--download-url-prefix: provide a prefix used to construct URLs for update downloads\n",
"\t--release-notes-url-prefix: provide a prefix used to construct URLs for release notes\n",
"\t-o: provide a filename for the generated appcast (allowed when only one will be created)\n"
)
let usage = """
Generate appcast from a directory of Sparkle update archives
Usage: \(command) [OPTIONS] [ARCHIVES_FOLDER]
-h: prints this message
-f: provide the path to the private DSA key
-n: provide the name of the private DSA key. This option must be used with `-k`
-k: provide the name of the keychain. This option must be used with `-n`
-s: provide the path to the private EdDSA key
-o: provide a filename for the generated appcast (allowed when only one will be created)
--download-url-prefix: provide a prefix used to construct URLs for update downloads
--release-notes-url-prefix: provide a prefix used to construct URLs for release notes
Examples:
\(command) ./my-app-release-zipfiles/
\(command) -o appcast-name.xml ./my-app-release-zipfiles/
\(command) dsa_priv.pem ./my-app-release-zipfiles/ [DEPRECATED]
Appcast files and deltas will be written to the archives directory.
Note that pkg-based updates are not supported.
"""
print(usage)
}
func loadPrivateKeys(_ privateDSAKey: SecKey?, _ privateEdString: String?) -> PrivateKeys {
@@ -78,27 +89,24 @@ func loadPrivateKeys(_ privateDSAKey: SecKey?, _ privateEdString: String?) -> Pr
}
/**
* Parses all possible command line options and returns the values in a tuple.
* Parses all possible command line options and returns a dictionary that contains them
*/
func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?, privateEdString: String?, downloadUrlPrefix: URL?, releaseNotesURLPrefix: URL?, outputFilename: String?, archivesSourceDir: URL) {
// if the option `-h` is in the argument list print the help dialog
if argumentList.contains("-h") {
printHelp()
func parseCommandLineOptions() -> CommandLineArguments {
var arguments = CommandLine.arguments
// If there are fewer than two arguments (the name of the application +
// the required archive directory), or if `-h` is in the argument list,
// then show the usage message
if arguments.count < 2 || arguments.contains("-h") {
printUsage()
exit(1)
}
// make a mutable copy of the argument list
var arguments = argumentList
// remove the first element since this is the path to executable which we don't need
// Remove the first element since this is the path to executable which we don't need
arguments.removeFirst()
// define the variables for the possible argument values
var privateDSAKey: SecKey?
var privateEdString: String?
var downloadUrlPrefix: URL?
var releaseNotesURLPrefix: URL?
var outputFilename: String?
var archivesSourceDir: URL
// Create the struct that will hold the parsed args
var commandLineArguments = CommandLineArguments()
// check if the private dsa key option is present
if let privateDSAKeyOptionIndex = arguments.firstIndex(of: "-f") {
@@ -111,7 +119,7 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?,
// get the private DSA key
let privateKeyUrl = URL(fileURLWithPath: arguments[privateDSAKeyOptionIndex + 1])
do {
privateDSAKey = try loadPrivateDSAKey(at: privateKeyUrl)
commandLineArguments.privateDSAKey = try loadPrivateDSAKey(at: privateKeyUrl)
} catch {
print("Unable to load DSA private key from", privateKeyUrl.path, "\n", error)
exit(1)
@@ -134,7 +142,7 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?,
let keyName: String = arguments[keyNameOptionIndex + 1]
let keychainUrl: URL = URL(fileURLWithPath: arguments[keychainNameOptionIndex + 1])
do {
privateDSAKey = try loadPrivateDSAKey(named: keyName, fromKeychainAt: keychainUrl)
commandLineArguments.privateDSAKey = try loadPrivateDSAKey(named: keyName, fromKeychainAt: keychainUrl)
} catch {
print("Unable to load DSA private key '\(keyName)' from keychain at", keychainUrl.path, "\n", error)
exit(1)
@@ -156,7 +164,7 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?,
}
// get the private EdDSA key string
privateEdString = arguments[privateEdDSAKeyOptionIndex + 1]
commandLineArguments.privateEdString = arguments[privateEdDSAKeyOptionIndex + 1]
// remove the already parsed argument
arguments.remove(at: privateEdDSAKeyOptionIndex + 1)
@@ -172,7 +180,7 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?,
}
// get the download url prefix
downloadUrlPrefix = URL(string: arguments[downloadUrlPrefixOptionIndex + 1])
commandLineArguments.downloadURLPrefix = URL(string: arguments[downloadUrlPrefixOptionIndex + 1])
// remove the parsed argument
arguments.remove(at: downloadUrlPrefixOptionIndex + 1)
@@ -187,7 +195,7 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?,
}
// Get the URL prefix for the release notes
releaseNotesURLPrefix = URL(string: arguments[releaseNotesURLPrefixOptionIndex + 1])
commandLineArguments.releaseNotesURLPrefix = URL(string: arguments[releaseNotesURLPrefixOptionIndex + 1])
// Remove the parsed argument
arguments.remove(at: releaseNotesURLPrefixOptionIndex + 1)
@@ -203,7 +211,7 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?,
}
// Get the URL prefix for the release notes
outputFilename = arguments[outputFilenameOptionIndex + 1]
commandLineArguments.outputPathURL = URL(fileURLWithPath: arguments[outputFilenameOptionIndex + 1])
// Remove the parsed argument
arguments.remove(at: outputFilenameOptionIndex + 1)
@@ -215,11 +223,11 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?,
if arguments.count == 2 {
// if there are two arguments left they are the private DSA key and the path to the archives directory (in this order)
// first get the private DSA key
let privateKeyUrl = URL(fileURLWithPath: arguments[0])
let privateKeyURL = URL(fileURLWithPath: arguments[0])
do {
privateDSAKey = try loadPrivateDSAKey(at: privateKeyUrl)
commandLineArguments.privateDSAKey = try loadPrivateDSAKey(at: privateKeyURL)
} catch {
print("Unable to load DSA private key from", privateKeyUrl.path, "\n", error)
print("Unable to load DSA private key from", privateKeyURL.path, "\n", error)
exit(1)
}
@@ -228,40 +236,38 @@ func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?,
}
// now only the archives source dir is left
archivesSourceDir = URL(fileURLWithPath: arguments[0], isDirectory: true)
if let archivesSourceDir = arguments.first {
commandLineArguments.archivesSourceDir = URL(fileURLWithPath: archivesSourceDir, isDirectory: true)
} else {
print("Archive folder must be specified")
exit(1);
}
return (privateDSAKey, privateEdString, downloadUrlPrefix, releaseNotesURLPrefix, outputFilename, archivesSourceDir)
return commandLineArguments
}
func main() {
let args = CommandLine.arguments
if args.count < 2 {
printUsage()
exit(1)
}
var privateDSAKey: SecKey?
var privateEdString: String?
var downloadUrlPrefix: URL?
var releaseNotesURLPrefix: URL?
var outputFilename: String?
var archivesSourceDir: URL
(privateDSAKey, privateEdString, downloadUrlPrefix, releaseNotesURLPrefix, outputFilename, archivesSourceDir) = parseCommandLineOptions(argumentList: args)
let keys = loadPrivateKeys(privateDSAKey, privateEdString)
// Parse the command line arguments
let args = parseCommandLineOptions()
// If parsing the command line options was successful, then
// the archivesSourceDir must exist
let archivesSourceDir = args.archivesSourceDir!
// Extract the keys
let keys = loadPrivateKeys(args.privateDSAKey, args.privateEdString)
do {
let allUpdates = try makeAppcast(archivesSourceDir: archivesSourceDir, keys: keys, verbose: verbose)
// If a URL prefix was provided, set on the archive items
if downloadUrlPrefix != nil || releaseNotesURLPrefix != nil {
if args.downloadURLPrefix != nil || args.releaseNotesURLPrefix != nil {
for (_, archiveItems) in allUpdates {
for archiveItem in archiveItems {
if downloadUrlPrefix != nil {
archiveItem.downloadUrlPrefix = downloadUrlPrefix
if let downloadURLPrefix = args.downloadURLPrefix {
archiveItem.downloadUrlPrefix = downloadURLPrefix
}
if releaseNotesURLPrefix != nil {
if let releaseNotesURLPrefix = args.releaseNotesURLPrefix {
archiveItem.releaseNotesURLPrefix = releaseNotesURLPrefix
}
}
@@ -270,22 +276,17 @@ func main() {
// If a (single) output filename was specified on the command-line, but more than one
// appcast file was found in the archives, then it's an error.
if let filename = outputFilename,
if let outputPathURL = args.outputPathURL,
allUpdates.count > 1 {
print("Cannot write to \(filename): multiple appcasts found")
print("Cannot write to \(outputPathURL.path): multiple appcasts found")
exit(1);
}
for (appcastFile, updates) in allUpdates {
// Determine the appcast's destination
var appcastDestPath : URL
if let filename = outputFilename {
// An output filename was specified, use it
appcastDestPath = URL(fileURLWithPath: filename)
} else {
// Use the name of the appcast file found in the archive
appcastDestPath = URL(fileURLWithPath: appcastFile, relativeTo: archivesSourceDir)
}
// If an output filename was specified, use it.
// Otherwise, use the name of the appcast file found in the archive.
let appcastDestPath = args.outputPathURL ?? URL(fileURLWithPath: appcastFile,
relativeTo: archivesSourceDir)
// Write the appcast
try writeAppcast(appcastDestPath: appcastDestPath, updates: updates)