feat: output flag plaintext#197
Conversation
| } | ||
|
|
||
| // CmdOutput returns a command's response as a string formatted based on the user's requested type. | ||
| func CmdOutput(outputKind string, outputter OutputterFn) (string, error) { |
There was a problem hiding this comment.
This is the main function in this package. We use the OutputterFn factory so we can do error handling here instead of at the CmdOutput call site in the command handler.
// with factory fn
output, err := output.CmdOutput(
viper.GetString(cliflags.OutputFlag),
output.SingularOutput(response),
)
if err != nil {
return err
}
vs
// without factory fn
outputter, err := output.SingularOutput(response)
if err != nil {
return err
}
output, err := output.CmdOutput(
viper.GetString(cliflags.OutputFlag),
outputter
)
| return o.String(), nil | ||
| } | ||
|
|
||
| return "", ErrInvalidOutputKind |
There was a problem hiding this comment.
We should never get here since we validate the input in the command handler.
| "fmt" | ||
| ) | ||
|
|
||
| var singularPlaintextOutputFn = func(r resource) string { |
There was a problem hiding this comment.
We're going to end up with a few of these functions depending on the shape of the response. We'll figure out a better name when there are a few of these.
| return errors.New(errorMessage) | ||
| } | ||
|
|
||
| func getCommandPath(cmd *cobra.Command) string { |
There was a problem hiding this comment.
Sneaking this in here based on this comment.
|
Moving this to draft to work on it more. |
|
|
||
| // Outputter defines the different ways a command's response can be formatted based on | ||
| // user input. | ||
| type Outputter interface { |
There was a problem hiding this comment.
We need an interface to define how to output JSON and and plain text versions of a response. This will be for a singular resource and when there are multiple resources.
| return err | ||
| } | ||
|
|
||
| output, err := output.CmdOutput( |
There was a problem hiding this comment.
This is the implementation. Another PR will add it for all endpoints and add functionality to work for a response with multiple resources.
Use the output flag to show either JSON or plaintext for a command's response.
This only works for
environments get, but another PR will add support to the rest of the commands.Requirements
Related issues
Provide links to any issues in this repository or elsewhere relating to this pull request.
Describe the solution you've provided
Provide a clear and concise description of what you expect to happen.
Describe alternatives you've considered
Provide a clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context about the pull request here.