Modern Apps git

xtool.git

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

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

DSProfilesCommand.swift101 lines · 3.4 KiBRaw
1import XUtils
2import Foundation
3import ArgumentParser
4import XKit
5import DeveloperAPI
6
7struct DSProfilesCommand: AsyncParsableCommand {
8 static let configuration = CommandConfiguration(
9 commandName: "profiles",
10 abstract: "Interact with profiles",
11 subcommands: [
12 DSProfilesListCommand.self,
13 ],
14 defaultSubcommand: DSProfilesListCommand.self
15 )
16}
17
18struct DSProfilesListCommand: AsyncParsableCommand {
19 static let configuration = CommandConfiguration(
20 commandName: "list",
21 abstract: "List profiles"
22 )
23
24 private static let encoder = JSONEncoder()
25
26 // swiftlint:disable:next cyclomatic_complexity
27 func run() async throws {
28 let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
29
30 let profiles = try await DeveloperAPIPages {
31 try await client.profilesGetCollection().ok.body.json
32 } next: {
33 $0.links.next
34 }
35 .map(\.data)
36 .reduce(into: [], +=)
37
38 for profile in profiles {
39 d.out.print("- id: \(profile.id)")
40 guard let attributes = profile.attributes else {
41 continue
42 }
43
44 if let name = attributes.name {
45 d.out.print(" name: \(name)")
46 }
47
48 if let platform = attributes.platform {
49 d.out.print(" platform: \(platform.rawValue)")
50 }
51
52 if let profileType = attributes.profileType {
53 d.out.print(" profile type: \(profileType.rawValue)")
54 }
55
56 if let profileState = attributes.profileState {
57 d.out.print(" profile state: \(profileState.rawValue)")
58 }
59
60 if let uuid = attributes.uuid {
61 d.out.print(" uuid: \(uuid)")
62 }
63
64 if let createdDate = attributes.createdDate {
65 d.out.print(" created date: \(createdDate.formatted(.dateTime))")
66 }
67
68 if let expirationDate = attributes.expirationDate {
69 d.out.print(" expiration date: \(expirationDate.formatted(.dateTime))")
70 }
71
72 if let contentString = attributes.profileContent {
73 guard let contentData = Data(base64Encoded: contentString) else {
74 d.out.print(" content: error: bad base64")
75 continue
76 }
77
78 do {
79 let profile = try Mobileprovision(data: contentData)
80 let digest = try profile.digest()
81 d.out.print(" team identifiers:")
82 for teamID in digest.teamIdentifiers {
83 d.out.print(" - \(teamID.rawValue)")
84 }
85 d.out.print(" certificates:")
86 for certificate in digest.certificates {
87 d.out.print(" - \(certificate.serialNumber())")
88 }
89 d.out.print(" devices:")
90 for device in digest.devices {
91 d.out.print(" - \(device)")
92 }
93 let entitlements = try? String(decoding: Self.encoder.encode(digest.entitlements), as: UTF8.self)
94 d.out.print(" entitlements: \(entitlements ?? "<error>")")
95 } catch {
96 d.out.print(" content: error: \(error)")
97 }
98 }
99 }
100 }
101}