-
Notifications
You must be signed in to change notification settings - Fork 13
feat: output flag plaintext #197
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
dbolson
merged 8 commits into
sc-241065/output-flag
from
sc-241065/output-flag-plaintext
Apr 23, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
31a2196
Use output flag to show JSON or plaintext
cdaabf6
Call outputter in command handler
c86d564
Backfill test
ab8e855
Rename
eb3ea48
Refactor to pass in fn instead of interface
552db88
Remove stripe code
05a6152
Create additional interface
2fa2d85
Implement plaintext for multiple resource response (#199)
dbolson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,27 +11,27 @@ import ( | |
|
|
||
| "ldcli/cmd/cliflags" | ||
| errs "ldcli/internal/errors" | ||
| "ldcli/internal/output" | ||
| ) | ||
|
|
||
| // Validate is a validator for commands to print an error when the user input is invalid. | ||
| func Validate() cobra.PositionalArgs { | ||
| return func(cmd *cobra.Command, args []string) error { | ||
| rebindFlags(cmd, cmd.ValidArgs) // rebind flags before validating them below | ||
| commandPath := getCommandPath(cmd) | ||
|
|
||
| _, err := url.ParseRequestURI(viper.GetString(cliflags.BaseURIFlag)) | ||
| if err != nil { | ||
| return CmdError(errs.ErrInvalidBaseURI, commandPath) | ||
| return CmdError(errs.ErrInvalidBaseURI, cmd.CommandPath()) | ||
| } | ||
|
|
||
| err = cmd.ValidateRequiredFlags() | ||
| if err != nil { | ||
| return CmdError(err, commandPath) | ||
| return CmdError(err, cmd.CommandPath()) | ||
| } | ||
|
|
||
| err = validateOutput(viper.GetString(cliflags.OutputFlag)) | ||
| if err != nil { | ||
| return CmdError(err, commandPath) | ||
| return CmdError(err, cmd.CommandPath()) | ||
| } | ||
|
|
||
| return nil | ||
|
|
@@ -48,25 +48,14 @@ func CmdError(err error, commandPath string) error { | |
| return errors.New(errorMessage) | ||
| } | ||
|
|
||
| func getCommandPath(cmd *cobra.Command) string { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sneaking this in here based on this comment. |
||
| var commandPath string | ||
| if cmd.Annotations["scope"] == "plugin" { | ||
| commandPath = fmt.Sprintf("stripe %s", cmd.CommandPath()) | ||
| } else { | ||
| commandPath = cmd.CommandPath() | ||
| } | ||
|
|
||
| return commandPath | ||
| } | ||
|
|
||
| func validateOutput(outputFlag string) error { | ||
| validKinds := map[string]struct{}{ | ||
| "json": {}, | ||
| "plaintext": {}, | ||
| } | ||
| _, ok := validKinds[outputFlag] | ||
| if !ok { | ||
| return errors.New("output is invalid") | ||
| return output.ErrInvalidOutputKind | ||
| } | ||
|
|
||
| return nil | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package output | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| ) | ||
|
|
||
| var multiplePlaintextOutputFn = func(r resource) string { | ||
| return fmt.Sprintf("* %s (%s)", r.Name, r.Key) | ||
| } | ||
|
|
||
| // TODO: rename this to be "cleaner"? -- NewMultipleOutput() | ||
| func NewMultipleOutputterFn(input []byte) multipleOutputterFn { | ||
| return multipleOutputterFn{ | ||
| input: input, | ||
| } | ||
| } | ||
|
|
||
| type multipleOutputterFn struct { | ||
| input []byte | ||
| } | ||
|
|
||
| func (o multipleOutputterFn) New() (Outputter, error) { | ||
| var r resources | ||
| err := json.Unmarshal(o.input, &r) | ||
| if err != nil { | ||
| return MultipleOutputter{}, err | ||
| } | ||
|
|
||
| return MultipleOutputter{ | ||
| outputFn: multiplePlaintextOutputFn, | ||
| resources: r, | ||
| resourceJSON: o.input, | ||
| }, nil | ||
| } | ||
|
|
||
| type MultipleOutputter struct { | ||
| outputFn PlaintextOutputFn | ||
| resources resources | ||
| resourceJSON []byte | ||
| } | ||
|
|
||
| func (o MultipleOutputter) JSON() string { | ||
| return string(o.resourceJSON) | ||
| } | ||
|
|
||
| func (o MultipleOutputter) String() string { | ||
| return formatColl(o.resources.Items, o.outputFn) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package output_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "ldcli/internal/output" | ||
| ) | ||
|
|
||
| func TestMultipleOutputter_JSON(t *testing.T) { | ||
| input := []byte(`{ | ||
| "items": [ | ||
| { | ||
| "key": "test-key1", | ||
| "name": "test-name1", | ||
| "other": "another-value2" | ||
| }, | ||
| { | ||
| "key": "test-key2", | ||
| "name": "test-name2", | ||
| "other": "another-value2" | ||
| } | ||
| ] | ||
| }`) | ||
| output, err := output.CmdOutput( | ||
| "json", | ||
| output.NewMultipleOutputterFn(input), | ||
| ) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.JSONEq(t, output, string(input)) | ||
| } | ||
|
|
||
| func TestMultipleOutputter_String(t *testing.T) { | ||
| input := []byte(`{ | ||
| "items": [ | ||
| { | ||
| "key": "test-key1", | ||
| "name": "test-name1", | ||
| "other": "another-value2" | ||
| }, | ||
| { | ||
| "key": "test-key2", | ||
| "name": "test-name2", | ||
| "other": "another-value2" | ||
| } | ||
| ] | ||
| }`) | ||
| expected := "* test-name1 (test-key1)\n* test-name2 (test-key2)" | ||
| output, err := output.CmdOutput( | ||
| "plaintext", | ||
| output.NewMultipleOutputterFn(input), | ||
| ) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, expected, output) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is the implementation. Another PR will add it for all endpoints and add functionality to work for a response with multiple resources.