Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a verbose flag for displaying more messages in terminal #220

Merged
merged 7 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ struct ListCertificatesCommand: CommonParsableCommand {

let file = try certificateProcessor.write($0)

print("📥 Certificate '\($0.name ?? "")' downloaded to: \(file.path)")
// Only print if the `PrintLevel` is set to verbose.
if common.printLevel == .verbose {
print("📥 Certificate '\($0.name ?? "")' downloaded to: \(file.path)")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ struct ReadCertificateCommand: CommonParsableCommand {

let file = try fileProcessor.write(certificate)

print("📥 Certificate '\(certificate.name ?? "")' downloaded to: \(file.path)")
// Only print if the `PrintLevel` is set to verbose.
if common.printLevel == .verbose {
print("📥 Certificate '\(certificate.name ?? "")' downloaded to: \(file.path)")
}
}

certificate.render(format: common.outputFormat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ struct RevokeCertificatesCommand: CommonParsableCommand {
let service = try makeService()
_ = try service.revokeCertificates(serials: serials)

serials.forEach {
print("🚮 Certificate `\($0)` revoked.")
// Only print if the `PrintLevel` is set to verbose.
if common.printLevel == .verbose {
serials.forEach {
print("🚮 Certificate `\($0)` revoked.")
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions Sources/AppStoreConnectCLI/Commands/CommonParsableCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ protocol CommonParsableCommand: ParsableCommand {
func makeService() throws -> AppStoreConnectService
}

/// A level representing the verbosity of a command.
enum PrintLevel {
// Withholds displaying normal status messages
case quiet
// Displays all status messages
case verbose
}

extension CommonParsableCommand {
func makeService() throws -> AppStoreConnectService {
AppStoreConnectService(configuration: try APIConfiguration(common.authOptions))
Expand All @@ -22,4 +30,21 @@ struct CommonOptions: ParsableArguments {

@Flag(default: .table, help: "Display results in specified format.")
var outputFormat: OutputFormat

/// The verbosity of the executed command.
var printLevel: PrintLevel {
// Commands are parsable by default except for `--table` which is intended to be interactive.
switch (verbose, outputFormat == .table) {
// if `--verbose` or `--table`is used then return a verbose print level
case (true, _), (_, true):
return .verbose
// if both `--verbose` and `--table` are not used, then return a quiet print level
case (false, false):
return .quiet
}
}

/// Used to define the command's `PrintLevel`. Defaults to `false`.
@Flag(name: .shortAndLong, help: "Display extra messages as command is running.")
var verbose: Bool
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ struct ListProfilesCommand: CommonParsableCommand {
try profiles.forEach {
let file = try processor.write($0)

print("📥 Profile '\($0.name!)' downloaded to: \(file.path)")
// Only print if the `PrintLevel` is set to verbose.
if common.printLevel == .verbose {
print("📥 Profile '\($0.name!)' downloaded to: \(file.path)")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ struct SyncUsersCommand: CommonParsableCommand {
var dryRun: Bool

func run() throws {
if dryRun {
// Only print if the `PrintLevel` is set to verbose.
if common.printLevel == .verbose {
print("## Dry run ##")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ protocol ResultRenderable: Codable {
extension ResultRenderable {
func renderAsJSON() -> String {
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = [.prettyPrinted, .sortedKeys]
// Withholding `.prettyPrinted` to maintain output that is parsable by default.
// Consider adding the option `--pretty` if needed https://github.com/ittybittyapps/appstoreconnect-cli/issues/221
jsonEncoder.outputFormatting = [.sortedKeys]
let json = try! jsonEncoder.encode(self) // swiftlint:disable:this force_try
return String(data: json, encoding: .utf8)!
}
Expand Down