Merge pull request #1571 from D0miH/feature/1569-download-url-prefix-option

Prefix for download url can be provided to generate_appcast
This commit is contained in:
Kornel authored and GitHub committed 2020-04-07 00:17:05 +01:00
commit 2db0d8cb37
2 files changed
+155 -56

No files matched your search

+6 -2
View File
@@ -46,6 +46,7 @@ class ArchiveItem: CustomStringConvertible {
var dsaSignature: String?;
var edSignature: String?;
var downloadUrlPrefix: URL?;
init(version: String, shortVersion: String?, feedURL: URL?, minimumSystemVersion: String?, publicEdKey: String?, supportsDSA: Bool, appPath: URL, archivePath: URL) throws {
self.version = version;
@@ -125,8 +126,11 @@ class ArchiveItem: CustomStringConvertible {
guard let escapedFilename = self.archivePath.lastPathComponent.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
return nil;
}
if let relative = self.feedURL {
return URL(string: escapedFilename, relativeTo: relative)
if let downloadUrlPrefix = self.downloadUrlPrefix {
// if a download url prefix was given use this one
return URL(string: escapedFilename, relativeTo: downloadUrlPrefix)
} else if let relativeFeedUrl = self.feedURL {
return URL(string: escapedFilename, relativeTo: relativeFeedUrl)
}
return URL(string: escapedFilename)
}
+149 -54
View File
@@ -25,6 +25,19 @@ func printUsage() {
)
}
func printHelp() {
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 static url that will be used as prefix for the url from where updates will be downloaded\n"
)
}
func loadPrivateKeys(_ privateDSAKey: SecKey?, _ privateEdString: String?) -> PrivateKeys {
var privateEdKey: Data?;
var publicEdKey: Data?;
@@ -62,6 +75,129 @@ func loadPrivateKeys(_ privateDSAKey: SecKey?, _ privateEdString: String?) -> Pr
return PrivateKeys(privateDSAKey: privateDSAKey, privateEdKey: privateEdKey, publicEdKey: publicEdKey);
}
/**
* Parses all possible command line options and returns the values in a tuple.
*/
func parseCommandLineOptions(argumentList: [String]) -> (privateDSAKey: SecKey?, privateEdString: String?, downloadUrlPrefix: URL?, archivesSourceDir: URL) {
// if the option `-h` is in the argument list print the help dialog
if argumentList.contains("-h") {
printHelp()
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
arguments.removeFirst()
// define the variables for the possible argument values
var privateDSAKey: SecKey? = nil
var privateEdString: String? = nil
var downloadUrlPrefix: URL? = nil
var archivesSourceDir: URL
// check if the private dsa key option is present
if let privateDSAKeyOptionIndex = arguments.firstIndex(of: "-f") {
// check that when accessing the value of the option we don't get out of bounds
if privateDSAKeyOptionIndex + 1 >= arguments.count {
print("Too few arguments were given")
exit(1)
}
// get the private DSA key
let privateKeyUrl = URL(fileURLWithPath: arguments[privateDSAKeyOptionIndex + 1])
do {
privateDSAKey = try loadPrivateDSAKey(at: privateKeyUrl)
} catch {
print("Unable to load DSA private key from", privateKeyUrl.path, "\n", error)
exit(1)
}
// remove the already parsed arguments
arguments.remove(at: privateDSAKeyOptionIndex)
arguments.remove(at: privateDSAKeyOptionIndex + 1)
}
// check if the private dsa sould be loaded using the keyname and the name of the keychain
if let keyNameOptionIndex = arguments.firstIndex(of: "-n"), let keychainNameOptionIndex = arguments.firstIndex(of: "-k") {
// check that when accessing one of the values of the options we don't get out of bounds
if keyNameOptionIndex + 1 >= arguments.count || keychainNameOptionIndex + 1 >= arguments.count {
print("Too few arguments were given")
exit(1)
}
// get the keyname and the keychain url to load the private DSA key
let keyName: String = arguments[keyNameOptionIndex + 1]
let keychainUrl: URL = URL(fileURLWithPath: arguments[keychainNameOptionIndex + 1])
do {
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)
}
// remove the already parsed arguments
arguments.remove(at: keyNameOptionIndex + 1)
arguments.remove(at: keyNameOptionIndex)
arguments.remove(at: arguments.firstIndex(of: "-k")! + 1)
arguments.remove(at: keychainNameOptionIndex)
}
// check if the private EdDSA key string was given as an argument
if let privateEdDSAKeyOptionIndex = arguments.firstIndex(of: "-s") {
// check that when accessing the value of the option we don't get out of bounds
if privateEdDSAKeyOptionIndex + 1 >= arguments.count {
print("Too few arguments were given")
exit(1)
}
// get the private EdDSA key string
privateEdString = arguments[privateEdDSAKeyOptionIndex + 1]
// remove the already parsed argument
arguments.remove(at: privateEdDSAKeyOptionIndex + 1)
arguments.remove(at: privateEdDSAKeyOptionIndex)
}
// check if a prefix for the download url of the archives was given
if let downloadUrlPrefixOptionIndex = arguments.firstIndex(of: "--download-url-prefix") {
// check that when accessing the value of the option we don't get out of bounds
if downloadUrlPrefixOptionIndex + 1 >= arguments.count {
print("Too few arguments were given")
exit(1)
}
// get the download url prefix
downloadUrlPrefix = URL(string: arguments[downloadUrlPrefixOptionIndex + 1])
// remove the parsed argument
arguments.remove(at: downloadUrlPrefixOptionIndex + 1)
arguments.remove(at: downloadUrlPrefixOptionIndex)
}
// now that all command line options have been removed from the arguments array
// there should only be the path to the private DSA key (if provided) path to the archives dir left
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])
do {
privateDSAKey = try loadPrivateDSAKey(at: privateKeyUrl)
} catch {
print("Unable to load DSA private key from", privateKeyUrl.path, "\n", error)
exit(1)
}
// remove the parsed path to the DSA key
arguments.removeFirst()
}
// now only the archives source dir is left
archivesSourceDir = URL(fileURLWithPath: arguments[0], isDirectory: true)
return (privateDSAKey, privateEdString, downloadUrlPrefix, archivesSourceDir)
}
func main() {
let args = CommandLine.arguments;
if args.count < 2 {
@@ -71,66 +207,25 @@ func main() {
var privateDSAKey: SecKey? = nil;
var privateEdString: String? = nil;
// this was typical usage for DSA keys
if args.count == 3 || (args.count == 4 && args[1] == "-f") {
// private key specified by filename
let privateKeyURL = URL(fileURLWithPath: args.count == 3 ? args[1] : args[2])
do {
privateDSAKey = try loadPrivateDSAKey(at: privateKeyURL)
} catch {
print("Unable to load DSA private key from", privateKeyURL.path, "\n", error)
exit(1)
}
}
// this is legacy for DSA keychain; probably very rarely used
else if args.count == 6 && (args[1] == "-n" || args[1] == "-k") {
// private key specified by keychain + key name
let keyName: String
let keychainURL: URL
if args[1] == "-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 {
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)
}
}
// private + public EdDSA keys are provided via command line argument
else if args.count == 4 && args[1] == "-s" {
privateEdString = args[2]
}
else if args.count != 2 {
printUsage()
exit(1)
}
var downloadUrlPrefix: URL? = nil;
var archivesSourceDir: URL
(privateDSAKey, privateEdString, downloadUrlPrefix, archivesSourceDir) = parseCommandLineOptions(argumentList: args)
let archivesSourceDir = URL(fileURLWithPath: args.last!, isDirectory: true)
let keys = loadPrivateKeys(privateDSAKey, privateEdString)
do {
let allUpdates = try makeAppcast(archivesSourceDir: archivesSourceDir, keys: keys, verbose:verbose);
// if a download url prefix was provided set it for each archive item
if downloadUrlPrefix != nil {
for (_, archiveItems) in allUpdates {
for archiveItem in archiveItems {
archiveItem.downloadUrlPrefix = downloadUrlPrefix
}
}
}
for (appcastFile, updates) in allUpdates {
let appcastDestPath = URL(fileURLWithPath: appcastFile, relativeTo: archivesSourceDir);
try writeAppcast(appcastDestPath:appcastDestPath, updates:updates);