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