diff --git a/cmd/key.go b/cmd/key.go index 72e8df1..05960b3 100644 --- a/cmd/key.go +++ b/cmd/key.go @@ -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) }, ) diff --git a/cmd/root.go b/cmd/root.go index fd310a9..56aac5c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 ( @@ -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), @@ -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) { diff --git a/cmd/translation.go b/cmd/translation.go index 145249e..8d15421 100644 --- a/cmd/translation.go +++ b/cmd/translation.go @@ -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) }, )