[cli] Produce valid JSON when -f json
flag passed
#9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Or, say I want to easily query repo's tags and slicing / dicing / filtering based on arbitrary fields:
Last one — and a pattern I use all the time — is to look up the N most recent published tags for an image:
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.