replace flag globals, require context for commands #291

Merged
6543 merged 11 commits from noerw/tea:refactor-global-flags into master 2020-12-15 17:38:23 +00:00
11 changed files with 24 additions and 24 deletions
Showing only changes of commit c1fb506797 - Show all commits

View File

@ -5,7 +5,6 @@
package flags
import (
"code.gitea.io/sdk/gitea"
"github.com/urfave/cli/v2"
)
@ -92,16 +91,3 @@ var IssuePRFlags = append([]cli.Flag{
&PaginationPageFlag,
&PaginationLimitFlag,
}, AllDefaultFlags...)
// GetListOptions return ListOptions based on PaginationFlags
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

@ -40,7 +40,7 @@ func RunIssuesList(cmd *cli.Context) error {
}
issues, _, err := ctx.Login.Client().ListRepoIssues(ctx.Owner, ctx.Repo, gitea.ListIssueOption{
ListOptions: flags.GetListOptions(cmd),
ListOptions: ctx.GetListOptions(),
State: state,
Type: gitea.IssueTypeIssue,
})

View File

@ -39,7 +39,7 @@ func RunLabelsList(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
labels, _, err := client.ListRepoLabels(ctx.Owner, ctx.Repo, gitea.ListLabelsOptions{
ListOptions: flags.GetListOptions(cmd),
ListOptions: ctx.GetListOptions(),
})
if err != nil {
log.Fatal(err)

View File

@ -95,7 +95,7 @@ func runMilestoneIssueList(cmd *cli.Context) error {
}
issues, _, err := client.ListRepoIssues(ctx.Owner, ctx.Repo, gitea.ListIssueOption{
ListOptions: flags.GetListOptions(cmd),
ListOptions: ctx.GetListOptions(),
Milestones: []string{milestone},
Type: kind,
State: state,

View File

@ -47,7 +47,7 @@ func RunMilestonesList(cmd *cli.Context) error {
client := ctx.Login.Client()
milestones, _, err := client.ListRepoMilestones(ctx.Owner, ctx.Repo, gitea.ListMilestoneOption{
ListOptions: flags.GetListOptions(cmd),
ListOptions: ctx.GetListOptions(),
State: state,
})

View File

@ -50,7 +50,7 @@ func runNotifications(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
listOpts := flags.GetListOptions(cmd)
listOpts := ctx.GetListOptions()
if listOpts.Page == 0 {
listOpts.Page = 1
}

View File

@ -34,7 +34,7 @@ func RunOrganizationList(cmd *cli.Context) error {
client := ctx.Login.Client()
userOrganizations, _, err := client.ListUserOrgs(ctx.Login.User, gitea.ListOrgsOptions{
ListOptions: flags.GetListOptions(cmd),
ListOptions: ctx.GetListOptions(),
})
if err != nil {
log.Fatal(err)

View File

@ -34,7 +34,7 @@ func RunReleasesList(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
releases, _, err := ctx.Login.Client().ListReleases(ctx.Owner, ctx.Repo, gitea.ListReleasesOptions{
ListOptions: flags.GetListOptions(cmd),
ListOptions: ctx.GetListOptions(),
})
if err != nil {
log.Fatal(err)

View File

@ -60,14 +60,14 @@ func RunReposList(cmd *cli.Context) error {
return err
}
rps, _, err = client.SearchRepos(gitea.SearchRepoOptions{
ListOptions: flags.GetListOptions(cmd),
ListOptions: ctx.GetListOptions(),
StarredByUserID: user.ID,
})
} else if ctx.Bool("watched") {
rps, _, err = client.GetMyWatchedRepos() // TODO: this does not expose pagination..
} else {
rps, _, err = client.ListMyRepos(gitea.ListReposOptions{
ListOptions: flags.GetListOptions(cmd),
ListOptions: ctx.GetListOptions(),
})
}

View File

@ -109,7 +109,7 @@ func runReposSearch(cmd *cli.Context) error {
}
rps, _, err := client.SearchRepos(gitea.SearchRepoOptions{
ListOptions: flags.GetListOptions(cmd),
ListOptions: ctx.GetListOptions(),
OwnerID: ownerID,
IsPrivate: isPrivate,
IsArchived: isArchived,

View File

@ -10,6 +10,7 @@ import (
"log"
"strings"
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/modules/git"
"code.gitea.io/tea/modules/utils"
@ -29,6 +30,19 @@ type TeaContext struct {
LocalRepo *git.TeaRepo // maybe, we have opened it already anyway
}
// GetListOptions return ListOptions based on PaginationFlags
func (ctx *TeaContext) GetListOptions() gitea.ListOptions {
page := ctx.Int("page")
limit := ctx.Int("limit")
if limit != 0 && page == 0 {
page = 1
}
return gitea.ListOptions{
Page: page,
PageSize: limit,
}
}
// InitCommand resolves the application context, and returns the active login, and if
// available the repo slug. It does this by reading the config file for logins, parsing
// the remotes of the .git repo specified in repoFlag or $PWD, and using overrides from