Filter for exact name
This commit is contained in:
parent
5cb09956d8
commit
a475c9e39a
|
@ -6,30 +6,6 @@ import Foundation
|
|||
import Basic
|
||||
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 {
|
||||
enum SubCommand {
|
||||
case noCommand
|
||||
|
@ -77,13 +53,6 @@ struct CommandLineOptions {
|
|||
}
|
||||
)
|
||||
|
||||
binder.bind(
|
||||
parser: parser,
|
||||
to: { _, _ in
|
||||
// print("Parsed subcommand: \(subcommand)")
|
||||
}
|
||||
)
|
||||
|
||||
for command in allCommands {
|
||||
let subparser = parser.add(
|
||||
subparser: command.name,
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ import SPMUtility
|
|||
|
||||
class InstallCACommand: Command {
|
||||
private struct Options {
|
||||
var path: String? = nil
|
||||
var path: String?
|
||||
}
|
||||
let name = "install-ca"
|
||||
let overview = "Install a Certificate Authority"
|
||||
|
@ -41,5 +41,8 @@ class InstallCACommand: Command {
|
|||
print(certificate)
|
||||
|
||||
print(filteringOptions)
|
||||
for device in try Simctl.flatListDevices().filter(using: filteringOptions) {
|
||||
print(device.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,10 @@ struct Simctl {
|
|||
var state: State
|
||||
}
|
||||
|
||||
static func flatListDevices() throws -> [Device] {
|
||||
return try listDevices().devices.values.flatMap { $0 }
|
||||
}
|
||||
|
||||
static func listDevices() throws -> DeviceList {
|
||||
return try parseDeviceList(readListDevices())
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ public class XcodeSimulatorTool {
|
|||
)
|
||||
try run(with: options)
|
||||
} 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)
|
||||
return 1
|
||||
} catch {
|
||||
|
|
Loading…
Reference in New Issue