-
Notifications
You must be signed in to change notification settings - Fork 58
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
feat: yaml output #2496
feat: yaml output #2496
Conversation
@@ -56,6 +57,12 @@ func newDestinationsListCmd(cli *CLI) *cobra.Command { | |||
return err | |||
} | |||
cli.Output(string(jsonOutput)) | |||
case "yaml": |
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.
Any way we could have a re-usable function for the output formatting to reduce the repetition in the cli output code here?
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.
Mentioned in the previous change. There should be a Formatter
interface which JSON
/YAML
/TableFormat
implements. The result is passed to cli.Output
for printing. This will allow more formats to be added with minimal impact on the existing code base
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 the actual function, not sure how this can be achieved, could I have an example?
The couple of commands here are all list
, so it expects similar outputs, but different functions will not use the same something.Items
but rather be completely different.
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.
Since yaml.Marshal
and json.Marshal
can accept anything, the function would can accept an argument of type interface{}
. The one challenge is the table format, since that requires a bit more guidance to tell it which columns to use.
I think we could do this as a follow up.
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 put together an example of one option here: cli-9...dnephin/formatter-example
But let's look into that in a follow up PR.
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.
Looks good. We should move the Conencted provider
output to Stderr
, but otherwise this looks great. I think we can refactor to a formatter interface in a follow up.
@@ -56,6 +57,12 @@ func newDestinationsListCmd(cli *CLI) *cobra.Command { | |||
return err | |||
} | |||
cli.Output(string(jsonOutput)) | |||
case "yaml": |
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.
Since yaml.Marshal
and json.Marshal
can accept anything, the function would can accept an argument of type interface{}
. The one challenge is the table format, since that requires a bit more guidance to tell it which columns to use.
I think we could do this as a follow up.
Co-authored-by: Daniel Nephin <[email protected]>
This reverts commit 7bd8b4d.
@@ -5,6 +5,7 @@ import ( | |||
"fmt" | |||
|
|||
"github.com/spf13/cobra" | |||
"sigs.k8s.io/yaml" |
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 anyone else curious about this. It turns out we have to use sigs.k8s.io/yaml
so that the field names come from the json
tags. If we used the more standard yaml library we'd have to add yaml
names to all the fields, and the output would look strange for some fields (ex: would use {}
for the zero value of api.Time
instead of null
).
Although the second part of that we could fix by changing to MarshalText
instead of MashalJson
.
@@ -56,6 +57,12 @@ func newDestinationsListCmd(cli *CLI) *cobra.Command { | |||
return err | |||
} | |||
cli.Output(string(jsonOutput)) | |||
case "yaml": |
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 put together an example of one option here: cli-9...dnephin/formatter-example
But let's look into that in a follow up PR.
Summary
Introduces
yaml
for existingjson
outputs.Comes with free tests. 👍
Checklist
Related Issues
Resolves #972