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

Conversation

csjones
Copy link
Contributor

@csjones csjones commented Aug 17, 2020

🤔 Objective and Motivation

The objective of these changes are to purpose adding an optional flag -v/--verbose to display more output in terminal. My motivation for this flag is to remove overhead when using asc with ansible. I'm using the following command in my ansible playbook:

asc profiles list --json --filter-name "App Store {{ app_name }}" --download-path .

...and I get the following output:

"stdout": "📥 Profile 'App Store Test001010002' downloaded to: ./aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa.mobileprovision\n[{\"createdDate\":619333688,\"expirationDate\":620178602,\"name\":\"App Store Test001010002\",\"platform\":\"IOS\",\"profileContent\":\"MdMIIdqAAYJKoZIhvcNAQQcCoIIdmTCCHZCUCAQExCzACJB........

The "stdout" is now malformed json and requires additional parsing to work correctly by filtering out the "📥 Profile 'App Store Test001010002' downloaded to: ./aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa.mobileprovision\n". With the --verbose flag added, the command now looks like this:

asc profiles list --verbose --json --filter-name "App Store {{ app_name }}" --download-path .

...and I get the following output:

"stdout": "📥 Profile 'App Store Test001010002' downloaded to: ./aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa.mobileprovision\n[{\"createdDate\":619333688,\"expirationDate\":620178602,\"name\":\"App Store Test001010002\",\"platform\":\"IOS\",\"profileContent\":\"MdMIIdqAAYJKoZIhvcNAQQcCoIIdmTCCHZCUCAQExCzACJB........

...and the default command and output looks like this:

asc profiles list --json --filter-name "App Store {{ app_name }}" --download-path .

...and the json returned to stdout is no longer malformed so now I get the following output:

"stdout": "[{\"createdDate\":619333688,\"expirationDate\":620178602,\"name\":\"App Store Test001010002\",\"platform\":\"IOS\",\"profileContent\":\"MdMIIdqAAYJKoZIhvcNAQQcCoIIdmTCCHZCUCAQExCzACJB........

📝 Summary of Changes

Changes proposed in this pull request:

  • Adds the optional flag -v/--verbose to display more in terminal.
  • Changed output of all commands to be parsable by default
  • Removed .prettyPrinted and created Add a flag to prettify output  #221

⚠️ Items of Note

Document anything here that you think the reviewer(s) of this PR may need to know, or would be of specific interest.

The --verbose only changes the output of a CommonParsableCommand that has not been commented with a TODO.

🧐🗒 Reviewer Notes

💁 Example

swift run asc profiles list --verbose --json --filter-name "Cool App" --download-path ~/Documents/Cool\ App

🔨 How To Test

Run these command and notice the messaging in terminal:

swift run asc profiles list --json --filter-name "Cool App" --download-path ~/Documents/Cool\ App
swift run asc users sync ~/Documents/fake.json --dry-run
swift run asc certificates list
swift run asc certificates read "M109UYTGSF007" --certificate-output distribution.cer
swift run asc certificates revoke "M109UYTGSF007"

Run the same commands with -v/--verbose added and notice more messaging in terminal:

swift run asc profiles list --verbose --json --filter-name "Cool App" --download-path ~/Documents/Cool\ App
swift run asc users sync -v ~/Documents/fake.json --dry-run
swift run asc certificates list -v
swift run asc certificates read --verbose "M109UYTGSF007" --certificate-output distribution.cer
swift run asc certificates revoke --verbose "M109UYTGSF007"

@adamjcampbell
Copy link
Contributor

adamjcampbell commented Aug 19, 2020

Hey @csjones, thank you for your contribution. You've prompted a discussion on our end with this PR about whether quiet, output only commands should be the default response. That is, whether or not this should be implemented as only logging these Certificate downloaded Certificate revoked messages when a --verbose flag is passed.

This would then need to be rolled out across other commands so that the output of all commands is parsable by default. Did you have any other thoughts on the matter for your use case?

@orj
Copy link
Member

orj commented Aug 19, 2020

