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

[cli] Produce valid JSON when -f json flag passed #9

Merged
merged 2 commits into from
Jul 5, 2018

Conversation

jessestuart
Copy link
Contributor

What does this PR do?

Amends the --format json flag to print valid JSON, which can be piped into other commands for further processing.

What's the context?

Hiya!
Long time user, first time contributor. I noticed that the "JSON" output format isn't actually valid JSON, but rather Python's standard serialized dict=>string format (using e.g., single quoted strings as keys, which are forbidden in the JSON spec); this makes combining the docker-hub command — something I find myself increasingly doing — not particularly Unix friendly.

In particular, outputting only valid JSON (no headers, etc.) to stdout ensures that the command can be easily piped into other tools, notably jq, allowing all sorts of fun constructions. I'll just give a couple examples —

Let's say I just want to see how many docker pulls my top N repos have received — I can simply run:

λ docker-hub repos -o jessestuart -f json | jq '.[] | ."Pull count"' | sum
110171

Or, say I want to easily query repo's tags and slicing / dicing / filtering based on arbitrary fields:

λ docker-hub tags -o jessestuart -r minio -f json | jq '.[] | .Name, ."Size", ."Last updated"' | xargs -L3 echo | sort -k2,3h | column -t
RELEASE.2018-06-09T03-43-35Z        20.49  MB  2018-06-22  00:29
RELEASE.2018-06-22T23-48-46Z        20.5   MB  2018-06-29  00:27
RELEASE.2018-06-22T23-48-46Z-amd64  20.5   MB  2018-06-29  00:17
RELEASE.2018-06-29T02-11-29Z        20.5   MB  2018-07-05  00:38
RELEASE.2018-06-29T02-11-29Z-amd64  20.5   MB  2018-07-05  00:28
latest                              20.5   MB  2018-07-05  00:38
latest-amd64                        20.5   MB  2018-07-05  00:28
RELEASE.2018-06-09T03-43-35Z-arm64  27.06  MB  2018-06-22  00:24
RELEASE.2018-06-22T23-48-46Z-arm64  27.07  MB  2018-06-29  00:20
RELEASE.2018-06-29T02-11-29Z-arm64  27.08  MB  2018-07-05  00:27
latest-arm64                        27.08  MB  2018-07-05  00:27
RELEASE.2018-06-09T03-43-35Z-arm    27.63  MB  2018-06-22  00:24
RELEASE.2018-06-22T23-48-46Z-arm    27.65  MB  2018-06-29  00:19
RELEASE.2018-06-29T02-11-29Z-arm    27.65  MB  2018-07-05  00:26
latest-arm                          27.65  MB  2018-07-05  00:26

Last one — and a pattern I use all the time — is to look up the N most recent published tags for an image:

λ docker-hub tags -o library -r consul -f json | jq -r '.[range(0,5)].Name'
latest
1.2.0
1.1.0
1.0.7
1.0.6

Other things to note?

In the interest of keeping the command's stdout clean & consistent to enable reuse (see the paper referenced above), I also removed the println stating that the DH login was successful. Feel free to object to this — those who prefer the unix-y style can easily grep -v that out — but I didn't feel like it added much value, since (personally) as a user, I assume that a command I execute multiple times will transparently handle auth after initial setup, and after that should only be alerted if authentication fails.

@coveralls
Copy link

coveralls commented Jul 5, 2018

Pull Request Test Coverage Report for Build 30

  • 2 of 3 (66.67%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.7%) to 34.756%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/libs/utils.py 2 3 66.67%
Files with Coverage Reduction New Missed Lines %
src/libs/utils.py 2 30.16%
Totals Coverage Status
Change from base Build 29: 0.7%
Covered Lines: 114
Relevant Lines: 328

💛 - Coveralls

@amalfra
Copy link
Owner

amalfra commented Jul 5, 2018

@jessestuart Nice PR, was a pleasure to read! Thank you so much for the valuable contribution. Special thanks for linking that paper it was very informative read. 🙏

@amalfra amalfra merged commit 8a56964 into amalfra:master Jul 5, 2018
@jessestuart jessestuart deleted the jesse/produce_valid_json branch July 5, 2018 20:29
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