Add some logging, refactor InstallCACommand, create parent dirs

This commit is contained in:
Simon Kågedal Reimer 2019-06-07 19:19:41 +02:00
parent fecb2d096a
commit 9dd9986553
4 changed files with 84 additions and 33 deletions

View File

@ -6,7 +6,7 @@ import Foundation
import SPMUtility
class InstallCACommand: Command {
private struct Options {
struct Options {
var path: String?
var dryRun: Bool = false
}
@ -45,36 +45,11 @@ class InstallCACommand: Command {
}
func run(reporter: Reporter) throws {
let url = URL(fileURLWithPath: options.path!)
let certificate = try Certificate.load(from: url)
let sha1 = certificate.sha1
let certificateName = certificate.subjectSummary ?? "<unknown certificate>"
for device in try Simctl.flatListDevices().filter(using: filteringOptions) {
let trustStore = TrustStore(uuid: device.udid)
if !trustStore.exists && options.dryRun {
print("Would install '\(certificateName)' into '\(device.name)'.")
continue
}
let connection = try trustStore.open()
if try connection.hasCertificate(with: sha1) {
if options.dryRun {
print("Would skip installing '\(certificateName)' into '\(device.name)' it's already there.")
} else {
print("Not installing '\(certificateName)' into '\(device.name)' it's already there.")
}
continue
}
if options.dryRun {
print("Would install '\(certificateName)' into '\(device.name)'.")
} else {
print("Installing '\(certificateName)' into '\(device.name)'.")
try trustStore.open().addCertificate(certificate)
}
}
let runner = InstallCACommandRunner(
options: options,
filteringOptions: filteringOptions,
reporter: reporter
)
try runner.run()
}
}

View File

@ -0,0 +1,66 @@
//
// Copyright © 2019 Simon Kågedal Reimer. See LICENSE.
//
import Foundation
class InstallCACommandRunner {
private let reporter: Reporter
private let options: InstallCACommand.Options
private let filteringOptions: FilteringOptions
init(
options: InstallCACommand.Options,
filteringOptions: FilteringOptions,
reporter: Reporter) {
self.reporter = reporter
self.options = options
self.filteringOptions = filteringOptions
}
func run() throws {
let url = URL(fileURLWithPath: options.path!)
let certificate = try Certificate.load(from: url)
for device in try Simctl.flatListDevices().filter(using: filteringOptions) {
try self.install(certificate, in: device)
}
}
private func install(_ certificate: Certificate, in device: Simctl.Device) throws {
let sha1 = certificate.sha1
let certificateName = certificate.subjectSummary ?? "<unknown certificate>"
let trustStore = TrustStore(uuid: device.udid)
reporter.info("Trust store path: \(trustStore.path)")
if !trustStore.exists {
reporter.info("Trust store does not exist yet.")
if options.dryRun {
print("Would install '\(certificateName)' into '\(device.name)'.")
return
}
reporter.info("Creating directories.")
try trustStore.createParentDirectories()
}
let connection = try trustStore.open()
reporter.info("Opened trust store.")
if try connection.hasCertificate(with: sha1) {
if options.dryRun {
print("Would skip installing '\(certificateName)' into '\(device.name)' it's already there.")
} else {
print("Not installing '\(certificateName)' into '\(device.name)' it's already there.")
}
return
}
if options.dryRun {
print("Would install '\(certificateName)' into '\(device.name)'.")
} else {
print("Installing '\(certificateName)' into '\(device.name)'.")
try trustStore.open().addCertificate(certificate)
}
}
}

View File

@ -5,7 +5,7 @@
import Foundation
protocol Reporter {
func info(_ string: String)
}
class DefaultReporter: Reporter {
@ -14,4 +14,10 @@ class DefaultReporter: Reporter {
init(verbosity: Verbosity) {
self.verbosity = verbosity
}
func info(_ string: String) {
guard verbosity == .loud else { return }
print("INFO: ", terminator: "")
print(string)
}
}

View File

@ -20,6 +20,10 @@ struct TrustStore {
return localFileSystem.exists(path)
}
func createParentDirectories() throws {
try localFileSystem.createDirectory(path.parentDirectory, recursive: true)
}
func open() throws -> Connection {
return try Connection(openingPath: path.pathString)
}