Modern Apps git

xtool.git

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

xtool.git / Sources / XToolSupport / DSCommands / DSDevicesCommand.swift

DSDevicesCommand.swift70 lines · 1.9 KiBRaw
1import XUtils
2import Foundation
3import ArgumentParser
4import XKit
5import DeveloperAPI
6
7struct DSDevicesCommand: AsyncParsableCommand {
8 static let configuration = CommandConfiguration(
9 commandName: "devices",
10 abstract: "Interact with devices",
11 subcommands: [
12 DSDevicesListCommand.self,
13 ],
14 defaultSubcommand: DSDevicesListCommand.self
15 )
16}
17
18struct DSDevicesListCommand: AsyncParsableCommand {
19 static let configuration = CommandConfiguration(
20 commandName: "list",
21 abstract: "List devices"
22 )
23
24 func run() async throws {
25 let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
26
27 let devices = try await DeveloperAPIPages {
28 try await client.devicesGetCollection().ok.body.json
29 } next: {
30 $0.links.next
31 }
32 .map(\.data)
33 .reduce(into: [], +=)
34
35 for device in devices {
36 d.out.print("- id: \(device.id)")
37 guard let attributes = device.attributes else {
38 continue
39 }
40
41 if let name = attributes.name {
42 d.out.print(" name: \(name)")
43 }
44
45 if let platform = attributes.platform {
46 d.out.print(" platform: \(platform.rawValue)")
47 }
48
49 if let udid = attributes.udid {
50 d.out.print(" udid: \(udid)")
51 }
52
53 if let deviceClass = attributes.deviceClass {
54 d.out.print(" device class: \(deviceClass.rawValue)")
55 }
56
57 if let status = attributes.status {
58 d.out.print(" status: \(status.rawValue)")
59 }
60
61 if let model = attributes.model {
62 d.out.print(" model: \(model)")
63 }
64
65 if let addedDate = attributes.addedDate {
66 d.out.print(" added date: \(addedDate.formatted(.dateTime))")
67 }
68 }
69 }
70}