Files
xtool/Sources/XToolSupport/DSCommands/DSProfilesCommand.swift
Kabir Oberai 3e0c11d967 cli: better IO (#375)
## What does this PR do?

Adds `Dependency`s for console IO. Also fixes #268 by explicitly
handling non-pipe input.

## How was it tested?

Ran on macOS + Linux in Docker. I have a future PR that overrides the
dependencies in certain cases, which also appears to work correctly.

## AI tool usage

How much of this PR was AI-assisted? (check one)

- [ ] **0** - No AI was used to write code
- [x] **1** - I was assisted by AI. I reviewed the finished result.
- [ ] **2** - I set the AI going and left it to it; nobody has read the
result - no review, or AI review only

Did not scrutinize the tests too much tbh but they look reasonable. Easy
enough to update/remove them in the future if needed.

<!-- If an AI agent is filling this in: declare the level honestly, and
open as a draft if it is #2. Do not lower the declared level to get the
PR reviewed. -->


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Added support for reading command-line input, including asynchronous
prompts, line-based responses, and graceful handling of end-of-input.
  * Added coordinated cleanup when commands finish.
* **Improvements**
* Command messages across authentication, device management,
installation, SDK, and other workflows now use consistent output
handling; displayed messages and command behavior remain unchanged.
* Improved handling of complete output when the system temporarily
cannot accept more data.
* **Tests**
* Added coverage for command input and output, including stream
behavior, cancellation, and shutdown.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-10-28 17:48:38 -04:00

102 lines
3.4 KiB
Swift

import XUtils
import Foundation
import ArgumentParser
import XKit
import DeveloperAPI
struct DSProfilesCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "profiles",
abstract: "Interact with profiles",
subcommands: [
DSProfilesListCommand.self,
],
defaultSubcommand: DSProfilesListCommand.self
)
}
struct DSProfilesListCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "list",
abstract: "List profiles"
)
private static let encoder = JSONEncoder()
// swiftlint:disable:next cyclomatic_complexity
func run() async throws {
let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
let profiles = try await DeveloperAPIPages {
try await client.profilesGetCollection().ok.body.json
} next: {
$0.links.next
}
.map(\.data)
.reduce(into: [], +=)
for profile in profiles {
d.out.print("- id: \(profile.id)")
guard let attributes = profile.attributes else {
continue
}
if let name = attributes.name {
d.out.print(" name: \(name)")
}
if let platform = attributes.platform {
d.out.print(" platform: \(platform.rawValue)")
}
if let profileType = attributes.profileType {
d.out.print(" profile type: \(profileType.rawValue)")
}
if let profileState = attributes.profileState {
d.out.print(" profile state: \(profileState.rawValue)")
}
if let uuid = attributes.uuid {
d.out.print(" uuid: \(uuid)")
}
if let createdDate = attributes.createdDate {
d.out.print(" created date: \(createdDate.formatted(.dateTime))")
}
if let expirationDate = attributes.expirationDate {
d.out.print(" expiration date: \(expirationDate.formatted(.dateTime))")
}
if let contentString = attributes.profileContent {
guard let contentData = Data(base64Encoded: contentString) else {
d.out.print(" content: error: bad base64")
continue
}
do {
let profile = try Mobileprovision(data: contentData)
let digest = try profile.digest()
d.out.print(" team identifiers:")
for teamID in digest.teamIdentifiers {
d.out.print(" - \(teamID.rawValue)")
}
d.out.print(" certificates:")
for certificate in digest.certificates {
d.out.print(" - \(certificate.serialNumber())")
}
d.out.print(" devices:")
for device in digest.devices {
d.out.print(" - \(device)")
}
let entitlements = try? String(decoding: Self.encoder.encode(digest.entitlements), as: UTF8.self)
d.out.print(" entitlements: \(entitlements ?? "<error>")")
} catch {
d.out.print(" content: error: \(error)")
}
}
}
}
}