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
41 changed files with 221 additions and 225 deletions
Showing only changes of commit ad203ea8a5 - Show all commits

View File

@ -9,49 +9,32 @@ import (
"github.com/urfave/cli/v2"
)
// create global variables for global Flags to simplify
// access to the options without requiring cli.Context
var (
// GlobalLoginValue contain value of --login|-l arg
GlobalLoginValue string
// GlobalRepoValue contain value of --repo|-r arg
GlobalRepoValue string
// GlobalOutputValue contain value of --output|-o arg
GlobalOutputValue string
// GlobalRemoteValue contain value of --remote|-R arg
GlobalRemoteValue string
)
// LoginFlag provides flag to specify tea login profile
var LoginFlag = cli.StringFlag{
Name: "login",
Aliases: []string{"l"},
Usage: "Use a different Gitea login. Optional",
Destination: &GlobalLoginValue,
Name: "login",
Aliases: []string{"l"},
Usage: "Use a different Gitea Login. Optional",
}
// RepoFlag provides flag to specify repository
var RepoFlag = cli.StringFlag{
Name: "repo",
Aliases: []string{"r"},
Usage: "Override local repository path or gitea repository slug to interact with. Optional",
Destination: &GlobalRepoValue,
Name: "repo",
Aliases: []string{"r"},
Usage: "Override local repository path or gitea repository slug to interact with. Optional",
}
// RemoteFlag provides flag to specify remote repository
var RemoteFlag = cli.StringFlag{
Name: "remote",
Aliases: []string{"R"},
Usage: "Discover Gitea login from remote. Optional",
Destination: &GlobalRemoteValue,
Name: "remote",
Aliases: []string{"R"},
Usage: "Discover Gitea login from remote. Optional",
}
// OutputFlag provides flag to specify output type
var OutputFlag = cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "Output format. (csv, simple, table, tsv, yaml)",
Destination: &GlobalOutputValue,
Name: "output",
Aliases: []string{"o"},
Usage: "Output format. (csv, simple, table, tsv, yaml)",
}
// StateFlag provides flag to specify issue/pr state, defaulting to "open"

View File

