Skip to content

Commit

Permalink
feat: Add cursor based listing
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyLV committed Jun 4, 2024
1 parent 5f08e08 commit f1e7b1b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
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

0 comments on commit f1e7b1b

Please sign in to comment.