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
30 changed files with 54 additions and 0 deletions
Showing only changes of commit 98cebeb04f - Show all commits

View File

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

View File

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

View File

@ -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"),

View File

@ -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") {

View File

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

View File

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

View File

@ -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(),

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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") {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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") {

View File

@ -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"),

View File

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

View File

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

View File

@ -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(),

View File

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

View File

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

View File

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

View File

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

View File

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