Filter for exact name

This commit is contained in:
Simon Kågedal Reimer 2019-05-30 15:36:02 +02:00
parent 5cb09956d8
commit a475c9e39a
6 changed files with 70 additions and 52 deletions

View File

@ -6,30 +6,6 @@ import Foundation
import Basic import Basic
import SPMUtility import SPMUtility
struct FilteringOptions {
enum Availability: String, StringEnumArgument {
case yes
case no
case all
public static var completion: ShellCompletion = .none
}
var availability: Availability = .yes
}
extension ArgumentBinder where Options == FilteringOptions {
func bind(to options: inout FilteringOptions, parser: ArgumentParser) {
bind(option: parser.add(
option: "--availability",
kind: FilteringOptions.Availability.self,
usage: "Only list available devices? yes|no|all, defaults to all"
), to: { options, availability in
options.availability = availability
})
}
}
struct CommandLineOptions { struct CommandLineOptions {
enum SubCommand { enum SubCommand {
case noCommand case noCommand
@ -77,13 +53,6 @@ struct CommandLineOptions {
} }
) )
binder.bind(
parser: parser,
to: { _, _ in
// print("Parsed subcommand: \(subcommand)")
}
)
for command in allCommands { for command in allCommands {
let subparser = parser.add( let subparser = parser.add(
subparser: command.name, subparser: command.name,

View File

@ -0,0 +1,61 @@
//
// Copyright © 2019 Simon Kågedal Reimer. See LICENSE.
//
import Foundation
import SPMUtility
struct FilteringOptions {
enum Availability: String, StringEnumArgument {
case yes
case no
case all
public static var completion: ShellCompletion = .none
}
var availability: Availability = .yes
var deviceName: String?
}
extension ArgumentBinder where Options == FilteringOptions {
func bind(to options: inout FilteringOptions, parser: ArgumentParser) {
bind(option: parser.add(
option: "--availability",
kind: FilteringOptions.Availability.self,
usage: "Only affect available devices? yes|no|all, defaults to all"
), 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
})
}
}
extension FilteringOptions.Availability {
func matches(_ availability: Bool) -> Bool {
switch (self, availability) {
case (.yes, true), (.no, false), (.all, _):
return true
case (.yes, false), (.no, true):
return false
}
}
}
extension Sequence where Element == Simctl.Device {
func filter(using filteringOptions: FilteringOptions) -> [Simctl.Device] {
return filter { device in
if let deviceName = filteringOptions.deviceName, deviceName != device.name {
return false
}
return filteringOptions.availability.matches(device.isAvailable)
}
}
}

View File

@ -7,7 +7,7 @@ import SPMUtility
class InstallCACommand: Command { class InstallCACommand: Command {
private struct Options { private struct Options {
var path: String? = nil var path: String?
} }
let name = "install-ca" let name = "install-ca"
let overview = "Install a Certificate Authority" let overview = "Install a Certificate Authority"
@ -41,5 +41,8 @@ class InstallCACommand: Command {
print(certificate) print(certificate)
print(filteringOptions) print(filteringOptions)
for device in try Simctl.flatListDevices().filter(using: filteringOptions) {
print(device.name)
}
} }
} }

View File

@ -61,22 +61,3 @@ class ListDevicesCommand: Command {
} }
} }
} }
extension FilteringOptions.Availability {
func matches(_ availability: Bool) -> Bool {
switch (self, availability) {
case (.yes, true), (.no, false), (.all, _):
return true
case (.yes, false), (.no, true):
return false
}
}
}
extension Sequence where Element == Simctl.Device {
func filter(using filteringOptions: FilteringOptions) -> [Simctl.Device] {
return filter { device in
filteringOptions.availability.matches(device.isAvailable)
}
}
}

View File

@ -26,6 +26,10 @@ struct Simctl {
var state: State var state: State
} }
static func flatListDevices() throws -> [Device] {
return try listDevices().devices.values.flatMap { $0 }
}
static func listDevices() throws -> DeviceList { static func listDevices() throws -> DeviceList {
return try parseDeviceList(readListDevices()) return try parseDeviceList(readListDevices())
} }

View File

@ -24,7 +24,7 @@ public class XcodeSimulatorTool {
) )
try run(with: options) try run(with: options)
} catch let error as CommandLineOptions.Error { } catch let error as CommandLineOptions.Error {
stderrStream.write("error: \(error.underlyingError.localizedDescription)\n\n") stderrStream.write("error: \(error.underlyingError)\n\n")
error.printUsage(on: stderrStream) error.printUsage(on: stderrStream)
return 1 return 1
} catch { } catch {