@ -33,19 +33,19 @@ var CmdIssues = cli.Command{
func runIssues(ctx *cli.Context) error {
if ctx.Args().Len() == 1 {
return runIssueDetail(ctx.Args().First())
return runIssueDetail(ctx, ctx.Args().First())
}
return issues.RunIssuesList(ctx)
}
func runIssueDetail(index string) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runIssueDetail(cmd *cli.Context, index string) error {
ctx := config.InitCommand(cmd)
idx, err := utils.ArgToIndex(index)
if err != nil {
return err
}
issue, _, err := login.Client().GetIssue(owner, repo, idx)
issue, _, err := ctx.Login.Client().GetIssue(ctx.Owner, ctx.Repo, idx)
if err != nil {
return err
}

View File

@ -30,8 +30,8 @@ var CmdIssuesClose = cli.Command{
}
// editIssueState abstracts the arg parsing to edit the given issue
func editIssueState(ctx *cli.Context, opts gitea.EditIssueOption) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func editIssueState(cmd *cli.Context, opts gitea.EditIssueOption) error {
ctx := config.InitCommand(cmd)
if ctx.Args().Len() == 0 {
log.Fatal(ctx.Command.ArgsUsage)
}
@ -41,7 +41,7 @@ func editIssueState(ctx *cli.Context, opts gitea.EditIssueOption) error {
return err
}
issue, _, err := login.Client().EditIssue(owner, repo, index, opts)
issue, _, err := ctx.Login.Client().EditIssue(ctx.Owner, ctx.Repo, index, opts)
if err != nil {
return err
}

View File

@ -36,10 +36,10 @@ var CmdIssuesCreate = cli.Command{
}, flags.LoginRepoFlags...),
}
func runIssuesCreate(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runIssuesCreate(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
issue, _, err := login.Client().CreateIssue(owner, repo, gitea.CreateIssueOption{
issue, _, err := ctx.Login.Client().CreateIssue(ctx.Owner, ctx.Repo, gitea.CreateIssueOption{
Title: ctx.String("title"),
Body: ctx.String("body"),
// TODO:

View File

@ -26,8 +26,8 @@ var CmdIssuesList = cli.Command{
}
// RunIssuesList list issues
func RunIssuesList(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func RunIssuesList(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
state := gitea.StateOpen
switch ctx.String("state") {
@ -39,8 +39,8 @@ func RunIssuesList(ctx *cli.Context) error {
state = gitea.StateClosed
}
issues, _, err := login.Client().ListRepoIssues(owner, repo, gitea.ListIssueOption{
ListOptions: flags.GetListOptions(ctx),
issues, _, err := ctx.Login.Client().ListRepoIssues(ctx.Owner, ctx.Repo, gitea.ListIssueOption{
ListOptions: flags.GetListOptions(cmd),
State: state,
Type: gitea.IssueTypeIssue,
})
@ -49,6 +49,6 @@ func RunIssuesList(ctx *cli.Context) error {
log.Fatal(err)
}
print.IssuesList(issues, flags.GlobalOutputValue)
print.IssuesList(issues, ctx.Output)
return nil
}

View File

@ -10,7 +10,6 @@ import (
"os"
"strings"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/config"
"code.gitea.io/sdk/gitea"
@ -43,13 +42,13 @@ var CmdLabelCreate = cli.Command{
},
}
func runLabelCreate(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runLabelCreate(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
labelFile := ctx.String("file")
var err error
if len(labelFile) == 0 {
_, _, err = login.Client().CreateLabel(owner, repo, gitea.CreateLabelOption{
_, _, err = ctx.Login.Client().CreateLabel(ctx.Owner, ctx.Repo, gitea.CreateLabelOption{
Name: ctx.String("name"),
Color: ctx.String("color"),
Description: ctx.String("description"),
@ -69,7 +68,7 @@ func runLabelCreate(ctx *cli.Context) error {
if color == "" || name == "" {
log.Printf("Line %d ignored because lack of enough fields: %s\n", i, line)
} else {
_, _, err = login.Client().CreateLabel(owner, repo, gitea.CreateLabelOption{
_, _, err = ctx.Login.Client().CreateLabel(ctx.Owner, ctx.Repo, gitea.CreateLabelOption{
Name: name,
Color: color,
Description: description,

View File

@ -7,7 +7,6 @@ package labels
import (
"log"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/config"
"github.com/urfave/cli/v2"
@ -27,10 +26,10 @@ var CmdLabelDelete = cli.Command{
},
}
func runLabelDelete(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runLabelDelete(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
_, err := login.Client().DeleteLabel(owner, repo, ctx.Int64("id"))
_, err := ctx.Login.Client().DeleteLabel(ctx.Owner, ctx.Repo, ctx.Int64("id"))
if err != nil {
log.Fatal(err)
}

View File

@ -35,10 +35,12 @@ var CmdLabelsList = cli.Command{
}
// RunLabelsList list labels.
func RunLabelsList(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
labels, _, err := login.Client().ListRepoLabels(owner, repo, gitea.ListLabelsOptions{ListOptions: flags.GetListOptions(ctx)})
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),
})
if err != nil {
log.Fatal(err)
}
@ -47,6 +49,6 @@ func RunLabelsList(ctx *cli.Context) error {
return task.LabelsExport(labels, ctx.String("save"))
}
print.LabelsList(labels, flags.GlobalOutputValue)
print.LabelsList(labels, ctx.Output)
return nil
}

View File

@ -7,7 +7,6 @@ package labels
import (
"log"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/config"
"code.gitea.io/sdk/gitea"
@ -40,8 +39,8 @@ var CmdLabelUpdate = cli.Command{
},
}
func runLabelUpdate(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runLabelUpdate(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
id := ctx.Int64("id")
var pName, pColor, pDescription *string
@ -61,7 +60,7 @@ func runLabelUpdate(ctx *cli.Context) error {
}
var err error
_, _, err = login.Client().EditLabel(owner, repo, id, gitea.EditLabelOption{
_, _, err = ctx.Login.Client().EditLabel(ctx.Owner, ctx.Repo, id, gitea.EditLabelOption{
Name: pName,
Color: pColor,
Description: pDescription,

View File

@ -7,7 +7,6 @@ package cmd
import (
"fmt"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/cmd/login"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/print"
@ -45,6 +44,6 @@ func runLoginDetail(name string) error {
return nil
}
print.LoginDetails(l, flags.GlobalOutputValue)
print.LoginDetails(l)
return nil
}

View File

@ -25,12 +25,11 @@ var CmdLoginList = cli.Command{
}
// RunLoginList list all logins
func RunLoginList(_ *cli.Context) error {
func RunLoginList(cmd *cli.Context) error {
logins, err := config.GetLogins()
if err != nil {
log.Fatal(err)
}
print.LoginsList(logins, flags.GlobalOutputValue)
print.LoginsList(logins, cmd.String("output"))
return nil
}

View File

@ -34,16 +34,16 @@ var CmdMilestones = cli.Command{
func runMilestones(ctx *cli.Context) error {
if ctx.Args().Len() == 1 {
return runMilestoneDetail(ctx.Args().First())
return runMilestoneDetail(ctx, ctx.Args().First())
}
return milestones.RunMilestonesList(ctx)
}
func runMilestoneDetail(name string) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func runMilestoneDetail(cmd *cli.Context, name string) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
milestone, _, err := client.GetMilestoneByName(owner, repo, name)
milestone, _, err := client.GetMilestoneByName(ctx.Owner, ctx.Repo, name)
if err != nil {
return err
}

View File

@ -41,8 +41,8 @@ var CmdMilestonesCreate = cli.Command{
}, flags.AllDefaultFlags...),
}
func runMilestonesCreate(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runMilestonesCreate(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
title := ctx.String("title")
if len(title) == 0 {
@ -55,7 +55,7 @@ func runMilestonesCreate(ctx *cli.Context) error {
state = gitea.StateClosed
}
mile, _, err := login.Client().CreateMilestone(owner, repo, gitea.CreateMilestoneOption{
mile, _, err := ctx.Login.Client().CreateMilestone(ctx.Owner, ctx.Repo, gitea.CreateMilestoneOption{
Title: title,
Description: ctx.String("description"),
State: state,

View File

@ -22,10 +22,10 @@ var CmdMilestonesDelete = cli.Command{
Flags: flags.AllDefaultFlags,
}
func deleteMilestone(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func deleteMilestone(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
_, err := client.DeleteMilestoneByName(owner, repo, ctx.Args().First())
_, err := client.DeleteMilestoneByName(ctx.Owner, ctx.Repo, ctx.Args().First())
return err
}

View File

@ -65,9 +65,9 @@ var CmdMilestoneRemoveIssue = cli.Command{
Flags: flags.AllDefaultFlags,
}
func runMilestoneIssueList(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func runMilestoneIssueList(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
state := gitea.StateOpen
switch ctx.String("state") {
@ -89,13 +89,13 @@ func runMilestoneIssueList(ctx *cli.Context) error {
milestone := ctx.Args().First()
// make sure milestone exist
_, _, err := client.GetMilestoneByName(owner, repo, milestone)
_, _, err := client.GetMilestoneByName(ctx.Owner, ctx.Repo, milestone)
if err != nil {
return err
}
issues, _, err := client.ListRepoIssues(owner, repo, gitea.ListIssueOption{
ListOptions: flags.GetListOptions(ctx),
issues, _, err := client.ListRepoIssues(ctx.Owner, ctx.Repo, gitea.ListIssueOption{
ListOptions: flags.GetListOptions(cmd),
Milestones: []string{milestone},
Type: kind,
State: state,
@ -104,13 +104,13 @@ func runMilestoneIssueList(ctx *cli.Context) error {
return err
}
print.IssuesPullsList(issues, flags.GlobalOutputValue)
print.IssuesPullsList(issues, ctx.Output)
return nil
}
func runMilestoneIssueAdd(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func runMilestoneIssueAdd(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
if ctx.Args().Len() == 0 {
return fmt.Errorf("need two arguments")
}
@ -123,20 +123,20 @@ func runMilestoneIssueAdd(ctx *cli.Context) error {
}
// make sure milestone exist
mile, _, err := client.GetMilestoneByName(owner, repo, mileName)
mile, _, err := client.GetMilestoneByName(ctx.Owner, ctx.Repo, mileName)
if err != nil {
return err
}
_, _, err = client.EditIssue(owner, repo, idx, gitea.EditIssueOption{
_, _, err = client.EditIssue(ctx.Owner, ctx.Repo, idx, gitea.EditIssueOption{
Milestone: &mile.ID,
})
return err
}
func runMilestoneIssueRemove(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func runMilestoneIssueRemove(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
if ctx.Args().Len() == 0 {
return fmt.Errorf("need two arguments")
}
@ -148,7 +148,7 @@ func runMilestoneIssueRemove(ctx *cli.Context) error {
return err
}
issue, _, err := client.GetIssue(owner, repo, idx)
issue, _, err := client.GetIssue(ctx.Owner, ctx.Repo, idx)
if err != nil {
return err
}
@ -162,7 +162,7 @@ func runMilestoneIssueRemove(ctx *cli.Context) error {
}
zero := int64(0)
_, _, err = client.EditIssue(owner, repo, idx, gitea.EditIssueOption{
_, _, err = client.EditIssue(ctx.Owner, ctx.Repo, idx, gitea.EditIssueOption{
Milestone: &zero,
})
return err

View File

@ -34,8 +34,8 @@ var CmdMilestonesList = cli.Command{
}
// RunMilestonesList list milestones
func RunMilestonesList(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func RunMilestonesList(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
state := gitea.StateOpen
switch ctx.String("state") {
@ -45,8 +45,9 @@ func RunMilestonesList(ctx *cli.Context) error {
state = gitea.StateClosed
}
milestones, _, err := login.Client().ListRepoMilestones(owner, repo, gitea.ListMilestoneOption{
ListOptions: flags.GetListOptions(ctx),
client := ctx.Login.Client()
milestones, _, err := client.ListRepoMilestones(ctx.Owner, ctx.Repo, gitea.ListMilestoneOption{
ListOptions: flags.GetListOptions(cmd),
State: state,
})
@ -54,6 +55,6 @@ func RunMilestonesList(ctx *cli.Context) error {
log.Fatal(err)
}
print.MilestonesList(milestones, flags.GlobalOutputValue, state)
print.MilestonesList(milestones, ctx.Output, state)
return nil
}

View File

@ -25,15 +25,15 @@ var CmdMilestonesReopen = cli.Command{
Flags: flags.AllDefaultFlags,
}
func editMilestoneStatus(ctx *cli.Context, close bool) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func editMilestoneStatus(cmd *cli.Context, close bool) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
state := gitea.StateOpen
if close {
state = gitea.StateClosed
}
_, _, err := client.EditMilestoneByName(owner, repo, ctx.Args().First(), gitea.EditMilestoneOption{
_, _, err := client.EditMilestoneByName(ctx.Owner, ctx.Repo, ctx.Args().First(), gitea.EditMilestoneOption{
State: &state,
Title: ctx.Args().First(),
})

View File

@ -43,13 +43,14 @@ var CmdNotifications = cli.Command{
}, flags.AllDefaultFlags...),
}
func runNotifications(ctx *cli.Context) error {
func runNotifications(cmd *cli.Context) error {
var news []*gitea.NotificationThread
var err error
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
listOpts := flags.GetListOptions(ctx)
listOpts := flags.GetListOptions(cmd)
if listOpts.Page == 0 {
listOpts.Page = 1
}
@ -63,12 +64,12 @@ func runNotifications(ctx *cli.Context) error {
}
if ctx.Bool("all") {
news, _, err = login.Client().ListNotifications(gitea.ListNotificationOptions{
news, _, err = client.ListNotifications(gitea.ListNotificationOptions{
ListOptions: listOpts,
Status: status,
})
} else {
news, _, err = login.Client().ListRepoNotifications(owner, repo, gitea.ListNotificationOptions{
news, _, err = client.ListRepoNotifications(ctx.Owner, ctx.Repo, gitea.ListNotificationOptions{
ListOptions: listOpts,
Status: status,
})
@ -77,6 +78,6 @@ func runNotifications(ctx *cli.Context) error {
log.Fatal(err)
}
print.NotificationsList(news, flags.GlobalOutputValue, ctx.Bool("all"))
print.NotificationsList(news, ctx.Output, ctx.Bool("all"))
return nil
}

View File

@ -26,8 +26,8 @@ var CmdOpen = cli.Command{
Flags: append([]cli.Flag{}, flags.LoginRepoFlags...),
}
func runOpen(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runOpen(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
var suffix string
number := ctx.Args().Get(0)
@ -73,7 +73,7 @@ func runOpen(ctx *cli.Context) error {
suffix = number
}
u := path.Join(login.URL, owner, repo, suffix)
u := path.Join(ctx.Login.URL, ctx.RepoSlug, suffix)
err := open.Run(u)
if err != nil {
log.Fatal(err)

View File

@ -7,7 +7,6 @@ package organizations
import (
"log"
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/modules/config"
"github.com/urfave/cli/v2"
)
@ -23,10 +22,10 @@ var CmdOrganizationDelete = cli.Command{
}
// RunOrganizationDelete delete user organization
func RunOrganizationDelete(ctx *cli.Context) error {
login, _, _ := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func RunOrganizationDelete(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := login.Client()
client := ctx.Login.Client()
if ctx.Args().Len() < 1 {
log.Fatal("You have to specify the organization name you want to delete.")

View File

@ -29,17 +29,18 @@ var CmdOrganizationList = cli.Command{
}
// RunOrganizationList list user organizations
func RunOrganizationList(ctx *cli.Context) error {
login, _, _ := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func RunOrganizationList(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
client := login.Client()
userOrganizations, _, err := client.ListUserOrgs(login.User, gitea.ListOrgsOptions{ListOptions: flags.GetListOptions(ctx)})
userOrganizations, _, err := client.ListUserOrgs(ctx.Login.User, gitea.ListOrgsOptions{
ListOptions: flags.GetListOptions(cmd),
})
if err != nil {
log.Fatal(err)
}
print.OrganizationsList(userOrganizations, flags.GlobalOutputValue)
print.OrganizationsList(userOrganizations, ctx.Output)
return nil
}

View File

@ -36,25 +36,25 @@ var CmdPulls = cli.Command{
func runPulls(ctx *cli.Context) error {
if ctx.Args().Len() == 1 {
return runPullDetail(ctx.Args().First())
return runPullDetail(ctx, ctx.Args().First())
}
return pulls.RunPullsList(ctx)
}
func runPullDetail(index string) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runPullDetail(cmd *cli.Context, index string) error {
ctx := config.InitCommand(cmd)
idx, err := utils.ArgToIndex(index)
if err != nil {
return err
}
client := login.Client()
pr, _, err := client.GetPullRequest(owner, repo, idx)
client := ctx.Login.Client()
pr, _, err := client.GetPullRequest(ctx.Owner, ctx.Repo, idx)
if err != nil {
return err
}
reviews, _, err := client.ListPullReviews(owner, repo, idx, gitea.ListPullReviewsOptions{})
reviews, _, err := client.ListPullReviews(ctx.Owner, ctx.Repo, idx, gitea.ListPullReviewsOptions{})
if err != nil {
fmt.Printf("error while loading reviews: %v\n", err)
}

View File

@ -26,8 +26,8 @@ var CmdPullsCheckout = cli.Command{
Flags: flags.AllDefaultFlags,
}
func runPullsCheckout(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runPullsCheckout(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
if ctx.Args().Len() != 1 {
log.Fatal("Must specify a PR index")
}
@ -36,5 +36,5 @@ func runPullsCheckout(ctx *cli.Context) error {
return err
}
return task.PullCheckout(login, owner, repo, idx, interact.PromptPassword)
return task.PullCheckout(ctx.Login, ctx.Owner, ctx.Repo, idx, interact.PromptPassword)
}

View File

@ -31,8 +31,8 @@ var CmdPullsClean = cli.Command{
}, flags.AllDefaultFlags...),
}
func runPullsClean(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runPullsClean(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
if ctx.Args().Len() != 1 {
return fmt.Errorf("Must specify a PR index")
}
@ -42,5 +42,5 @@ func runPullsClean(ctx *cli.Context) error {
return err
}
return task.PullClean(login, owner, repo, idx, ctx.Bool("ignore-sha"), interact.PromptPassword)
return task.PullClean(ctx.Login, ctx.Owner, ctx.Repo, idx, ctx.Bool("ignore-sha"), interact.PromptPassword)
}

View File

@ -42,19 +42,19 @@ var CmdPullsCreate = cli.Command{
}, flags.AllDefaultFlags...),
}
func runPullsCreate(ctx *cli.Context) error {
login, ownerArg, repoArg := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runPullsCreate(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
// no args -> interactive mode
if ctx.NumFlags() == 0 {
return interact.CreatePull(login, ownerArg, repoArg)
return interact.CreatePull(ctx.Login, ctx.Owner, ctx.Repo)
}
// else use args to create PR
return task.CreatePull(
login,
ownerArg,
repoArg,
ctx.Login,
ctx.Owner,
ctx.Repo,
ctx.String("base"),
ctx.String("head"),
ctx.String("title"),

View File

@ -26,8 +26,8 @@ var CmdPullsList = cli.Command{
}
// RunPullsList return list of pulls
func RunPullsList(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func RunPullsList(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
state := gitea.StateOpen
switch ctx.String("state") {
@ -39,7 +39,7 @@ func RunPullsList(ctx *cli.Context) error {
state = gitea.StateClosed
}
prs, _, err := login.Client().ListRepoPullRequests(owner, repo, gitea.ListPullRequestsOptions{
prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{
State: state,
})
@ -47,6 +47,6 @@ func RunPullsList(ctx *cli.Context) error {
log.Fatal(err)
}
print.PullsList(prs, flags.GlobalOutputValue)
print.PullsList(prs, ctx.Output)
return nil
}

View File

@ -61,10 +61,10 @@ var CmdReleaseCreate = cli.Command{
}, flags.AllDefaultFlags...),
}
func runReleaseCreate(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runReleaseCreate(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
release, resp, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
release, resp, err := ctx.Login.Client().CreateRelease(ctx.Owner, ctx.Repo, gitea.CreateReleaseOption{
TagName: ctx.String("tag"),
Target: ctx.String("target"),
Title: ctx.String("title"),
@ -90,7 +90,7 @@ func runReleaseCreate(ctx *cli.Context) error {
filePath := filepath.Base(asset)
if _, _, err = login.Client().CreateReleaseAttachment(owner, repo, release.ID, file, filePath); err != nil {
if _, _, err = ctx.Login.Client().CreateReleaseAttachment(ctx.Owner, ctx.Repo, release.ID, file, filePath); err != nil {
file.Close()
log.Fatal(err)
}

View File

@ -33,9 +33,9 @@ var CmdReleaseDelete = cli.Command{
}, flags.AllDefaultFlags...),
}
func runReleaseDelete(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func runReleaseDelete(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
tag := ctx.Args().First()
if len(tag) == 0 {
@ -48,7 +48,7 @@ func runReleaseDelete(ctx *cli.Context) error {
return nil
}
release, err := getReleaseByTag(owner, repo, tag, client)
release, err := getReleaseByTag(ctx.Owner, ctx.Repo, tag, client)
if err != nil {
return err
}
@ -56,13 +56,13 @@ func runReleaseDelete(ctx *cli.Context) error {
return nil
}
_, err = client.DeleteRelease(owner, repo, release.ID)
_, err = client.DeleteRelease(ctx.Owner, ctx.Repo, release.ID)
if err != nil {
return err
}
if ctx.Bool("delete-tag") {
_, err = client.DeleteReleaseTag(owner, repo, tag)
_, err = client.DeleteReleaseTag(ctx.Owner, ctx.Repo, tag)
return err
}

View File

@ -56,9 +56,9 @@ var CmdReleaseEdit = cli.Command{
}, flags.AllDefaultFlags...),
}
func runReleaseEdit(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func runReleaseEdit(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
tag := ctx.Args().First()
if len(tag) == 0 {
@ -66,7 +66,7 @@ func runReleaseEdit(ctx *cli.Context) error {
return nil
}
release, err := getReleaseByTag(owner, repo, tag, client)
release, err := getReleaseByTag(ctx.Owner, ctx.Repo, tag, client)
if err != nil {
return err
}
@ -82,7 +82,7 @@ func runReleaseEdit(ctx *cli.Context) error {
isPre = gitea.OptionalBool(strings.ToLower(ctx.String("prerelease"))[:1] == "t")
}
_, _, err = client.EditRelease(owner, repo, release.ID, gitea.EditReleaseOption{
_, _, err = client.EditRelease(ctx.Owner, ctx.Repo, release.ID, gitea.EditReleaseOption{
TagName: ctx.String("tag"),
Target: ctx.String("target"),
Title: ctx.String("title"),

View File

@ -30,15 +30,17 @@ var CmdReleaseList = cli.Command{
}
// RunReleasesList list releases
func RunReleasesList(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func RunReleasesList(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
releases, _, err := login.Client().ListReleases(owner, repo, gitea.ListReleasesOptions{ListOptions: flags.GetListOptions(ctx)})
releases, _, err := ctx.Login.Client().ListReleases(ctx.Owner, ctx.Repo, gitea.ListReleasesOptions{
ListOptions: flags.GetListOptions(cmd),
})
if err != nil {
log.Fatal(err)
}
print.ReleasesList(releases, flags.GlobalOutputValue)
print.ReleasesList(releases, ctx.Output)
return nil
}

View File

@ -5,7 +5,6 @@
package cmd
import (
"code.gitea.io/tea/cmd/flags"
"code.gitea.io/tea/cmd/repos"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/print"
@ -33,20 +32,20 @@ var CmdRepos = cli.Command{
func runRepos(ctx *cli.Context) error {
if ctx.Args().Len() == 1 {
return runRepoDetail(ctx.Args().First())
return runRepoDetail(ctx, ctx.Args().First())
}
return repos.RunReposList(ctx)
}
func runRepoDetail(path string) error {
login, ownerFallback, _ := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
repoOwner, repoName := utils.GetOwnerAndRepo(path, ownerFallback)
func runRepoDetail(cmd *cli.Context, path string) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
repoOwner, repoName := utils.GetOwnerAndRepo(path, ctx.Owner)
repo, _, err := client.GetRepo(repoOwner, repoName)
if err != nil {
return err
}
topics, _, err := client.ListRepoTopics(repo.Owner.UserName, repo.Name, gitea.ListRepoTopicsOptions{})
topics, _, err := client.ListRepoTopics(repoOwner, repoName, gitea.ListRepoTopicsOptions{})
if err != nil {
return err
}

View File

@ -82,9 +82,9 @@ var CmdRepoCreate = cli.Command{
}, flags.LoginOutputFlags...),
}
func runRepoCreate(ctx *cli.Context) error {
login, _, _ := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func runRepoCreate(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
var (
repo *gitea.Repository
err error

View File

@ -44,11 +44,11 @@ var CmdReposList = cli.Command{
}
// RunReposList list repositories
func RunReposList(ctx *cli.Context) error {
login, _, _ := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func RunReposList(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
typeFilter, err := getTypeFilter(ctx)
typeFilter, err := getTypeFilter(cmd)
if err != nil {
return err
}
@ -60,14 +60,14 @@ func RunReposList(ctx *cli.Context) error {
return err
}
rps, _, err = client.SearchRepos(gitea.SearchRepoOptions{
ListOptions: flags.GetListOptions(ctx),
ListOptions: flags.GetListOptions(cmd),
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(ctx),
ListOptions: flags.GetListOptions(cmd),
})
}
@ -80,7 +80,7 @@ func RunReposList(ctx *cli.Context) error {
reposFiltered = filterReposByType(rps, typeFilter)
}
print.ReposList(reposFiltered, flags.GlobalOutputValue, getFields(ctx))
print.ReposList(reposFiltered, ctx.Output, getFields(cmd))
return nil
}

View File

@ -56,9 +56,9 @@ var CmdReposSearch = cli.Command{
}, flags.LoginOutputFlags...),
}
func runReposSearch(ctx *cli.Context) error {
login, _, _ := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func runReposSearch(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
var ownerID int64
if ctx.IsSet("owner") {
@ -93,7 +93,7 @@ func runReposSearch(ctx *cli.Context) error {
isPrivate = &private
}
mode, err := getTypeFilter(ctx)
mode, err := getTypeFilter(cmd)
if err != nil {
return err
}
@ -109,7 +109,7 @@ func runReposSearch(ctx *cli.Context) error {
}
rps, _, err := client.SearchRepos(gitea.SearchRepoOptions{
ListOptions: flags.GetListOptions(ctx),
ListOptions: flags.GetListOptions(cmd),
OwnerID: ownerID,
IsPrivate: isPrivate,
IsArchived: isArchived,
@ -123,6 +123,6 @@ func runReposSearch(ctx *cli.Context) error {
return err
}
print.ReposList(rps, flags.GlobalOutputValue, getFields(ctx))
print.ReposList(rps, ctx.Output, getFields(cmd))
return nil
}

View File

@ -18,7 +18,7 @@ var CmdTrackedTimes = cli.Command{
Depending on your permissions on the repository, only your own tracked
times might be listed.`,
ArgsUsage: "[username | #issue]",
Action: runTrackedTimes,
Action: times.RunTimesList,
Subcommands: []*cli.Command{
&times.CmdTrackedTimesAdd,
&times.CmdTrackedTimesDelete,
@ -26,7 +26,3 @@ var CmdTrackedTimes = cli.Command{
&times.CmdTrackedTimesList,
},
}
func runTrackedTimes(ctx *cli.Context) error {
return times.RunTimesList(ctx)
}

View File

@ -31,8 +31,8 @@ var CmdTrackedTimesAdd = cli.Command{
Flags: flags.LoginRepoFlags,
}
func runTrackedTimesAdd(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
func runTrackedTimesAdd(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
if ctx.Args().Len() < 2 {
return fmt.Errorf("No issue or duration specified.\nUsage:\t%s", ctx.Command.UsageText)
@ -48,7 +48,7 @@ func runTrackedTimesAdd(ctx *cli.Context) error {
log.Fatal(err)
}
_, _, err = login.Client().AddTime(owner, repo, issue, gitea.AddTimeOption{
_, _, err = ctx.Login.Client().AddTime(ctx.Owner, ctx.Repo, issue, gitea.AddTimeOption{
Time: int64(duration.Seconds()),
})
if err != nil {

View File

@ -26,9 +26,9 @@ var CmdTrackedTimesDelete = cli.Command{
Flags: flags.LoginRepoFlags,
}
func runTrackedTimesDelete(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func runTrackedTimesDelete(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
if ctx.Args().Len() < 2 {
return fmt.Errorf("No issue or time ID specified.\nUsage:\t%s", ctx.Command.UsageText)
@ -44,7 +44,7 @@ func runTrackedTimesDelete(ctx *cli.Context) error {
log.Fatal(err)
}
_, err = client.DeleteTime(owner, repo, issue, timeID)
_, err = client.DeleteTime(ctx.Owner, ctx.Repo, issue, timeID)
if err != nil {
log.Fatal(err)
}

View File

@ -50,9 +50,9 @@ var CmdTrackedTimesList = cli.Command{
}
// RunTimesList list repositories
func RunTimesList(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func RunTimesList(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
var times []*gitea.TrackedTime
var err error
@ -61,17 +61,17 @@ func RunTimesList(ctx *cli.Context) error {
fmt.Println(ctx.Command.ArgsUsage)
if user == "" {
// get all tracked times on the repo
times, _, err = client.GetRepoTrackedTimes(owner, repo)
times, _, err = client.GetRepoTrackedTimes(ctx.Owner, ctx.Repo)
} else if strings.HasPrefix(user, "#") {
// get all tracked times on the specified issue
issue, err := utils.ArgToIndex(user)
if err != nil {
return err
}
times, _, err = client.ListTrackedTimes(owner, repo, issue, gitea.ListTrackedTimesOptions{})
times, _, err = client.ListTrackedTimes(ctx.Owner, ctx.Repo, issue, gitea.ListTrackedTimesOptions{})
} else {
// get all tracked times by the specified user
times, _, err = client.GetUserTrackedTimes(owner, repo, user)
times, _, err = client.GetUserTrackedTimes(ctx.Owner, ctx.Repo, user)
}
if err != nil {
@ -92,6 +92,6 @@ func RunTimesList(ctx *cli.Context) error {
}
}
print.TrackedTimesList(times, flags.GlobalOutputValue, from, until, ctx.Bool("total"))
print.TrackedTimesList(times, ctx.Output, from, until, ctx.Bool("total"))
return nil
}

View File

@ -25,9 +25,9 @@ var CmdTrackedTimesReset = cli.Command{
Flags: flags.LoginRepoFlags,
}
func runTrackedTimesReset(ctx *cli.Context) error {
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
client := login.Client()
func runTrackedTimesReset(cmd *cli.Context) error {
ctx := config.InitCommand(cmd)
client := ctx.Login.Client()
if ctx.Args().Len() != 1 {
return fmt.Errorf("No issue specified.\nUsage:\t%s", ctx.Command.UsageText)
@ -38,7 +38,7 @@ func runTrackedTimesReset(ctx *cli.Context) error {
log.Fatal(err)
}
_, err = client.ResetIssueTime(owner, repo, issue)
_, err = client.ResetIssueTime(ctx.Owner, ctx.Repo, issue)
if err != nil {
log.Fatal(err)
}

View File

@ -14,18 +14,34 @@ import (
"code.gitea.io/tea/modules/utils"
gogit "github.com/go-git/go-git/v5"
"github.com/urfave/cli/v2"
)
// TeaContext contains all context derived during command initalization.
// and wraps cli.Context
type TeaContext struct {
*cli.Context
Login *Login
RepoSlug string // <owner>/<repo>
Owner string // repo owner as derived from context
Repo string // repo name as derived from context or provided in flag
Output string // value of output flag
LocalRepo *git.TeaRepo // maybe, we have opened it already anyway
}
// 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
// command flags. If a local git repo can't be found, repo slug values are unset.
func InitCommand(repoFlag, loginFlag, remoteFlag string) (login *Login, owner string, reponame string) {
func InitCommand(ctx *cli.Context) *TeaContext {
err := loadConfig()
if err != nil {
log.Fatal(err)
}
repoFlag := ctx.String("repo")
loginFlag := ctx.String("login")
remoteFlag := ctx.String("remote")
var repoSlug string
var repoPath string // empty means PWD
var repoFlagPathExists bool
@ -42,7 +58,7 @@ func InitCommand(repoFlag, loginFlag, remoteFlag string) (login *Login, owner st
}
// try to read git repo & extract context, ignoring if PWD is not a repo
login, repoSlug, err = contextFromLocalRepo(repoPath, remoteFlag)
localRepo, login, repoSlug, err := contextFromLocalRepo(repoPath, remoteFlag)
if err != nil && err != gogit.ErrRepositoryNotExists {
log.Fatal(err.Error())
}
@ -65,24 +81,25 @@ func InitCommand(repoFlag, loginFlag, remoteFlag string) (login *Login, owner st
}
// parse reposlug (owner falling back to login owner if reposlug contains only repo name)
owner, reponame = utils.GetOwnerAndRepo(repoSlug, login.User)
return
owner, reponame := utils.GetOwnerAndRepo(repoSlug, login.User)
return &TeaContext{ctx, login, repoSlug, owner, reponame, ctx.String("output"), localRepo}
}
// contextFromLocalRepo discovers login & repo slug from the default branch remote of the given local repo
func contextFromLocalRepo(repoValue, remoteValue string) (*Login, string, error) {
func contextFromLocalRepo(repoValue, remoteValue string) (*git.TeaRepo, *Login, string, error) {
repo, err := git.RepoFromPath(repoValue)
if err != nil {
return nil, "", err
return nil, nil, "", err
}
gitConfig, err := repo.Config()
if err != nil {
return nil, "", err
return repo, nil, "", err
}
// if no remote
if len(gitConfig.Remotes) == 0 {
return nil, "", errors.New("No remote(s) found in this Git repository")
return repo, nil, "", errors.New("No remote(s) found in this Git repository")
}
// if only one remote exists
@ -103,28 +120,28 @@ func contextFromLocalRepo(repoValue, remoteValue string) (*Login, string, error)
remoteConfig, ok := gitConfig.Remotes[remoteValue]
if !ok || remoteConfig == nil {
return nil, "", errors.New("Remote " + remoteValue + " not found in this Git repository")
return repo, nil, "", errors.New("Remote " + remoteValue + " not found in this Git repository")
}
for _, l := range config.Logins {
for _, u := range remoteConfig.URLs {
p, err := git.ParseURL(strings.TrimSpace(u))
if err != nil {
return nil, "", fmt.Errorf("Git remote URL parse failed: %s", err.Error())
return repo, nil, "", fmt.Errorf("Git remote URL parse failed: %s", err.Error())
}
if strings.EqualFold(p.Scheme, "http") || strings.EqualFold(p.Scheme, "https") {
if strings.HasPrefix(u, l.URL) {
ps := strings.Split(p.Path, "/")
path := strings.Join(ps[len(ps)-2:], "/")
return &l, strings.TrimSuffix(path, ".git"), nil
return repo, &l, strings.TrimSuffix(path, ".git"), nil
}
} else if strings.EqualFold(p.Scheme, "ssh") {
if l.GetSSHHost() == strings.Split(p.Host, ":")[0] {
return &l, strings.TrimLeft(strings.TrimSuffix(p.Path, ".git"), "/"), nil
return repo, &l, strings.TrimLeft(strings.TrimSuffix(p.Path, ".git"), "/"), nil
}
}
}
}
return nil, "", errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository")
return repo, nil, "", errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository")
}

View File

@ -13,7 +13,7 @@ import (
)
// LoginDetails print login entry to stdout
func LoginDetails(login *config.Login, output string) {
func LoginDetails(login *config.Login) {
in := fmt.Sprintf("# %s\n\n[@%s](%s/%s)\n",
login.Name,
login.User,