From cf2c18c32b2440e35db6d6dd488d6eb9ea1cf9e9 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Sun, 27 Sep 2020 12:07:46 +0000 Subject: [PATCH] Add Pagination Options for List Subcomands (#204) Add Pagination Options for List subcomands Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/204 Reviewed-by: Lunny Xiao Reviewed-by: Norwin --- cmd/config.go | 13 +++++++++++++ cmd/flags.go | 16 ++++++++++++++++ cmd/issues.go | 5 +++-- cmd/labels.go | 4 +++- cmd/milestone_issues.go | 9 ++++++--- cmd/milestones.go | 5 ++++- cmd/notifications.go | 19 +++++-------------- cmd/releases.go | 7 +++++-- cmd/repos.go | 11 +++++++---- 9 files changed, 62 insertions(+), 27 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 472e1e7..875f245 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -22,6 +22,7 @@ import ( "code.gitea.io/tea/modules/utils" "github.com/muesli/termenv" + "github.com/urfave/cli/v2" "gopkg.in/yaml.v2" ) @@ -271,3 +272,15 @@ func curGitRepoPath(path string) (*Login, string, error) { return nil, "", errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository") } + +func getListOptions(ctx *cli.Context) gitea.ListOptions { + page := ctx.Int("page") + limit := ctx.Int("limit") + if limit != 0 && page == 0 { + page = 1 + } + return gitea.ListOptions{ + Page: page, + PageSize: limit, + } +} diff --git a/cmd/flags.go b/cmd/flags.go index 2ee623e..133e12f 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -60,6 +60,20 @@ var StateFlag = cli.StringFlag{ DefaultText: "open", } +// PaginationPageFlag provides flag for pagination options +var PaginationPageFlag = cli.StringFlag{ + Name: "page", + Aliases: []string{"p"}, + Usage: "specify page, default is 1", +} + +// PaginationLimitFlag provides flag for pagination options +var PaginationLimitFlag = cli.StringFlag{ + Name: "limit", + Aliases: []string{"lm"}, + Usage: "specify limit of items per page", +} + // LoginOutputFlags defines login and output flags that should // added to all subcommands and appended to the flags of the // subcommand to work around issue and provide --login and --output: @@ -91,6 +105,8 @@ var AllDefaultFlags = append([]cli.Flag{ // IssuePRFlags defines flags that should be available on issue & pr listing flags. var IssuePRFlags = append([]cli.Flag{ &StateFlag, + &PaginationPageFlag, + &PaginationLimitFlag, }, AllDefaultFlags...) // initCommand returns repository and *Login based on flags diff --git a/cmd/issues.go b/cmd/issues.go index 848bd4f..75ba0de 100644 --- a/cmd/issues.go +++ b/cmd/issues.go @@ -84,8 +84,9 @@ func runIssuesList(ctx *cli.Context) error { } issues, _, err := login.Client().ListRepoIssues(owner, repo, gitea.ListIssueOption{ - State: state, - Type: gitea.IssueTypeIssue, + ListOptions: getListOptions(ctx), + State: state, + Type: gitea.IssueTypeIssue, }) if err != nil { diff --git a/cmd/labels.go b/cmd/labels.go index d7a3317..6daf842 100644 --- a/cmd/labels.go +++ b/cmd/labels.go @@ -35,6 +35,8 @@ var CmdLabels = cli.Command{ Aliases: []string{"s"}, Usage: "Save all the labels as a file", }, + &PaginationPageFlag, + &PaginationLimitFlag, }, AllDefaultFlags...), } @@ -50,7 +52,7 @@ func runLabels(ctx *cli.Context) error { var values [][]string - labels, _, err := login.Client().ListRepoLabels(owner, repo, gitea.ListLabelsOptions{}) + labels, _, err := login.Client().ListRepoLabels(owner, repo, gitea.ListLabelsOptions{ListOptions: getListOptions(ctx)}) if err != nil { log.Fatal(err) } diff --git a/cmd/milestone_issues.go b/cmd/milestone_issues.go index 419c85c..0043356 100644 --- a/cmd/milestone_issues.go +++ b/cmd/milestone_issues.go @@ -34,6 +34,8 @@ var CmdMilestonesIssues = cli.Command{ Name: "kind", Usage: "Filter by kind (issue|pull)", }, + &PaginationPageFlag, + &PaginationLimitFlag, }, AllDefaultFlags...), } @@ -89,9 +91,10 @@ func runMilestoneIssueList(ctx *cli.Context) error { } issues, _, err := client.ListRepoIssues(owner, repo, gitea.ListIssueOption{ - Milestones: []string{milestone}, - Type: kind, - State: state, + ListOptions: getListOptions(ctx), + Milestones: []string{milestone}, + Type: kind, + State: state, }) if err != nil { return err diff --git a/cmd/milestones.go b/cmd/milestones.go index 82858b1..311cf11 100644 --- a/cmd/milestones.go +++ b/cmd/milestones.go @@ -43,6 +43,8 @@ var CmdMilestonesList = cli.Command{ Usage: "Filter by milestone state (all|open|closed)", DefaultText: "open", }, + &PaginationPageFlag, + &PaginationLimitFlag, }, AllDefaultFlags...), } @@ -86,7 +88,8 @@ func runMilestonesList(ctx *cli.Context) error { } milestones, _, err := login.Client().ListRepoMilestones(owner, repo, gitea.ListMilestoneOption{ - State: state, + ListOptions: getListOptions(ctx), + State: state, }) if err != nil { diff --git a/cmd/notifications.go b/cmd/notifications.go index fcf25bd..28eddc7 100644 --- a/cmd/notifications.go +++ b/cmd/notifications.go @@ -34,17 +34,8 @@ var CmdNotifications = cli.Command{ Aliases: []string{"pd"}, Usage: "show pinned notifications instead unread", }, - &cli.IntFlag{ - Name: "page", - Aliases: []string{"p"}, - Usage: "specify page, default is 1", - Value: 1, - }, - &cli.IntFlag{ - Name: "limit", - Aliases: []string{"lm"}, - Usage: "specify limit of items per page", - }, + &PaginationPageFlag, + &PaginationLimitFlag, }, AllDefaultFlags...), } @@ -52,9 +43,9 @@ func runNotifications(ctx *cli.Context) error { var news []*gitea.NotificationThread var err error - listOpts := gitea.ListOptions{ - Page: ctx.Int("page"), - PageSize: ctx.Int("limit"), + listOpts := getListOptions(ctx) + if listOpts.Page == 0 { + listOpts.Page = 1 } var status []gitea.NotifyStatus diff --git a/cmd/releases.go b/cmd/releases.go index 6d55df3..506664c 100644 --- a/cmd/releases.go +++ b/cmd/releases.go @@ -39,13 +39,16 @@ var CmdReleaseList = cli.Command{ Usage: "List Releases", Description: "List Releases", Action: runReleases, - Flags: AllDefaultFlags, + Flags: append([]cli.Flag{ + &PaginationPageFlag, + &PaginationLimitFlag, + }, AllDefaultFlags...), } func runReleases(ctx *cli.Context) error { login, owner, repo := initCommand() - releases, _, err := login.Client().ListReleases(owner, repo, gitea.ListReleasesOptions{}) + releases, _, err := login.Client().ListReleases(owner, repo, gitea.ListReleasesOptions{ListOptions: getListOptions(ctx)}) if err != nil { log.Fatal(err) } diff --git a/cmd/repos.go b/cmd/repos.go index 75e0644..33ca875 100644 --- a/cmd/repos.go +++ b/cmd/repos.go @@ -59,6 +59,8 @@ var CmdReposList = cli.Command{ Required: false, Usage: "Filter archived repos (true|false)", }, + &PaginationPageFlag, + &PaginationLimitFlag, }, LoginOutputFlags...), } @@ -189,10 +191,11 @@ func runReposList(ctx *cli.Context) error { } rps, _, err := client.SearchRepos(gitea.SearchRepoOptions{ - OwnerID: ownerID, - IsPrivate: isPrivate, - IsArchived: isArchived, - Type: mode, + ListOptions: getListOptions(ctx), + OwnerID: ownerID, + IsPrivate: isPrivate, + IsArchived: isArchived, + Type: mode, }) if err != nil { return err