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