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