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

feat: yaml output #2496

Merged
merged 13 commits into from
Jul 7, 2022
Merged

feat: yaml output #2496

merged 13 commits into from
Jul 7, 2022

Conversation

kimskimchi
Copy link
Contributor

@kimskimchi kimskimchi commented Jul 6, 2022

Summary

Introduces yaml for existing json outputs.
Comes with free tests. 👍

Checklist

  • Wrote appropriate unit tests
  • Considered security implications of the change
  • Updated associated docs where necessary
  • Updated associated configuration where necessary
  • Change is backwards compatible if it needs to be (user can upgrade without manual steps?)
  • Nothing sensitive logged
  • Considered data migrations for smooth upgrades

Related Issues

Resolves #972

@vercel vercel bot temporarily deployed to Preview July 6, 2022 16:18 Inactive
@@ -56,6 +57,12 @@ func newDestinationsListCmd(cli *CLI) *cobra.Command {
return err
}
cli.Output(string(jsonOutput))
case "yaml":
Copy link
Collaborator

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?

Copy link
Collaborator

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

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

internal/cmd/providers_test.go Outdated Show resolved Hide resolved
@kimskimchi kimskimchi requested review from mxyng and BruceMacD July 7, 2022 15:21
@vercel vercel bot temporarily deployed to Preview July 7, 2022 15:26 Inactive
Copy link
Contributor

@dnephin dnephin left a 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":
Copy link
Contributor

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.

internal/cmd/testdata/TestProvidersAddCmd/list_with_yaml Outdated Show resolved Hide resolved
@@ -5,6 +5,7 @@ import (
"fmt"

"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
Copy link
Contributor

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":
Copy link
Contributor

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.

internal/cmd/users.go Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview July 7, 2022 17:20 Inactive
@kimskimchi kimskimchi merged commit 4cbad5a into main Jul 7, 2022
@kimskimchi kimskimchi deleted the cli-9 branch July 7, 2022 18:23
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.

cli: Support YAML for --format flag
4 participants