Modern Apps git

xtool.git

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

xtool.git / Sources / XToolSupport / ConnectionOptions.swift

ConnectionOptions.swift42 lines · 1.3 KiBRaw
1import Foundation
2import XKit
3import ArgumentParser
4
5struct ConnectionOptions: ParsableArguments {
6 struct WithoutSearchMode: ParsableArguments {
7 @Option(name: .shortAndLong) var udid: String?
8 }
9
10 @OptionGroup var base: WithoutSearchMode
11 @Flag var search: ClientDevice.SearchMode = .all
12
13 func client() async throws -> ClientDevice {
14 try await base.client(searchMode: search)
15 }
16}
17
18extension ConnectionOptions.WithoutSearchMode {
19 func client(searchMode search: ClientDevice.SearchMode) async throws -> ClientDevice {
20 print("Waiting for device to be connected...")
21
22 let stream = try await ClientDevice.search(mode: search)
23 for await devices in stream {
24 guard !devices.isEmpty else { continue }
25 if let udid {
26 guard let device = devices.first(where: { $0.udid == udid }) else {
27 continue
28 }
29 return device
30 } else {
31 return try await Console.choose(
32 from: devices,
33 onNoElement: { throw Console.Error("Device not found") },
34 multiPrompt: "Choose device",
35 formatter: { "\($0.deviceName) (\($0.connectionType), udid: \($0.udid))" }
36 )
37 }
38 }
39
40 throw CancellationError()
41 }
42}