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"
"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,
}
}

View File

@ -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

View File

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

View File

@ -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)
}

View File

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

View File

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

View File

@ -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

View File

@ -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)
}

View File

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