-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: show pagination #261
Changes from 3 commits
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 |
---|---|---|
|
@@ -3,6 +3,9 @@ package output | |
import ( | ||
"encoding/json" | ||
"fmt" | ||
"math" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
|
@@ -45,35 +48,60 @@ func CmdOutput(action string, outputKind string, input []byte) (string, error) { | |
// no success message | ||
} | ||
|
||
if isMultipleResponse { | ||
if len(maybeResources.Items) == 0 { | ||
return "No items found", nil | ||
} | ||
if !isMultipleResponse { | ||
return plaintextOutput(SingularPlaintextOutputFn(maybeResource), successMessage+" "), nil | ||
} | ||
|
||
// the response could have various properties we want to show | ||
keyExists := func(key string) bool { _, ok := maybeResources.Items[0][key]; return ok } | ||
outputFn := MultiplePlaintextOutputFn | ||
switch { | ||
case keyExists("email"): | ||
outputFn = MultipleEmailPlaintextOutputFn | ||
case keyExists("_id"): | ||
outputFn = MultipleIDPlaintextOutputFn | ||
} | ||
if len(maybeResources.Items) == 0 { | ||
return "No items found", nil | ||
} | ||
|
||
items := make([]string, 0, len(maybeResources.Items)) | ||
for _, i := range maybeResources.Items { | ||
items = append(items, outputFn(i)) | ||
} | ||
// the response could have various properties we want to show | ||
keyExists := func(key string) bool { _, ok := maybeResources.Items[0][key]; return ok } | ||
outputFn := MultiplePlaintextOutputFn | ||
switch { | ||
case keyExists("email"): | ||
outputFn = MultipleEmailPlaintextOutputFn | ||
case keyExists("_id"): | ||
outputFn = MultipleIDPlaintextOutputFn | ||
} | ||
|
||
return plaintextOutput("\n"+strings.Join(items, "\n"), successMessage), nil | ||
items := make([]string, 0, len(maybeResources.Items)) | ||
for _, i := range maybeResources.Items { | ||
items = append(items, outputFn(i)) | ||
} | ||
|
||
return plaintextOutput(SingularPlaintextOutputFn(maybeResource), successMessage), nil | ||
var ( | ||
pagination string | ||
limit int | ||
offset int | ||
) | ||
self, ok := maybeResources.Links["self"] | ||
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 gets the pagination values from the HATEOAS "self" link. |
||
if ok && maybeResources.TotalCount > 0 { | ||
selfURL, _ := url.Parse(self["href"]) | ||
limit, _ = strconv.Atoi(selfURL.Query().Get("limit")) | ||
offset, _ = strconv.Atoi(selfURL.Query().Get("offset")) | ||
maxResults := int(math.Min(float64(offset+limit), float64(maybeResources.TotalCount))) | ||
pagination = fmt.Sprintf( | ||
"\nShowing results %d - %d of %d.", | ||
offset+1, | ||
maxResults, | ||
maybeResources.TotalCount, | ||
) | ||
if offset+limit < maybeResources.TotalCount { | ||
pagination += fmt.Sprintf(" Use --offset %d for additional results.", offset+limit) | ||
} | ||
} | ||
|
||
if successMessage != "" { | ||
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. I had to do some newline + space manipulation to make things work for a single response and multiple responses. We should refactor this to make it easier to reason about. |
||
successMessage += "\n" | ||
} | ||
return plaintextOutput(strings.Join(items, "\n"), successMessage) + pagination, nil | ||
} | ||
|
||
func plaintextOutput(out string, successMessage string) string { | ||
if successMessage != "" { | ||
return fmt.Sprintf("%s %s", successMessage, out) | ||
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.
Reversed this check to remove the indentation.