replace flag globals, require context for commands #291
|
@ -40,6 +40,7 @@ func runIssues(ctx *cli.Context) error {
|
|||
|
||||
func runIssueDetail(cmd *cli.Context, index string) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
idx, err := utils.ArgToIndex(index)
|
||||
if err != nil {
|
||||
|
|
|
@ -32,6 +32,7 @@ var CmdIssuesClose = cli.Command{
|
|||
// editIssueState abstracts the arg parsing to edit the given issue
|
||||
func editIssueState(cmd *cli.Context, opts gitea.EditIssueOption) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
if ctx.Args().Len() == 0 {
|
||||
log.Fatal(ctx.Command.ArgsUsage)
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ var CmdIssuesCreate = cli.Command{
|
|||
|
||||
func runIssuesCreate(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
issue, _, err := ctx.Login.Client().CreateIssue(ctx.Owner, ctx.Repo, gitea.CreateIssueOption{
|
||||
Title: ctx.String("title"),
|
||||
|
|
|
@ -28,6 +28,7 @@ var CmdIssuesList = cli.Command{
|
|||
// RunIssuesList list issues
|
||||
func RunIssuesList(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
state := gitea.StateOpen
|
||||
switch ctx.String("state") {
|
||||
|
|
|
@ -44,6 +44,7 @@ var CmdLabelCreate = cli.Command{
|
|||
|
||||
func runLabelCreate(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
labelFile := ctx.String("file")
|
||||
var err error
|
||||
|
|
|
@ -28,6 +28,7 @@ var CmdLabelDelete = cli.Command{
|
|||
|
||||
func runLabelDelete(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
_, err := ctx.Login.Client().DeleteLabel(ctx.Owner, ctx.Repo, ctx.Int64("id"))
|
||||
if err != nil {
|
||||
|
|
|
@ -37,6 +37,8 @@ var CmdLabelsList = cli.Command{
|
|||
// RunLabelsList list labels.
|
||||
func RunLabelsList(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
client := ctx.Login.Client()
|
||||
labels, _, err := client.ListRepoLabels(ctx.Owner, ctx.Repo, gitea.ListLabelsOptions{
|
||||
ListOptions: ctx.GetListOptions(),
|
||||
|
|
|
@ -41,6 +41,7 @@ var CmdLabelUpdate = cli.Command{
|
|||
|
||||
func runLabelUpdate(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
id := ctx.Int64("id")
|
||||
var pName, pColor, pDescription *string
|
||||
|
|
|
@ -41,6 +41,7 @@ func runMilestones(ctx *cli.Context) error {
|
|||
|
||||
func runMilestoneDetail(cmd *cli.Context, name string) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
|
||||
milestone, _, err := client.GetMilestoneByName(ctx.Owner, ctx.Repo, name)
|
||||
|
|
|
@ -43,6 +43,7 @@ var CmdMilestonesCreate = cli.Command{
|
|||
|
||||
func runMilestonesCreate(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
title := ctx.String("title")
|
||||
if len(title) == 0 {
|
||||
|
|
|
@ -24,6 +24,7 @@ var CmdMilestonesDelete = cli.Command{
|
|||
|
||||
func deleteMilestone(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
|
||||
_, err := client.DeleteMilestoneByName(ctx.Owner, ctx.Repo, ctx.Args().First())
|
||||
|
|
|
@ -67,6 +67,7 @@ var CmdMilestoneRemoveIssue = cli.Command{
|
|||
|
||||
func runMilestoneIssueList(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
|
||||
state := gitea.StateOpen
|
||||
|
@ -110,6 +111,7 @@ func runMilestoneIssueList(cmd *cli.Context) error {
|
|||
|
||||
func runMilestoneIssueAdd(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
if ctx.Args().Len() == 0 {
|
||||
return fmt.Errorf("need two arguments")
|
||||
|
@ -136,6 +138,7 @@ func runMilestoneIssueAdd(cmd *cli.Context) error {
|
|||
|
||||
func runMilestoneIssueRemove(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
if ctx.Args().Len() == 0 {
|
||||
return fmt.Errorf("need two arguments")
|
||||
|
|
|
@ -36,6 +36,7 @@ var CmdMilestonesList = cli.Command{
|
|||
// RunMilestonesList list milestones
|
||||
func RunMilestonesList(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
state := gitea.StateOpen
|
||||
switch ctx.String("state") {
|
||||
|
|
|
@ -27,6 +27,7 @@ var CmdMilestonesReopen = cli.Command{
|
|||
|
||||
func editMilestoneStatus(cmd *cli.Context, close bool) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
|
||||
state := gitea.StateOpen
|
||||
|
|
|
@ -69,6 +69,7 @@ func runNotifications(cmd *cli.Context) error {
|
|||
Status: status,
|
||||
})
|
||||
} else {
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
news, _, err = client.ListRepoNotifications(ctx.Owner, ctx.Repo, gitea.ListNotificationOptions{
|
||||
ListOptions: listOpts,
|
||||
Status: status,
|
||||
|
|
|
@ -28,6 +28,7 @@ var CmdOpen = cli.Command{
|
|||
|
||||
func runOpen(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
var suffix string
|
||||
number := ctx.Args().Get(0)
|
||||
|
|
|
@ -43,6 +43,7 @@ func runPulls(ctx *cli.Context) error {
|
|||
|
||||
func runPullDetail(cmd *cli.Context, index string) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
idx, err := utils.ArgToIndex(index)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -28,6 +28,7 @@ var CmdPullsCheckout = cli.Command{
|
|||
|
||||
func runPullsCheckout(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{LocalRepo: true})
|
||||
if ctx.Args().Len() != 1 {
|
||||
log.Fatal("Must specify a PR index")
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ var CmdPullsClean = cli.Command{
|
|||
|
||||
func runPullsClean(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{LocalRepo: true})
|
||||
if ctx.Args().Len() != 1 {
|
||||
return fmt.Errorf("Must specify a PR index")
|
||||
}
|
||||
|
|
|
@ -44,6 +44,7 @@ var CmdPullsCreate = cli.Command{
|
|||
|
||||
func runPullsCreate(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{LocalRepo: true})
|
||||
|
||||
// no args -> interactive mode
|
||||
if ctx.NumFlags() == 0 {
|
||||
|
|
|
@ -28,6 +28,7 @@ var CmdPullsList = cli.Command{
|
|||
// RunPullsList return list of pulls
|
||||
func RunPullsList(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
state := gitea.StateOpen
|
||||
switch ctx.String("state") {
|
||||
|
|
|
@ -63,6 +63,7 @@ var CmdReleaseCreate = cli.Command{
|
|||
|
||||
func runReleaseCreate(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
release, resp, err := ctx.Login.Client().CreateRelease(ctx.Owner, ctx.Repo, gitea.CreateReleaseOption{
|
||||
TagName: ctx.String("tag"),
|
||||
|
|
|
@ -35,6 +35,7 @@ var CmdReleaseDelete = cli.Command{
|
|||
|
||||
func runReleaseDelete(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
|
||||
tag := ctx.Args().First()
|
||||
|
|
|
@ -58,6 +58,7 @@ var CmdReleaseEdit = cli.Command{
|
|||
|
||||
func runReleaseEdit(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
|
||||
tag := ctx.Args().First()
|
||||
|
|
|
@ -32,6 +32,7 @@ var CmdReleaseList = cli.Command{
|
|||
// RunReleasesList list releases
|
||||
func RunReleasesList(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
releases, _, err := ctx.Login.Client().ListReleases(ctx.Owner, ctx.Repo, gitea.ListReleasesOptions{
|
||||
ListOptions: ctx.GetListOptions(),
|
||||
|
|
|
@ -33,6 +33,7 @@ var CmdTrackedTimesAdd = cli.Command{
|
|||
|
||||
func runTrackedTimesAdd(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
|
||||
if ctx.Args().Len() < 2 {
|
||||
return fmt.Errorf("No issue or duration specified.\nUsage:\t%s", ctx.Command.UsageText)
|
||||
|
|
|
@ -28,6 +28,7 @@ var CmdTrackedTimesDelete = cli.Command{
|
|||
|
||||
func runTrackedTimesDelete(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
|
||||
if ctx.Args().Len() < 2 {
|
||||
|
|
|
@ -52,6 +52,7 @@ var CmdTrackedTimesList = cli.Command{
|
|||
// RunTimesList list repositories
|
||||
func RunTimesList(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
|
||||
var times []*gitea.TrackedTime
|
||||
|
|
|
@ -27,6 +27,7 @@ var CmdTrackedTimesReset = cli.Command{
|
|||
|
||||
func runTrackedTimesReset(cmd *cli.Context) error {
|
||||
ctx := config.InitCommand(cmd)
|
||||
ctx.Ensure(config.CtxRequirement{RemoteRepo: true})
|
||||
client := ctx.Login.Client()
|
||||
|
||||
if ctx.Args().Len() != 1 {
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
|
@ -43,6 +44,27 @@ func (ctx *TeaContext) GetListOptions() gitea.ListOptions {
|
|||
}
|
||||
}
|
||||
|
||||
// Ensure checks if requirements on the context are set, and terminates otherwise.
|
||||
func (ctx *TeaContext) Ensure(req CtxRequirement) {
|
||||
if req.LocalRepo && ctx.LocalRepo == nil {
|
||||
fmt.Println("Local repository required: Execute from a repo dir, or specify a path with --repo.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if req.RemoteRepo && len(ctx.RepoSlug) == 0 {
|
||||
fmt.Println("Remote repository required: Specify ID via --repo or execute from a local git repo.")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// CtxRequirement specifies context needed for operation
|
||||
type CtxRequirement struct {
|
||||
// ensures a local git repo is available & ctx.LocalRepo is set. Implies .RemoteRepo
|
||||
LocalRepo bool
|
||||
// ensures ctx.RepoSlug, .Owner, .Repo are set
|
||||
RemoteRepo bool
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
Loading…
Reference in New Issue
Block a user