Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
appstore-distribution ▾

xtool.git / Sources / XToolSupport / DevicesCommand.swift

DevicesCommand.swift40 lines · 1.2 KiBRaw
1import Foundation
2import XKit
3import ArgumentParser
4
5extension ClientDevice.SearchMode: EnumerableFlag {
6// public static func name(for value: ConnectionManager.SearchMode) -> NameSpecification {
7// [.short, .long]
8// }
9}
10
11struct 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}