xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XToolSupport / DevicesCommand.swift
DevicesCommand.swift40 lines · 1.2 KiBRaw
| 1 | import Foundation |
| 2 | import XKit |
| 3 | import ArgumentParser |
| 4 | |
| 5 | extension ClientDevice.SearchMode: EnumerableFlag { |
| 6 | // public static func name(for value: ConnectionManager.SearchMode) -> NameSpecification { |
| 7 | // [.short, .long] |
| 8 | // } |
| 9 | } |
| 10 | |
| 11 | struct DevicesCommand: AsyncParsableCommand { |
| 12 | static let configuration = CommandConfiguration( |
| 13 | commandName: "devices", |
| 14 | abstract: "List devices" |
| 15 | ) |
| 16 | |
| 17 | @Flag(help: "Which devices to search for") var search: ClientDevice.SearchMode = .all |
| 18 | @Flag( |
| 19 | inversion: .prefixedNo, |
| 20 | help: "If no devices are found at first, wait until at least one is connected." |
| 21 | ) var wait = true |
| 22 | |
| 23 | func run() async throws { |
| 24 | var shouldPrint = true |
| 25 | var foundDevices: [ClientDevice] = [] |
| 26 | for await devices in try await ClientDevice.search(mode: search) { |
| 27 | if !devices.isEmpty { |
| 28 | foundDevices = devices |
| 29 | break |
| 30 | } |
| 31 | guard wait else { break } |
| 32 | if shouldPrint { |
| 33 | shouldPrint = false |
| 34 | print("Waiting for devices to be connected...") |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | print(foundDevices.map { "\($0.deviceName) [\($0.connectionType)]: \($0.udid)" }.joined(separator: "\n")) |
| 39 | } |
| 40 | } |