diff --git a/sources/coordinate.swift b/sources/coordinate.swift index 01bd269..3b21c7f 100644 --- a/sources/coordinate.swift +++ b/sources/coordinate.swift @@ -1,6 +1,6 @@ import CoreLocation -func coordinate(from arguments: [String]) -> Result { +func coordinate(from arguments: [String]) -> Result { if arguments.count != 2 { return .failure("Incorrect number of arguments, expected 2 got \(arguments.count)") } diff --git a/sources/delete.swift b/sources/delete.swift new file mode 100644 index 0000000..02e6386 --- /dev/null +++ b/sources/delete.swift @@ -0,0 +1,5 @@ +import CoreLocation + +func delete(_: [String]) -> Result { + return .success(nil) +} diff --git a/sources/main.swift b/sources/main.swift index 5cf342d..fa2d0cf 100644 --- a/sources/main.swift +++ b/sources/main.swift @@ -10,6 +10,7 @@ guard let flag = arguments.popFirst() else { let commands = [ "-c": coordinate, + "-d": delete, "-q": findLocation, ] @@ -18,7 +19,7 @@ guard let command = commands[flag] else { } switch command(Array(arguments)) { - case .success(let coordinate) where coordinate.isValid: + case .success(let coordinate) where coordinate?.isValid != false: do { let bootedSimulators = try getBootedSimulators() let simulators = @@ -26,12 +27,16 @@ switch command(Array(arguments)) { ?? deviceName.map { try getSimulators(named: $0, from: bootedSimulators) } ?? bootedSimulators postNotification(for: coordinate, to: simulators.map { $0.udid.uuidString }) - print("Setting location to \(coordinate.latitude) \(coordinate.longitude)") + if let coordinate = coordinate { + print("Setting location to \(coordinate.latitude) \(coordinate.longitude)") + } else { + print("Clearing location") + } } catch let error as SimulatorFetchError { exitWithUsage(error: error.description) } case .success(let coordinate): - exitWithUsage(error: "Coordinate: \(coordinate) is invalid") + exitWithUsage(error: "Coordinate: \(String(describing: coordinate)) is invalid") case .failure(let error): exitWithUsage(error: error) } diff --git a/sources/notification.swift b/sources/notification.swift index c0a0176..233882f 100644 --- a/sources/notification.swift +++ b/sources/notification.swift @@ -3,13 +3,16 @@ import Foundation private let kNotificationName = "com.apple.iphonesimulator.simulateLocation" -func postNotification(for coordinate: CLLocationCoordinate2D, to simulators: [String]) { - let userInfo: [AnyHashable: Any] = [ - "simulateLocationLatitude": coordinate.latitude, - "simulateLocationLongitude": coordinate.longitude, +func postNotification(for coordinate: CLLocationCoordinate2D?, to simulators: [String]) { + var userInfo: [AnyHashable: Any] = [ "simulateLocationDevices": simulators, ] + if let coordinate = coordinate { + userInfo["simulateLocationLatitude"] = coordinate.latitude + userInfo["simulateLocationLongitude"] = coordinate.longitude + } + let notification = Notification(name: Notification.Name(rawValue: kNotificationName), object: nil, userInfo: userInfo) diff --git a/sources/query.swift b/sources/query.swift index ddd6ec0..4df7928 100644 --- a/sources/query.swift +++ b/sources/query.swift @@ -2,7 +2,7 @@ import CoreLocation import Foundation import MapKit -func findLocation(from arguments: [String]) -> Result { +func findLocation(from arguments: [String]) -> Result { if arguments.isEmpty { return .failure("No arguments passed for location search") } diff --git a/sources/stderr.swift b/sources/stderr.swift index e19d78d..6629aac 100644 --- a/sources/stderr.swift +++ b/sources/stderr.swift @@ -13,7 +13,7 @@ func exitWithUsage(error: String? = nil) -> Never { print(error, terminator: "\n\n", to: &stderrStream) } - print("Usage: set-simulator-location [-c 0 0|-q San Francisco] [-s Simulator Name|-u Simulator udid]", + print("Usage: set-simulator-location [-c 0 0|-q San Francisco|-d] [-s Simulator Name|-u Simulator udid]", to: &stderrStream) exit(EXIT_FAILURE) }