Skip to content

feat: output flag plaintext#197

Merged
dbolson merged 8 commits into
sc-241065/output-flagfrom
sc-241065/output-flag-plaintext
Apr 23, 2024
Merged

feat: output flag plaintext#197
dbolson merged 8 commits into
sc-241065/output-flagfrom
sc-241065/output-flag-plaintext

Conversation

@dbolson
Copy link
Copy Markdown
Contributor

@dbolson dbolson commented Apr 22, 2024

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

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

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.

Comment thread internal/output/output.go
}

// 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) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
)

Comment thread internal/output/output.go
return o.String(), nil
}

return "", ErrInvalidOutputKind
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We should never get here since we validate the input in the command handler.

"fmt"
)

var singularPlaintextOutputFn = func(r resource) string {
Copy link
Copy Markdown
Contributor Author

@dbolson dbolson Apr 23, 2024

Choose a reason for hiding this comment

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

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 {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sneaking this in here based on this comment.

@dbolson dbolson marked this pull request as draft April 23, 2024 17:18
@dbolson
Copy link
Copy Markdown
Contributor Author

dbolson commented Apr 23, 2024

Moving this to draft to work on it more.

Comment thread internal/output/output.go

// Outputter defines the different ways a command's response can be formatted based on
// user input.
type Outputter interface {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@dbolson dbolson marked this pull request as ready for review April 23, 2024 17:54
Comment thread cmd/environments/get.go
return err
}

output, err := output.CmdOutput(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the implementation. Another PR will add it for all endpoints and add functionality to work for a response with multiple resources.

Copy link
Copy Markdown
Contributor

@k3llymariee k3llymariee left a comment

Choose a reason for hiding this comment

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

into it

@dbolson dbolson merged commit 424d2fd into sc-241065/output-flag Apr 23, 2024
@dbolson dbolson deleted the sc-241065/output-flag-plaintext branch April 23, 2024 18:24
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.

3 participants