Add skeleton remove-ca command

This commit is contained in:
Simon Kågedal Reimer 2019-05-30 16:52:53 +02:00
parent a475c9e39a
commit ef3c94f6bc
4 changed files with 68 additions and 6 deletions

View File

@ -7,6 +7,5 @@ A description of this package.
* `install-ca` command
* `remove-ca` command
* Filter for os types
* Find out why the argument parsing errors are not printed correctly
* `--dry-run` option
* `remove` command that removes simulators

View File

@ -31,7 +31,11 @@ struct CommandLineOptions {
private let parser: ArgumentParser
private(set) var subCommand: SubCommand
private static let allCommands: [Command] = [ListDevicesCommand(), InstallCACommand()]
private static let allCommands: [Command] = [
ListDevicesCommand(),
InstallCACommand(),
RemoveCACommand()
]
static func parse(commandName: String, arguments: [String]) throws -> CommandLineOptions {
let parser = ArgumentParser(

View File

@ -24,16 +24,16 @@ extension ArgumentBinder where Options == FilteringOptions {
option: "--availability",
kind: FilteringOptions.Availability.self,
usage: "Only affect available devices? yes|no|all, defaults to all"
), to: { options, availability in
options.availability = availability
), to: { options, availability in
options.availability = availability
})
bind(option: parser.add(
option: "--device-name",
kind: String.self,
usage: "Only affect devices with an exact name"
), to: { options, name in
options.deviceName = name
), to: { options, name in
options.deviceName = name
})
}
}

View File

@ -0,0 +1,59 @@
//
// Copyright © 2019 Simon Kågedal Reimer. See LICENSE.
//
import Foundation
import SPMUtility
class RemoveCACommand: Command {
private struct Options {
var dryRun: Bool = false
}
let name = "remove-ca"
let overview = "Remove all Certificate Authorities from specified devices"
private let binder = ArgumentBinder<Options>()
private var options = Options()
private let filteringBinder = ArgumentBinder<FilteringOptions>()
private var filteringOptions = FilteringOptions()
func addOptions(to parser: ArgumentParser) {
binder.bind(option: parser.add(
option: "--dry-run",
kind: Bool.self,
usage: "Don't actually remove any CA:s"
), to: { options, dryRun in
options.dryRun = dryRun
})
filteringBinder.bind(to: &filteringOptions, parser: parser)
}
func fillParseResult(_ parseResult: ArgumentParser.Result) throws {
try binder.fill(parseResult: parseResult, into: &options)
try filteringBinder.fill(parseResult: parseResult, into: &filteringOptions)
}
func run() throws {
print(filteringOptions)
for device in try Simctl.flatListDevices().filter(using: filteringOptions) {
let trustStore = TrustStore(uuid: device.udid)
if trustStore.exists {
try removeCertificates(in: trustStore, deviceName: device.name, dry: options.dryRun)
}
}
}
private func removeCertificates(in trustStore: TrustStore, deviceName: String, dry: Bool) throws {
for row in try trustStore.open().rows() {
let certificate = try row.validatedCertificate()
let name = certificate.subjectSummary ?? "<unknown certificate>"
if dry {
print(" - Would remove certificate \(name) from \(deviceName)")
} else {
print(" - REMOVING certificate \(name) from \(deviceName)")
}
}
}
}