-
Notifications
You must be signed in to change notification settings - Fork 880
stage0/image: list images output in JSON format #3334
Conversation
Can one of the admins verify this patch? |
ok to test |
@lucasallan this looks really nice, thanks a lot! Code seems fine at a quick glance, can you please just format the commit titles as shown in the doc? I also retitled this PR to follow the same style, if you are unsure about the format. @euank @s-urbaniak can one of you please do a final pass on this and shepherd it in? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few nits, also I suggest to squash all commits adhering to https://github.com/coreos/rkt/blob/master/CONTRIBUTING.md.
@@ -140,21 +157,15 @@ func init() { | |||
cmdImageList.Flags().Var(&flagImagesSortAsc, "order", `choose the sorting order if at least one sort field is provided (--sort). Accepted values: "asc", "desc"`) | |||
cmdImageList.Flags().BoolVar(&flagNoLegend, "no-legend", false, "suppress a legend with the list") | |||
cmdImageList.Flags().BoolVar(&flagFullOutput, "full", false, "use long output format") | |||
cmdImageList.Flags().StringVar(&flagImageFormat, "format", "", "choose the output format, allowed format includes 'json', 'json-pretty'. If empty, then the result is printed as key value pairs") | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary newline
} | ||
|
||
imageImportTime := humanize.Time(aciInfo.ImportTime) | ||
imageLastUsed := humanize.Time(aciInfo.LastUsed) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these humanized time strings will also appear in the JSON output, but JSON is meant to be machine-readable. I suggest to use rfc3339 based outputs (see https://godoc.org/time#pkg-constants) in this case.
imageImportTime := humanize.Time(aciInfo.ImportTime) | ||
imageLastUsed := humanize.Time(aciInfo.LastUsed) | ||
totalSize := aciInfo.Size + aciInfo.TreeStoreSize | ||
imageSize := humanize.IBytes(uint64(totalSize)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for machine readable JSON output I'd go with plain totalSize
.
trimLength := pos + 13 | ||
if pos > 0 && trimLength < len(imageID) { | ||
imageID = imageID[:trimLength] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's put the whole else block in a separate private function, i.e. trimImageID
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also for the JSON case, I would not trim the imageID, this also retains symmetry with rkt list
, which also ignores rkt list --full --format=json
.
Thanks for the feedback, I really appreciate it. I'll make the requested changes and get back to you guys ASAP. |
@lucasallan looking forward to, thanks a lot for the contribution! |
1c11d24
to
0c398f2
Compare
I've made all the requested changes and I also squashed and reworded the commits (I hope I got it right). Let me know if you need me to do any other change. Thanks! |
0c398f2
to
08cc80b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code LGTM, thanks!
I retriggered a build, as it seems the git checkout didn't work properly on the CI machine.
One small nit in the commit message:
Replace "For more information: github.com//issues/3298" with "Fixes #3298"
08cc80b
to
6a952dd
Compare
Fixed the commit message, cc @s-urbaniak |
@@ -113,6 +131,7 @@ var ( | |||
flagImagesFields *rktflag.OptionList | |||
flagImagesSortFields *rktflag.OptionList | |||
flagImagesSortAsc ImagesSortAsc | |||
flagImageFormat string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could make a type OutputFormat int
that implements the cobra Set
method and does input validation.
It would also let all the == ""
things below look prettier with a const OutputFormatTabbed
type thing.
I'm okay with this cleanup as a followup, just wanted to capture the thought.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds great, thanks!
LGTM, though I tossed in a suggestion for a cleanup (either now or later). The other missing thing that can come in a followup is tests! |
@lucasallan Do you mind tackling @euank's comments? :-) |
@s-urbaniak yes, will do asap. thanks! |
6a952dd
to
97bca54
Compare
I've made some changes, could you take a look please? @euank I'm also looking into adding tests for it and for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I clarified a bit more what I was suggesting before.
Also, the test failures do look related (FAIL: TestImageSize
, looks like --full
used to print size-in-bytes and FAIL: TestAPIServiceListInspectImages
).
I find semaphore's output the easiest to read.
The help is appreciated! Tests in a followup PR sound fine to me.
return strings.Join(r.attributes(), "\t") | ||
} | ||
|
||
type outputFormat int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't express myself totally clearly on this, but I was also saying some consts could help readability.
Even though go doesn't have real enums, it's sorta idiomatic to make something like them with a type "alias" + consts (sorta like the iota docs reference). In this case, it would look something like:
const (
outputFormatTabbed outputFormat = iota
outputFormatJSON
outputFormatPrettyJSON
)
The consts can then be used for assignment and comparison to make things more readable and to give a little bit of type safety.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the changes. I've never used iota
before - Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last nit... Typically the consts for an "enum like thing" are grouped right under the type definition (for example, ConnState in the go stdlib).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks!
12c317b
to
e54dc98
Compare
e54dc98
to
2177c08
Compare
I have one last nit about where the consts are; after that LGTM on green. Edit: Nit addressed, proper LGTM! |
These changes add the option of printing the image list in json and json-pretty format. Now you can specify the output format when listing images: rkt image list --format=json rkt image list --format=json-pretty Fixes rkt#3298
2177c08
to
ee41e22
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks a lot! 🎉
Thank you! I'm already working in another PR. |
This fixes a regression introduced in rkt#3334, where the `--field` is not anymore working for plaintext output. Tests around image listing are also introduced here to catch future regressions.
This fixes a regression introduced in rkt#3334, where the `--field` flag is not anymore working for plaintext output. Tests around image listing are also introduced here to catch future regressions.
@lucab PTAL
List images in the json format:
And json-pretty:
Fixes #3298