Modern Apps git

xtool.git

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

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

DSCertificatesCommand.swift91 lines · 3.0 KiBRaw
1import Foundation
2import ArgumentParser
3import XKit
4import DeveloperAPI
5
6struct DSCertificatesCommand: AsyncParsableCommand {
7 static let configuration = CommandConfiguration(
8 commandName: "certificates",
9 abstract: "Interact with certificates",
10 subcommands: [
11 DSCertificatesListCommand.self,
12 ],
13 defaultSubcommand: DSCertificatesListCommand.self
14 )
15}
16
17struct DSCertificatesListCommand: AsyncParsableCommand {
18 static let configuration = CommandConfiguration(
19 commandName: "list",
20 abstract: "List certificates"
21 )
22
23 // swiftlint:disable:next cyclomatic_complexity
24 func run() async throws {
25 let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
26
27 let certificates = try await DeveloperAPIPages {
28 try await client.certificatesGetCollection().ok.body.json
29 } next: {
30 $0.links.next
31 }
32 .map(\.data)
33 .reduce(into: [], +=)
34
35 for certificate in certificates {
36 print("- id: \(certificate.id)")
37 guard let attributes = certificate.attributes else {
38 continue
39 }
40
41 if let name = attributes.name {
42 print(" name: \(name)")
43 }
44
45 if let certificateType = attributes.certificateType {
46 print(" type: \(certificateType.rawValue)")
47 }
48
49 if let displayName = attributes.displayName {
50 print(" display name: \(displayName)")
51 }
52
53 if let serialNumber = attributes.serialNumber {
54 print(" serial number: \(serialNumber)")
55 }
56
57 if let platform = attributes.platform {
58 print(" platform: \(platform.rawValue)")
59 }
60
61 if let expirationDate = attributes.expirationDate {
62 print(" expiry: \(expirationDate.formatted(.dateTime))")
63 }
64
65 if let contentString = attributes.certificateContent {
66 guard let content = Data(base64Encoded: contentString) else {
67 print(" content: error: bad base64")
68 continue
69 }
70 do {
71 let certificate = try Certificate(data: content)
72 print(" content:")
73 switch Result(catching: { try certificate.developerIdentity() }) {
74 case .success(let id):
75 print(" common name: \(id)")
76 case .failure(let error):
77 print(" common name: error: \(error)")
78 }
79 switch Result(catching: { try certificate.teamID() }) {
80 case .success(let teamID):
81 print(" team id: \(teamID)")
82 case .failure(let error):
83 print(" team id: error: \(error)")
84 }
85 } catch {
86 print(" content: error: \(error)")
87 }
88 }
89 }
90 }
91}