-
Notifications
You must be signed in to change notification settings - Fork 13
feat: show successful resource delete message #212
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
Changes from all commits
c676ad3
49fb577
34931e3
809ebaf
b5d30ed
7231107
ae363fd
6f59084
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,10 +42,10 @@ func (c MembersClient) Create(ctx context.Context, accessToken string, baseURI s | |
| if err != nil { | ||
| return nil, errors.NewLDAPIError(err) | ||
| } | ||
| memberJson, err := json.Marshal(members.Items) | ||
| membersJson, err := json.Marshal(members) | ||
|
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. Updated this for consistency with other responses for multiple resources. |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return memberJson, nil | ||
| return membersJson, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,10 +60,6 @@ type resources struct { | |
| Items []resource `json:"items"` | ||
| } | ||
|
|
||
| // resourcesBare is for responses that return a list of resources at the top level of the response, | ||
| // not as a value of an "items" property. | ||
| type resourcesBare []resource | ||
|
|
||
| // CmdOutputSingular builds a command response based on the flag the user provided and the shape of | ||
| // the input. The expected shape is a single JSON object. | ||
| func CmdOutputSingular(outputKind string, input []byte, fn PlaintextOutputFn) (string, error) { | ||
|
|
@@ -73,67 +69,19 @@ func CmdOutputSingular(outputKind string, input []byte, fn PlaintextOutputFn) (s | |
| return "", err | ||
| } | ||
|
|
||
| return outputFromKind(outputKind, "", SingularOutputter{ | ||
| outputFn: fn, | ||
| resource: r, | ||
| resourceJSON: input, | ||
| }) | ||
| } | ||
|
|
||
| // CmdOutputCreate builds a command response based on the flag the user provided and the shape of | ||
| // the input with a successfully created message. The expected shape is a single JSON object. | ||
| func CmdOutputCreate(outputKind string, input []byte, fn PlaintextOutputFn) (string, error) { | ||
|
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. Removed these to have a single |
||
| return cmdOutputWithMessage(outputKind, "Successfully created ", input, fn) | ||
| } | ||
|
|
||
| // CmdOutputUpdate builds a command response based on the flag the user provided and the shape of | ||
| // the input with a successfully created message. The expected shape is a single JSON object. | ||
| func CmdOutputUpdate(outputKind string, input []byte, fn PlaintextOutputFn) (string, error) { | ||
| return cmdOutputWithMessage(outputKind, "Successfully updated ", input, fn) | ||
| } | ||
|
|
||
| func cmdOutputWithMessage(outputKind string, message string, input []byte, fn PlaintextOutputFn) (string, error) { | ||
| var r resource | ||
| err := json.Unmarshal(input, &r) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return outputFromKind(outputKind, message, SingularOutputter{ | ||
| return outputFromKind(outputKind, SingularOutputter{ | ||
| outputFn: fn, | ||
| resource: r, | ||
| resourceJSON: input, | ||
| }) | ||
| } | ||
|
|
||
| // CmdOutputMultiple builds a command response based on the flag the user provided and the shape of | ||
| // the input. The expected shape is a list of JSON objects. | ||
| func CmdOutputMultiple(outputKind string, input []byte, fn PlaintextOutputFn) (string, error) { | ||
|
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. This is now unused. |
||
| var r resources | ||
| err := json.Unmarshal(input, &r) | ||
| if err != nil { | ||
| // sometimes a response doesn't include each item in an "items" property | ||
| var rr resourcesBare | ||
| err := json.Unmarshal(input, &rr) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| r.Items = rr | ||
| } | ||
|
|
||
| return outputFromKind(outputKind, "", MultipleOutputter{ | ||
| outputFn: fn, | ||
| resources: r, | ||
| resourceJSON: input, | ||
| }) | ||
| } | ||
|
|
||
| func outputFromKind(outputKind string, additional string, o Outputter) (string, error) { | ||
| func outputFromKind(outputKind string, o Outputter) (string, error) { | ||
| switch outputKind { | ||
| case "json": | ||
| return o.JSON(), nil | ||
| case "plaintext": | ||
| return additional + o.String(), nil | ||
| return o.String(), nil | ||
| } | ||
|
|
||
| return "", ErrInvalidOutputKind | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package output | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // CmdOutput returns a response from a resource create action formatted based on the | ||
| // output flag along with an optional message based on the action. | ||
| func CmdOutput(action string, outputKind string, input []byte) (string, error) { | ||
|
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. This is the "do all the things" function. |
||
| if outputKind == "json" { | ||
| return string(input), nil | ||
| } | ||
|
|
||
| var ( | ||
| maybeResource resource | ||
| maybeResources resources | ||
| isMultipleResponse bool | ||
| ) | ||
|
|
||
| // unmarshal either a singular resource or a list of them | ||
| err := json.Unmarshal(input, &maybeResource) | ||
| _, isMultipleResponse = maybeResource["items"] | ||
| if err != nil || isMultipleResponse { | ||
| err := json.Unmarshal(input, &maybeResources) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| } | ||
|
|
||
| var successMessage string | ||
| switch action { | ||
| case "create": | ||
| successMessage = "Successfully created" | ||
| case "delete": | ||
| successMessage = "Successfully deleted" | ||
| case "update": | ||
| successMessage = "Successfully updated" | ||
| default: | ||
| // no success message | ||
| } | ||
|
|
||
| if isMultipleResponse { | ||
| // the response could have various properties we want to show | ||
| outputFn := MultiplePlaintextOutputFn | ||
| if _, ok := maybeResources.Items[0]["email"]; ok { | ||
| outputFn = MultipleEmailPlaintextOutputFn | ||
| } | ||
|
|
||
| items := make([]string, 0, len(maybeResources.Items)) | ||
| for _, i := range maybeResources.Items { | ||
| items = append(items, outputFn(i)) | ||
| } | ||
|
|
||
| return plaintextOutput("\n"+strings.Join(items, "\n"), successMessage), nil | ||
| } | ||
|
|
||
| return plaintextOutput(SingularPlaintextOutputFn(maybeResource), successMessage), nil | ||
| } | ||
|
|
||
| func plaintextOutput(out string, successMessage string) string { | ||
| if successMessage != "" { | ||
| return fmt.Sprintf("%s %s", successMessage, out) | ||
| } | ||
|
|
||
| return out | ||
| } | ||
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.
We need to know if the plaintext output should include a success message, so we pass in this value.