Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: add JSON output format to many CLI commands #6082

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

Merged
merged 8 commits into from
Feb 8, 2023

Conversation

deansheather
Copy link
Member

Reworks the output format code which is scattered across many commands into a consistent output formatter object, which handles applying flags and the final output operation.

The output formatter provides an interface for making custom formatters (which some commands utilize, e.g. coder user show).

Some commands do some messy stuff currently as they run extra code before formatting, which I've accommodated for by reusing the existing functions to avoid breaking existing output formatting.

@deansheather deansheather self-assigned this Feb 7, 2023
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! One nice addition would be to have a golden file test for the JSON output for one or multiple commands. It might help us make sure we don't have any regressions when messing with struct tags.

I feel I have to mention the panics though.. I know why they're there, I just don't like them. 😂 (Won't ask for a change this time around. 😉)

@deansheather
Copy link
Member Author

I've added golden files for users list and list JSON output formats.

Regarding panics, it'd be awkward to return an error from the new outputter function as this func is called from within a new command function, which does not return an error.

@deansheather deansheather requested a review from mafredri February 8, 2023 15:26
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice solution for golden files! ❤️

replace[k] = []byte(v)
}
for k, v := range replace {
got = bytes.ReplaceAll(got, []byte(k), v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can just use a map[string]string and strings.ReplaceAll to simplify and avoid the byte/string conversions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have to convert got to a string in that case:

gotStr := strings.ReplaceAll([]byte(got), k, v)
got = []byte(gotStr)

@deansheather deansheather enabled auto-merge (squash) February 8, 2023 17:02
@deansheather deansheather merged commit d60ec3e into main Feb 8, 2023
@deansheather deansheather deleted the dean/output-formats branch February 8, 2023 17:09
@github-actions github-actions bot locked and limited conversation to collaborators Feb 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants