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 <xiaolunwen@gmail.com>
Reviewed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
6543 2020-09-27 12:07:46 +00:00
parent 887495f38f
commit cf2c18c32b
9 changed files with 62 additions and 27 deletions

View File

@ -22,6 +22,7 @@ import (
"code.gitea.io/tea/modules/utils" "code.gitea.io/tea/modules/utils"
"github.com/muesli/termenv" "github.com/muesli/termenv"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.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") 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,
}
}

View File

@ -60,6 +60,20 @@ var StateFlag = cli.StringFlag{
DefaultText: "open", 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 // LoginOutputFlags defines login and output flags that should
// added to all subcommands and appended to the flags of the // added to all subcommands and appended to the flags of the
// subcommand to work around issue and provide --login and --output: // 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. // IssuePRFlags defines flags that should be available on issue & pr listing flags.
var IssuePRFlags = append([]cli.Flag{ var IssuePRFlags = append([]cli.Flag{
&StateFlag, &StateFlag,
&PaginationPageFlag,
&PaginationLimitFlag,
}, AllDefaultFlags...) }, AllDefaultFlags...)
// initCommand returns repository and *Login based on flags // initCommand returns repository and *Login based on flags

View File

@ -84,6 +84,7 @@ func runIssuesList(ctx *cli.Context) error {
} }
issues, _, err := login.Client().ListRepoIssues(owner, repo, gitea.ListIssueOption{ issues, _, err := login.Client().ListRepoIssues(owner, repo, gitea.ListIssueOption{
ListOptions: getListOptions(ctx),
State: state, State: state,
Type: gitea.IssueTypeIssue, Type: gitea.IssueTypeIssue,
}) })

View File

@ -35,6 +35,8 @@ var CmdLabels = cli.Command{
Aliases: []string{"s"}, Aliases: []string{"s"},
Usage: "Save all the labels as a file", Usage: "Save all the labels as a file",
}, },
&PaginationPageFlag,
&PaginationLimitFlag,
}, AllDefaultFlags...), }, AllDefaultFlags...),
} }
@ -50,7 +52,7 @@ func runLabels(ctx *cli.Context) error {
var values [][]string 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -34,6 +34,8 @@ var CmdMilestonesIssues = cli.Command{
Name: "kind", Name: "kind",
Usage: "Filter by kind (issue|pull)", Usage: "Filter by kind (issue|pull)",
}, },
&PaginationPageFlag,
&PaginationLimitFlag,
}, AllDefaultFlags...), }, AllDefaultFlags...),
} }
@ -89,6 +91,7 @@ func runMilestoneIssueList(ctx *cli.Context) error {
} }
issues, _, err := client.ListRepoIssues(owner, repo, gitea.ListIssueOption{ issues, _, err := client.ListRepoIssues(owner, repo, gitea.ListIssueOption{
ListOptions: getListOptions(ctx),
Milestones: []string{milestone}, Milestones: []string{milestone},
Type: kind, Type: kind,
State: state, State: state,

View File

@ -43,6 +43,8 @@ var CmdMilestonesList = cli.Command{
Usage: "Filter by milestone state (all|open|closed)", Usage: "Filter by milestone state (all|open|closed)",
DefaultText: "open", DefaultText: "open",
}, },
&PaginationPageFlag,
&PaginationLimitFlag,
}, AllDefaultFlags...), }, AllDefaultFlags...),
} }
@ -86,6 +88,7 @@ func runMilestonesList(ctx *cli.Context) error {
} }
milestones, _, err := login.Client().ListRepoMilestones(owner, repo, gitea.ListMilestoneOption{ milestones, _, err := login.Client().ListRepoMilestones(owner, repo, gitea.ListMilestoneOption{
ListOptions: getListOptions(ctx),
State: state, State: state,
}) })

View File

@ -34,17 +34,8 @@ var CmdNotifications = cli.Command{
Aliases: []string{"pd"}, Aliases: []string{"pd"},
Usage: "show pinned notifications instead unread", Usage: "show pinned notifications instead unread",
}, },
&cli.IntFlag{ &PaginationPageFlag,
Name: "page", &PaginationLimitFlag,
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",
},
}, AllDefaultFlags...), }, AllDefaultFlags...),
} }
@ -52,9 +43,9 @@ func runNotifications(ctx *cli.Context) error {
var news []*gitea.NotificationThread var news []*gitea.NotificationThread
var err error var err error
listOpts := gitea.ListOptions{ listOpts := getListOptions(ctx)
Page: ctx.Int("page"), if listOpts.Page == 0 {
PageSize: ctx.Int("limit"), listOpts.Page = 1
} }
var status []gitea.NotifyStatus var status []gitea.NotifyStatus

View File

@ -39,13 +39,16 @@ var CmdReleaseList = cli.Command{
Usage: "List Releases", Usage: "List Releases",
Description: "List Releases", Description: "List Releases",
Action: runReleases, Action: runReleases,
Flags: AllDefaultFlags, Flags: append([]cli.Flag{
&PaginationPageFlag,
&PaginationLimitFlag,
}, AllDefaultFlags...),
} }
func runReleases(ctx *cli.Context) error { func runReleases(ctx *cli.Context) error {
login, owner, repo := initCommand() 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -59,6 +59,8 @@ var CmdReposList = cli.Command{
Required: false, Required: false,
Usage: "Filter archived repos (true|false)", Usage: "Filter archived repos (true|false)",
}, },
&PaginationPageFlag,
&PaginationLimitFlag,
}, LoginOutputFlags...), }, LoginOutputFlags...),
} }
@ -189,6 +191,7 @@ func runReposList(ctx *cli.Context) error {
} }
rps, _, err := client.SearchRepos(gitea.SearchRepoOptions{ rps, _, err := client.SearchRepos(gitea.SearchRepoOptions{
ListOptions: getListOptions(ctx),
OwnerID: ownerID, OwnerID: ownerID,
IsPrivate: isPrivate, IsPrivate: isPrivate,
IsArchived: isArchived, IsArchived: isArchived,