Skip to content
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: Add cursor based listing #139

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ var keyListCmd = &cobra.Command{
keyListOpts.FilterUntranslated = "1"
}

return repeatableList(
func(p int64) {
keyListOpts.Page = uint(p)
return repeatableCursorList(
func(cursor string) {
keyListOpts.Pagination = lokalise.PaginationCursor
keyListOpts.Cursor = cursor
k.SetListOptions(keyListOpts)
},
func() (lokalise.PageCounter, error) {
func() (lokalise.CursorPager, error) {
return k.List(projectId)
},
)
Expand Down
37 changes: 34 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package cmd
import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/lokalise/go-lokalise-api/v4"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"log"
"os"
)

const (
Expand Down Expand Up @@ -101,6 +100,12 @@ func printPageHeader(cur, total int64) {
}
}

func printCursorHeader(currentCursor string, nextCursor string) {
if viper.GetBool("debug") {
fmt.Printf("\n=============\n Cursor %s, next %s \n-------------\n", currentCursor, nextCursor)
}
}

// handy function for processing List response for all commands
func repeatableList(
forwardPage func(page int64),
Expand Down Expand Up @@ -133,6 +138,32 @@ func repeatableList(
return nil
}

// handy function for processing List response using cursor pagination for all commands
func repeatableCursorList(
forwardCursor func(cursor string),
list func() (lokalise.CursorPager, error),
) error {
cursor := ""

for {
forwardCursor(cursor)
resp, err := list()
if err != nil {
return err
}

printCursorHeader(cursor, resp.NextCursor())
_ = printJson(resp)

if !resp.HasNextCursor() {
break
}
cursor = resp.NextCursor()
}

return nil
}

// checkFlag checks if flag with a given name was among the ones being activated
func checkFlag(fs *pflag.FlagSet, name string) (wasSet bool) {
fs.Visit(func(f *pflag.Flag) {
Expand Down
9 changes: 5 additions & 4 deletions cmd/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ var translationListCmd = &cobra.Command{
t := Api.Translations()
translationListOpts.Limit = t.ListOpts().Limit

return repeatableList(
func(p int64) {
translationListOpts.Page = uint(p)
return repeatableCursorList(
func(cursor string) {
translationListOpts.Pagination = lokalise.PaginationCursor
translationListOpts.Cursor = cursor
t.SetListOptions(translationListOpts)
},
func() (lokalise.PageCounter, error) {
func() (lokalise.CursorPager, error) {
return t.List(projectId)
},
)
Expand Down
Loading