Add some logging, refactor InstallCACommand, create parent dirs
This commit is contained in:
parent
fecb2d096a
commit
9dd9986553
|
@ -6,7 +6,7 @@ import Foundation
|
||||||
import SPMUtility
|
import SPMUtility
|
||||||
|
|
||||||
class InstallCACommand: Command {
|
class InstallCACommand: Command {
|
||||||
private struct Options {
|
struct Options {
|
||||||
var path: String?
|
var path: String?
|
||||||
var dryRun: Bool = false
|
var dryRun: Bool = false
|
||||||
}
|
}
|
||||||
|
@ -45,36 +45,11 @@ class InstallCACommand: Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(reporter: Reporter) throws {
|
func run(reporter: Reporter) throws {
|
||||||
let url = URL(fileURLWithPath: options.path!)
|
let runner = InstallCACommandRunner(
|
||||||
let certificate = try Certificate.load(from: url)
|
options: options,
|
||||||
let sha1 = certificate.sha1
|
filteringOptions: filteringOptions,
|
||||||
|
reporter: reporter
|
||||||
let certificateName = certificate.subjectSummary ?? "<unknown certificate>"
|
)
|
||||||
|
try runner.run()
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,7 +5,7 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
protocol Reporter {
|
protocol Reporter {
|
||||||
|
func info(_ string: String)
|
||||||
}
|
}
|
||||||
|
|
||||||
class DefaultReporter: Reporter {
|
class DefaultReporter: Reporter {
|
||||||
|
@ -14,4 +14,10 @@ class DefaultReporter: Reporter {
|
||||||
init(verbosity: Verbosity) {
|
init(verbosity: Verbosity) {
|
||||||
self.verbosity = verbosity
|
self.verbosity = verbosity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func info(_ string: String) {
|
||||||
|
guard verbosity == .loud else { return }
|
||||||
|
print("INFO: ", terminator: "")
|
||||||
|
print(string)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,10 @@ struct TrustStore {
|
||||||
return localFileSystem.exists(path)
|
return localFileSystem.exists(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createParentDirectories() throws {
|
||||||
|
try localFileSystem.createDirectory(path.parentDirectory, recursive: true)
|
||||||
|
}
|
||||||
|
|
||||||
func open() throws -> Connection {
|
func open() throws -> Connection {
|
||||||
return try Connection(openingPath: path.pathString)
|
return try Connection(openingPath: path.pathString)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue