Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

xtool.git / Sources / XToolSupport / DevicesCommand.swift

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