I agree with @adamjcampbell here. I think the stuff like 📥 Profile 'X' downloaded to: ... should only be printed when a --verbose flag was in effect.

🤔 One thing to consider is perhaps when using --json, --yaml, and --csv, --verbose could be false but with --table it could be true? Given that the computer parsable formats are more likely to be used in pipes than the table output which is designed for interactive use. Thoughts?

@csjones
Copy link
Contributor Author

csjones commented Aug 20, 2020

Hey @adamjcampbell @orj, I'm in favor of these changes. I think machine parsable by default and a --verbose option instead of --quiet makes a lot of sense, especially for my use case. Also makes sense to default --verbose to true when using the human parseable --table option.

I would propose one more additional change. When using --json (or any machine parsable format) to minimize the output by default instead of pretty printing the output. Currently, --json is using a prettify option and adds additional whitespace and newlines which needs to be filtered out.

@adamjcampbell
Copy link
Contributor

@csjones sounds good to me. Are you happy to make those changes for this command / this PR?

In regards to the pretty formatting of json I would think filtering out the white space would be optional. It shouldn't affect the ability to parse the JSON. However we could have it as an option or the default when not in verbose mode.

What are your thoughts on the matter?

@orj
Copy link
Member

orj commented Aug 20, 2020

@adamjcampbell Perhaps we can have a --pretty option for prettifying the json. Mostly I think you would be piping json output into a tool like jqand jq can pretty it for you.

@csjones
Copy link
Contributor Author

csjones commented Aug 21, 2020

@adamjcampbell Sure! I'll make the changes and update the PR. For pretty printing output, I'm leaning towards removing .prettyPrinted since --pretty currently does not exist and I prefer @orj's idea of using jq if needed because my use case is aimed towards better accommodating machine parsable output. Additionally, I could also create a new GitHub issue to add a --pretty option and see if any other asc users would prefer to have this feature flag added.

Does this list of changes look good?

Summary of changes:

  • Change --quiet to be --verbose
  • --verbose defaults to false except when --table is used
  • Remove use of .prettyPrinted
  • Create GitHub issue for --pretty feature flag

@adamjcampbell
Copy link
Contributor

@csjones sounds like a plan! Thanks for all your efforts on this.

@csjones csjones changed the title Adding a quiet flag for displaying less messages in terminal Adding a verbose flag for displaying more messages in terminal Aug 23, 2020
@@ -55,7 +55,10 @@ struct ListCertificatesCommand: CommonParsableCommand {

let file = try certificateProcessor.write($0)

print("📥 Certificate '\($0.name ?? "")' downloaded to: \(file.path)")
// Command output is parsable by default. Only print if user is enabling verbosity or output is a `.table`
if common.verbose || common.outputFormat == .table {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to have to use common.verbose || common.outputFormat == .table quite a bit here.
What do you think about making verbose be Bool? (defaulting to nil) and then we can add a computed var either a Bool or enum to check.

Thinking along these lines:

// CommonParsableCommand.swift

enum PrintLevel {
  case quiet
  case verbose
}

// CommonOptions

var printLevel: PrintLevel {
  switch verbose {
    case true:
      return .verbose
    case false:
      return .quiet
    case nil:
      return common.outputFormat == .table ? .verbose : .quiet
  }
}

// Usage

if common.printLevel == .verbose {
  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this is a good suggestion and I'll make these changes with small tweaks. I'll leave verbose as a Bool and I'll resolve the common.outputFormat == .table ternary in the switch statement itself. Should look something like this:

// CommonOptions

var printLevel: PrintLevel {
  switch (verbose, outputFormat == .table) {
    case (true, _), (_, true):
      return .verbose
    case (false, false):
      return .quiet
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, thanks @csjones

Copy link
Contributor

@adamjcampbell adamjcampbell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@csjones This is looking ✅, just needs the swiftlint warnings attended to

@adamjcampbell adamjcampbell merged commit c176752 into ittybittyapps:master Aug 26, 2020
@csjones csjones deleted the csjones-quiet branch January 28, 2023 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants