Modern Apps git

xtool.git

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

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

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