diff --git a/cmd/admin.go b/cmd/admin.go index 6e33a23..71fc92e 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -7,7 +7,7 @@ import ( stdctx "context" "code.gitea.io/tea/cmd/admin/users" - "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/modules/print" "github.com/urfave/cli/v3" ) @@ -44,7 +44,7 @@ var cmdAdminUsers = cli.Command{ } func runAdminUserDetail(_ stdctx.Context, cmd *cli.Command, u string) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) client := ctx.Login.Client() user, _, err := client.GetUserInfo(u) if err != nil { diff --git a/cmd/admin/users/list.go b/cmd/admin/users/list.go index cc831d1..3aeda1b 100644 --- a/cmd/admin/users/list.go +++ b/cmd/admin/users/list.go @@ -6,8 +6,8 @@ package users import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "code.gitea.io/sdk/gitea" @@ -34,7 +34,7 @@ var CmdUserList = cli.Command{ // RunUserList list users func RunUserList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) fields, err := userFieldsFlag.GetValues(cmd) if err != nil { diff --git a/cmd/attachments/create.go b/cmd/attachments/create.go index e970d8f..9f31497 100644 --- a/cmd/attachments/create.go +++ b/cmd/attachments/create.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" @@ -27,7 +28,7 @@ var CmdReleaseAttachmentCreate = cli.Command{ } func runReleaseAttachmentCreate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/attachments/delete.go b/cmd/attachments/delete.go index 9abad42..76119b9 100644 --- a/cmd/attachments/delete.go +++ b/cmd/attachments/delete.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" @@ -32,7 +33,7 @@ var CmdReleaseAttachmentDelete = cli.Command{ } func runReleaseAttachmentDelete(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/attachments/list.go b/cmd/attachments/list.go index be6c142..5857779 100644 --- a/cmd/attachments/list.go +++ b/cmd/attachments/list.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -31,7 +32,7 @@ var CmdReleaseAttachmentList = cli.Command{ // RunReleaseAttachmentList list release attachments func RunReleaseAttachmentList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/base/command.go b/cmd/base/command.go new file mode 100644 index 0000000..6c7f4fb --- /dev/null +++ b/cmd/base/command.go @@ -0,0 +1,105 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package base + +import ( + "fmt" + "log" + "os" + + "code.gitea.io/tea/modules/config" + "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/modules/interact" + "code.gitea.io/tea/modules/utils" + + gogit "github.com/go-git/go-git/v5" + "github.com/urfave/cli/v3" +) + +// 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(cmd *cli.Command) *context.TeaContext { + // these flags are used as overrides to the context detection via local git repo + repoFlag := cmd.String("repo") + loginFlag := cmd.String("login") + remoteFlag := cmd.String("remote") + + var ( + c context.TeaContext + err error + repoPath string // empty means PWD + repoFlagPathExists bool + ) + + // check if repoFlag can be interpreted as path to local repo. + if len(repoFlag) != 0 { + if repoFlagPathExists, err = utils.DirExists(repoFlag); err != nil { + log.Fatal(err.Error()) + } + if repoFlagPathExists { + repoPath = repoFlag + } + } + + if len(remoteFlag) == 0 { + remoteFlag = config.GetPreferences().FlagDefaults.Remote + } + + if repoPath == "" { + if repoPath, err = os.Getwd(); err != nil { + log.Fatal(err.Error()) + } + } + + // try to read local git repo & extract context: if repoFlag specifies a valid path, read repo in that dir, + // otherwise attempt PWD. if no repo is found, continue with default login + if c.LocalRepo, c.Login, c.RepoSlug, err = context.ContextFromLocalRepo(repoPath, remoteFlag); err != nil { + if err == context.ErrNotAGiteaRepo || err == gogit.ErrRepositoryNotExists { + // we can deal with that, commands needing the optional values use ctx.Ensure() + } else { + log.Fatal(err.Error()) + } + } + + if len(repoFlag) != 0 && !repoFlagPathExists { + // if repoFlag is not a valid path, use it to override repoSlug + c.RepoSlug = repoFlag + } + + // override config user with env variable + envLogin := context.GetLoginByEnvVar() + if envLogin != nil { + _, err := utils.ValidateAuthenticationMethod(envLogin.URL, envLogin.Token, "", "", false, "", "") + if err != nil { + log.Fatal(err.Error()) + } + c.Login = envLogin + } + + // override login from flag, or use default login if repo based detection failed + if len(loginFlag) != 0 { + c.Login = config.GetLoginByName(loginFlag) + if c.Login == nil { + log.Fatalf("Login name '%s' does not exist", loginFlag) + } + } else if c.Login == nil { + c.Login, err = interact.LoginSelect() + if err != nil { + // TODO: maybe we can directly start interact.CreateLogin() (only if + // we're sure we can interactively!), as gh cli does. + fmt.Println(`No gitea login configured. To start using tea, first run + tea login add +and then run your command again.`) + os.Exit(1) + } + } + + // parse reposlug (owner falling back to login owner if reposlug contains only repo name) + c.Owner, c.Repo = utils.GetOwnerAndRepo(c.RepoSlug, c.Login.User) + c.Command = cmd + c.Output = cmd.String("output") + return &c +} diff --git a/cmd/branches/list.go b/cmd/branches/list.go index ce47330..d856f5c 100644 --- a/cmd/branches/list.go +++ b/cmd/branches/list.go @@ -6,6 +6,7 @@ package branches import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -38,7 +39,7 @@ var CmdBranchesList = cli.Command{ // RunBranchesList list branches func RunBranchesList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) owner := ctx.Owner @@ -52,7 +53,6 @@ func RunBranchesList(_ stdctx.Context, cmd *cli.Command) error { branches, _, err = ctx.Login.Client().ListRepoBranches(owner, ctx.Repo, gitea.ListRepoBranchesOptions{ ListOptions: flags.GetListOptions(), }) - if err != nil { return err } @@ -60,7 +60,6 @@ func RunBranchesList(_ stdctx.Context, cmd *cli.Command) error { protections, _, err = ctx.Login.Client().ListBranchProtections(owner, ctx.Repo, gitea.ListBranchProtectionsOptions{ ListOptions: flags.GetListOptions(), }) - if err != nil { return err } diff --git a/cmd/branches/protect.go b/cmd/branches/protect.go index 15b988b..816b37a 100644 --- a/cmd/branches/protect.go +++ b/cmd/branches/protect.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" @@ -45,7 +46,7 @@ var CmdBranchesUnprotect = cli.Command{ // RunBranchesProtect function to protect/unprotect a list of branches func RunBranchesProtect(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if !cmd.Args().Present() { diff --git a/cmd/clone.go b/cmd/clone.go index 7acb771..5bd058b 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -7,9 +7,9 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/config" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/debug" "code.gitea.io/tea/modules/git" "code.gitea.io/tea/modules/interact" @@ -48,7 +48,7 @@ When a host is specified in the repo-slug, it will override the login specified } func runRepoClone(ctx stdctx.Context, cmd *cli.Command) error { - teaCmd := context.InitCommand(cmd) + teaCmd := base.InitCommand(cmd) args := teaCmd.Args() if args.Len() < 1 { diff --git a/cmd/comment.go b/cmd/comment.go index a344dcd..dbc744d 100644 --- a/cmd/comment.go +++ b/cmd/comment.go @@ -10,6 +10,7 @@ import ( "io" "strings" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/config" "code.gitea.io/tea/modules/context" @@ -36,7 +37,7 @@ var CmdAddComment = cli.Command{ } func runAddComment(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) args := ctx.Args() diff --git a/cmd/issues.go b/cmd/issues.go index 68dfa59..5d6d940 100644 --- a/cmd/issues.go +++ b/cmd/issues.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/issues" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" @@ -48,7 +49,7 @@ func runIssues(ctx stdctx.Context, cmd *cli.Command) error { } func runIssueDetail(_ stdctx.Context, cmd *cli.Command, index string) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) idx, err := utils.ArgToIndex(index) diff --git a/cmd/issues/close.go b/cmd/issues/close.go index 4a18785..7aeb219 100644 --- a/cmd/issues/close.go +++ b/cmd/issues/close.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -23,7 +24,7 @@ var CmdIssuesClose = cli.Command{ Description: `Change state of one ore more issues to 'closed'`, ArgsUsage: " [...]", Action: func(ctx stdctx.Context, cmd *cli.Command) error { - var s = gitea.StateClosed + s := gitea.StateClosed return editIssueState(ctx, cmd, gitea.EditIssueOption{State: &s}) }, Flags: flags.AllDefaultFlags, @@ -31,7 +32,7 @@ var CmdIssuesClose = cli.Command{ // editIssueState abstracts the arg parsing to edit the given issue func editIssueState(_ stdctx.Context, cmd *cli.Command, opts gitea.EditIssueOption) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if ctx.Args().Len() == 0 { return fmt.Errorf(ctx.Command.ArgsUsage) diff --git a/cmd/issues/create.go b/cmd/issues/create.go index 84593d3..5f4bcf5 100644 --- a/cmd/issues/create.go +++ b/cmd/issues/create.go @@ -6,6 +6,7 @@ package issues import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" @@ -26,7 +27,7 @@ var CmdIssuesCreate = cli.Command{ } func runIssuesCreate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if ctx.NumFlags() == 0 { diff --git a/cmd/issues/edit.go b/cmd/issues/edit.go index 2e49437..5c43c27 100644 --- a/cmd/issues/edit.go +++ b/cmd/issues/edit.go @@ -8,6 +8,7 @@ import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" @@ -30,7 +31,7 @@ use an empty string (eg. --milestone "").`, } func runIssuesEdit(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if !cmd.Args().Present() { diff --git a/cmd/issues/list.go b/cmd/issues/list.go index 3bca2b4..634e592 100644 --- a/cmd/issues/list.go +++ b/cmd/issues/list.go @@ -8,8 +8,8 @@ import ( "fmt" "time" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "code.gitea.io/sdk/gitea" @@ -34,7 +34,7 @@ var CmdIssuesList = cli.Command{ // RunIssuesList list issues func RunIssuesList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) state := gitea.StateOpen switch ctx.String("state") { @@ -97,7 +97,6 @@ func RunIssuesList(_ stdctx.Context, cmd *cli.Command) error { Since: from, Before: until, }) - if err != nil { return err } @@ -116,7 +115,6 @@ func RunIssuesList(_ stdctx.Context, cmd *cli.Command) error { Before: until, Owner: owner, }) - if err != nil { return err } diff --git a/cmd/labels/create.go b/cmd/labels/create.go index ec72b32..961c1f2 100644 --- a/cmd/labels/create.go +++ b/cmd/labels/create.go @@ -10,6 +10,7 @@ import ( "os" "strings" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" @@ -46,7 +47,7 @@ var CmdLabelCreate = cli.Command{ } func runLabelCreate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) labelFile := ctx.String("file") @@ -65,7 +66,7 @@ func runLabelCreate(_ stdctx.Context, cmd *cli.Command) error { defer f.Close() scanner := bufio.NewScanner(f) - var i = 1 + i := 1 for scanner.Scan() { line := scanner.Text() color, name, description := splitLabelLine(line) diff --git a/cmd/labels/delete.go b/cmd/labels/delete.go index 0719fef..ca8fd20 100644 --- a/cmd/labels/delete.go +++ b/cmd/labels/delete.go @@ -6,6 +6,7 @@ package labels import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" @@ -29,7 +30,7 @@ var CmdLabelDelete = cli.Command{ } func runLabelDelete(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) _, err := ctx.Login.Client().DeleteLabel(ctx.Owner, ctx.Repo, ctx.Int64("id")) diff --git a/cmd/labels/list.go b/cmd/labels/list.go index fcbe90d..6dd0932 100644 --- a/cmd/labels/list.go +++ b/cmd/labels/list.go @@ -6,6 +6,7 @@ package labels import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -36,7 +37,7 @@ var CmdLabelsList = cli.Command{ // RunLabelsList list labels. func RunLabelsList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/labels/update.go b/cmd/labels/update.go index 3f20862..c051ce6 100644 --- a/cmd/labels/update.go +++ b/cmd/labels/update.go @@ -6,6 +6,7 @@ package labels import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" @@ -41,7 +42,7 @@ var CmdLabelUpdate = cli.Command{ } func runLabelUpdate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) id := ctx.Int64("id") @@ -67,7 +68,6 @@ func runLabelUpdate(_ stdctx.Context, cmd *cli.Command) error { Color: pColor, Description: pDescription, }) - if err != nil { return err } diff --git a/cmd/milestones.go b/cmd/milestones.go index dfde115..aa85ada 100644 --- a/cmd/milestones.go +++ b/cmd/milestones.go @@ -6,6 +6,7 @@ package cmd import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/milestones" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -40,7 +41,7 @@ func runMilestones(ctx stdctx.Context, cmd *cli.Command) error { } func runMilestoneDetail(_ stdctx.Context, cmd *cli.Command, name string) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/milestones/create.go b/cmd/milestones/create.go index e6d1865..da5d882 100644 --- a/cmd/milestones/create.go +++ b/cmd/milestones/create.go @@ -9,8 +9,8 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" "code.gitea.io/tea/modules/task" "github.com/araddon/dateparse" @@ -50,7 +50,7 @@ var CmdMilestonesCreate = cli.Command{ } func runMilestonesCreate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) date := ctx.String("deadline") deadline := &time.Time{} diff --git a/cmd/milestones/delete.go b/cmd/milestones/delete.go index 4274283..88e7336 100644 --- a/cmd/milestones/delete.go +++ b/cmd/milestones/delete.go @@ -6,6 +6,7 @@ package milestones import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" @@ -24,7 +25,7 @@ var CmdMilestonesDelete = cli.Command{ } func deleteMilestone(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/milestones/issues.go b/cmd/milestones/issues.go index 7d056ba..042f41e 100644 --- a/cmd/milestones/issues.go +++ b/cmd/milestones/issues.go @@ -9,6 +9,7 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -71,7 +72,7 @@ var CmdMilestoneRemoveIssue = cli.Command{ } func runMilestoneIssueList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() @@ -121,7 +122,7 @@ func runMilestoneIssueList(_ stdctx.Context, cmd *cli.Command) error { } func runMilestoneIssueAdd(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() if ctx.Args().Len() != 2 { @@ -148,7 +149,7 @@ func runMilestoneIssueAdd(_ stdctx.Context, cmd *cli.Command) error { } func runMilestoneIssueRemove(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() if ctx.Args().Len() != 2 { diff --git a/cmd/milestones/list.go b/cmd/milestones/list.go index e229c56..f7a3dc7 100644 --- a/cmd/milestones/list.go +++ b/cmd/milestones/list.go @@ -6,6 +6,7 @@ package milestones import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -40,7 +41,7 @@ var CmdMilestonesList = cli.Command{ // RunMilestonesList list milestones func RunMilestonesList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) fields, err := fieldsFlag.GetValues(cmd) @@ -64,7 +65,6 @@ func RunMilestonesList(_ stdctx.Context, cmd *cli.Command) error { ListOptions: flags.GetListOptions(), State: state, }) - if err != nil { return err } diff --git a/cmd/milestones/reopen.go b/cmd/milestones/reopen.go index 373db4b..f5560ee 100644 --- a/cmd/milestones/reopen.go +++ b/cmd/milestones/reopen.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -29,7 +30,7 @@ var CmdMilestonesReopen = cli.Command{ } func editMilestoneStatus(_ stdctx.Context, cmd *cli.Command, close bool) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if ctx.Args().Len() == 0 { return fmt.Errorf(ctx.Command.ArgsUsage) diff --git a/cmd/notifications/list.go b/cmd/notifications/list.go index 521a9b1..d05352e 100644 --- a/cmd/notifications/list.go +++ b/cmd/notifications/list.go @@ -7,6 +7,7 @@ import ( stdctx "context" "log" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -64,7 +65,7 @@ func listNotifications(_ stdctx.Context, cmd *cli.Command, status []gitea.Notify var news []*gitea.NotificationThread var err error - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) client := ctx.Login.Client() all := ctx.Bool("mine") diff --git a/cmd/notifications/mark_as.go b/cmd/notifications/mark_as.go index a9d1525..140ab69 100644 --- a/cmd/notifications/mark_as.go +++ b/cmd/notifications/mark_as.go @@ -8,6 +8,7 @@ import ( "fmt" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/utils" @@ -23,7 +24,7 @@ var CmdNotificationsMarkRead = cli.Command{ ArgsUsage: "[all | ]", Flags: flags.NotificationFlags, Action: func(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) filter, err := flags.NotificationStateFlag.GetValues(cmd) if err != nil { return err @@ -44,7 +45,7 @@ var CmdNotificationsMarkUnread = cli.Command{ ArgsUsage: "[all | ]", Flags: flags.NotificationFlags, Action: func(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) filter, err := flags.NotificationStateFlag.GetValues(cmd) if err != nil { return err @@ -65,7 +66,7 @@ var CmdNotificationsMarkPinned = cli.Command{ ArgsUsage: "[all | ]", Flags: flags.NotificationFlags, Action: func(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) filter, err := flags.NotificationStateFlag.GetValues(cmd) if err != nil { return err @@ -85,7 +86,7 @@ var CmdNotificationsUnpin = cli.Command{ ArgsUsage: "[all | ]", Flags: flags.NotificationFlags, Action: func(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) filter := []string{string(gitea.NotifyStatusPinned)} // NOTE: we implicitly mark it as read, to match web UI semantics. marking as unread might be more useful? return markNotificationAs(ctx, filter, gitea.NotifyStatusRead) diff --git a/cmd/open.go b/cmd/open.go index f4f3041..e9a7d35 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -8,6 +8,7 @@ import ( "path" "strings" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" local_git "code.gitea.io/tea/modules/git" @@ -28,7 +29,7 @@ var CmdOpen = cli.Command{ } func runOpen(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) var suffix string diff --git a/cmd/organizations.go b/cmd/organizations.go index 5ccb5a3..c4f93f3 100644 --- a/cmd/organizations.go +++ b/cmd/organizations.go @@ -6,6 +6,7 @@ package cmd import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/organizations" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -31,7 +32,7 @@ var CmdOrgs = cli.Command{ } func runOrganizations(ctx stdctx.Context, cmd *cli.Command) error { - teaCtx := context.InitCommand(cmd) + teaCtx := base.InitCommand(cmd) if teaCtx.Args().Len() == 1 { return runOrganizationDetail(teaCtx) } diff --git a/cmd/organizations/create.go b/cmd/organizations/create.go index 523d03c..23fd4b3 100644 --- a/cmd/organizations/create.go +++ b/cmd/organizations/create.go @@ -9,8 +9,8 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "github.com/urfave/cli/v3" ) @@ -53,7 +53,7 @@ var CmdOrganizationCreate = cli.Command{ // RunOrganizationCreate sets up a new organization func RunOrganizationCreate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) if ctx.Args().Len() < 1 { return fmt.Errorf("You have to specify the organization name you want to create") diff --git a/cmd/organizations/delete.go b/cmd/organizations/delete.go index b5d8f32..ffea0cc 100644 --- a/cmd/organizations/delete.go +++ b/cmd/organizations/delete.go @@ -7,8 +7,8 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "github.com/urfave/cli/v3" ) @@ -28,7 +28,7 @@ var CmdOrganizationDelete = cli.Command{ // RunOrganizationDelete delete user organization func RunOrganizationDelete(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) client := ctx.Login.Client() diff --git a/cmd/organizations/list.go b/cmd/organizations/list.go index 2c7a267..0a7f130 100644 --- a/cmd/organizations/list.go +++ b/cmd/organizations/list.go @@ -7,8 +7,8 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "github.com/urfave/cli/v3" ) @@ -29,7 +29,7 @@ var CmdOrganizationList = cli.Command{ // RunOrganizationList list user organizations func RunOrganizationList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) client := ctx.Login.Client() userOrganizations, _, err := client.ListUserOrgs(ctx.Login.User, gitea.ListOrgsOptions{ diff --git a/cmd/pulls.go b/cmd/pulls.go index e4defa9..be3333b 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/pulls" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" @@ -55,7 +56,7 @@ func runPulls(ctx stdctx.Context, cmd *cli.Command) error { } func runPullDetail(_ stdctx.Context, cmd *cli.Command, index string) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) idx, err := utils.ArgToIndex(index) if err != nil { diff --git a/cmd/pulls/approve.go b/cmd/pulls/approve.go index e691c4b..9db4e86 100644 --- a/cmd/pulls/approve.go +++ b/cmd/pulls/approve.go @@ -10,6 +10,7 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/task" @@ -25,7 +26,7 @@ var CmdPullsApprove = cli.Command{ Description: "Approve a pull request", ArgsUsage: " []", Action: func(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if ctx.Args().Len() == 0 { diff --git a/cmd/pulls/checkout.go b/cmd/pulls/checkout.go index 31e1867..ff2d84e 100644 --- a/cmd/pulls/checkout.go +++ b/cmd/pulls/checkout.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" @@ -34,7 +35,7 @@ var CmdPullsCheckout = cli.Command{ } func runPullsCheckout(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{ LocalRepo: true, RemoteRepo: true, diff --git a/cmd/pulls/clean.go b/cmd/pulls/clean.go index 3aaec2a..248a22e 100644 --- a/cmd/pulls/clean.go +++ b/cmd/pulls/clean.go @@ -8,6 +8,7 @@ import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" @@ -32,7 +33,7 @@ var CmdPullsClean = cli.Command{ } func runPullsClean(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{LocalRepo: true}) if ctx.Args().Len() != 1 { return fmt.Errorf("Must specify a PR index") diff --git a/cmd/pulls/create.go b/cmd/pulls/create.go index 14a1e67..02be3c5 100644 --- a/cmd/pulls/create.go +++ b/cmd/pulls/create.go @@ -7,8 +7,8 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" "code.gitea.io/tea/modules/task" "github.com/urfave/cli/v3" @@ -41,7 +41,7 @@ var CmdPullsCreate = cli.Command{ } func runPullsCreate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) // no args -> interactive mode if ctx.NumFlags() == 0 { diff --git a/cmd/pulls/edit.go b/cmd/pulls/edit.go index 615dbef..ce3988d 100644 --- a/cmd/pulls/edit.go +++ b/cmd/pulls/edit.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "code.gitea.io/tea/modules/utils" @@ -17,7 +18,7 @@ import ( // editPullState abstracts the arg parsing to edit the given pull request func editPullState(_ stdctx.Context, cmd *cli.Command, opts gitea.EditPullRequestOption) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if ctx.Args().Len() == 0 { return fmt.Errorf("Please provide a Pull Request index") diff --git a/cmd/pulls/list.go b/cmd/pulls/list.go index 9c8661e..bb2c133 100644 --- a/cmd/pulls/list.go +++ b/cmd/pulls/list.go @@ -7,6 +7,7 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -30,7 +31,7 @@ var CmdPullsList = cli.Command{ // RunPullsList return list of pulls func RunPullsList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) state := gitea.StateOpen @@ -46,7 +47,6 @@ func RunPullsList(_ stdctx.Context, cmd *cli.Command) error { prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{ State: state, }) - if err != nil { return err } diff --git a/cmd/pulls/merge.go b/cmd/pulls/merge.go index 11f36d1..8362660 100644 --- a/cmd/pulls/merge.go +++ b/cmd/pulls/merge.go @@ -7,6 +7,7 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" @@ -41,7 +42,7 @@ var CmdPullsMerge = cli.Command{ }, }, flags.AllDefaultFlags...), Action: func(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if ctx.Args().Len() != 1 { diff --git a/cmd/pulls/reject.go b/cmd/pulls/reject.go index 277fcd3..9daa2dd 100644 --- a/cmd/pulls/reject.go +++ b/cmd/pulls/reject.go @@ -8,6 +8,7 @@ import ( "fmt" "strings" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/task" @@ -24,7 +25,7 @@ var CmdPullsReject = cli.Command{ Description: "Request changes to a pull request", ArgsUsage: " ", Action: func(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if ctx.Args().Len() < 2 { diff --git a/cmd/pulls/review.go b/cmd/pulls/review.go index d5eaadb..887a47a 100644 --- a/cmd/pulls/review.go +++ b/cmd/pulls/review.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/interact" @@ -22,7 +23,7 @@ var CmdPullsReview = cli.Command{ Description: "Interactively review a pull request", ArgsUsage: "", Action: func(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if ctx.Args().Len() != 1 { diff --git a/cmd/releases/create.go b/cmd/releases/create.go index ba1b345..8965f5d 100644 --- a/cmd/releases/create.go +++ b/cmd/releases/create.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" @@ -68,7 +69,7 @@ var CmdReleaseCreate = cli.Command{ } func runReleaseCreate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) tag := ctx.String("tag") diff --git a/cmd/releases/delete.go b/cmd/releases/delete.go index a3acc8f..30bd15a 100644 --- a/cmd/releases/delete.go +++ b/cmd/releases/delete.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" @@ -35,7 +36,7 @@ var CmdReleaseDelete = cli.Command{ } func runReleaseDelete(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/releases/edit.go b/cmd/releases/edit.go index b1378ff..c6f6578 100644 --- a/cmd/releases/edit.go +++ b/cmd/releases/edit.go @@ -10,6 +10,7 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "github.com/urfave/cli/v3" @@ -58,7 +59,7 @@ var CmdReleaseEdit = cli.Command{ } func runReleaseEdit(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/releases/list.go b/cmd/releases/list.go index 431e43a..bd946a3 100644 --- a/cmd/releases/list.go +++ b/cmd/releases/list.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -31,7 +32,7 @@ var CmdReleaseList = cli.Command{ // RunReleasesList list releases func RunReleasesList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) releases, _, err := ctx.Login.Client().ListReleases(ctx.Owner, ctx.Repo, gitea.ListReleasesOptions{ diff --git a/cmd/repos.go b/cmd/repos.go index 2ab1ab7..ba65717 100644 --- a/cmd/repos.go +++ b/cmd/repos.go @@ -6,8 +6,8 @@ package cmd import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/repos" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "code.gitea.io/tea/modules/utils" @@ -44,7 +44,7 @@ func runRepos(ctx stdctx.Context, cmd *cli.Command) error { } func runRepoDetail(_ stdctx.Context, cmd *cli.Command, path string) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) client := ctx.Login.Client() repoOwner, repoName := utils.GetOwnerAndRepo(path, ctx.Owner) repo, _, err := client.GetRepo(repoOwner, repoName) diff --git a/cmd/repos/create.go b/cmd/repos/create.go index 3486352..41c93e0 100644 --- a/cmd/repos/create.go +++ b/cmd/repos/create.go @@ -7,8 +7,8 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "code.gitea.io/sdk/gitea" @@ -103,7 +103,7 @@ var CmdRepoCreate = cli.Command{ } func runRepoCreate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) client := ctx.Login.Client() var ( repo *gitea.Repository diff --git a/cmd/repos/create_from_template.go b/cmd/repos/create_from_template.go index 7670980..8a9e627 100644 --- a/cmd/repos/create_from_template.go +++ b/cmd/repos/create_from_template.go @@ -9,8 +9,8 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "code.gitea.io/tea/modules/utils" "github.com/urfave/cli/v3" @@ -83,7 +83,7 @@ var CmdRepoCreateFromTemplate = cli.Command{ } func runRepoCreateFromTemplate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) client := ctx.Login.Client() templateOwner, templateRepo := utils.GetOwnerAndRepo(ctx.String("template"), ctx.Login.User) diff --git a/cmd/repos/delete.go b/cmd/repos/delete.go index f54fcc7..644012f 100644 --- a/cmd/repos/delete.go +++ b/cmd/repos/delete.go @@ -7,8 +7,8 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "github.com/charmbracelet/huh" "github.com/urfave/cli/v3" @@ -46,7 +46,7 @@ var CmdRepoRm = cli.Command{ } func runRepoDelete(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) client := ctx.Login.Client() diff --git a/cmd/repos/fork.go b/cmd/repos/fork.go index 764c027..d48d620 100644 --- a/cmd/repos/fork.go +++ b/cmd/repos/fork.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -33,7 +34,7 @@ var CmdRepoFork = cli.Command{ } func runRepoFork(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/repos/list.go b/cmd/repos/list.go index bea0243..29dee37 100644 --- a/cmd/repos/list.go +++ b/cmd/repos/list.go @@ -6,8 +6,8 @@ package repos import ( stdctx "context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "code.gitea.io/sdk/gitea" @@ -50,7 +50,7 @@ var CmdReposList = cli.Command{ // RunReposList list repositories func RunReposList(_ stdctx.Context, cmd *cli.Command) error { - teaCmd := context.InitCommand(cmd) + teaCmd := base.InitCommand(cmd) client := teaCmd.Login.Client() typeFilter, err := getTypeFilter(cmd) diff --git a/cmd/repos/migrate.go b/cmd/repos/migrate.go index af73ecb..3f41603 100644 --- a/cmd/repos/migrate.go +++ b/cmd/repos/migrate.go @@ -9,8 +9,8 @@ import ( stdctx "context" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "github.com/urfave/cli/v3" ) @@ -109,7 +109,7 @@ var CmdRepoMigrate = cli.Command{ } func runRepoMigrate(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) client := ctx.Login.Client() var ( repo *gitea.Repository @@ -157,7 +157,6 @@ func runRepoMigrate(_ stdctx.Context, cmd *cli.Command) error { } repo, _, err = client.MigrateRepo(opts) - if err != nil { return err } diff --git a/cmd/repos/search.go b/cmd/repos/search.go index 9b8ad3b..41fdddb 100644 --- a/cmd/repos/search.go +++ b/cmd/repos/search.go @@ -8,8 +8,8 @@ import ( "fmt" "strings" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" - "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" "code.gitea.io/sdk/gitea" @@ -57,7 +57,7 @@ var CmdReposSearch = cli.Command{ } func runReposSearch(_ stdctx.Context, cmd *cli.Command) error { - teaCmd := context.InitCommand(cmd) + teaCmd := base.InitCommand(cmd) client := teaCmd.Login.Client() var ownerID int64 diff --git a/cmd/times/add.go b/cmd/times/add.go index 868ffbe..e7d35eb 100644 --- a/cmd/times/add.go +++ b/cmd/times/add.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/utils" @@ -32,7 +33,7 @@ var CmdTrackedTimesAdd = cli.Command{ } func runTrackedTimesAdd(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) if ctx.Args().Len() < 2 { diff --git a/cmd/times/delete.go b/cmd/times/delete.go index d0b57af..d0aab43 100644 --- a/cmd/times/delete.go +++ b/cmd/times/delete.go @@ -8,6 +8,7 @@ import ( "fmt" "strconv" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/utils" @@ -26,7 +27,7 @@ var CmdTrackedTimesDelete = cli.Command{ } func runTrackedTimesDelete(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/times/list.go b/cmd/times/list.go index 0e8d0e9..95a623f 100644 --- a/cmd/times/list.go +++ b/cmd/times/list.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/print" @@ -70,7 +71,7 @@ Depending on your permissions on the repository, only your own tracked times mig // RunTimesList list repositories func RunTimesList(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/times/reset.go b/cmd/times/reset.go index 47c22e5..8fd7823 100644 --- a/cmd/times/reset.go +++ b/cmd/times/reset.go @@ -7,6 +7,7 @@ import ( stdctx "context" "fmt" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/utils" @@ -25,7 +26,7 @@ var CmdTrackedTimesReset = cli.Command{ } func runTrackedTimesReset(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) client := ctx.Login.Client() diff --git a/cmd/whoami.go b/cmd/whoami.go index c6fb029..febc24c 100644 --- a/cmd/whoami.go +++ b/cmd/whoami.go @@ -6,7 +6,7 @@ package cmd import ( stdctx "context" - "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/cmd/base" "code.gitea.io/tea/modules/print" "github.com/urfave/cli/v3" ) @@ -19,7 +19,7 @@ var CmdWhoami = cli.Command{ Usage: "Show current logged in user", ArgsUsage: " ", // command does not accept arguments Action: func(_ stdctx.Context, cmd *cli.Command) error { - ctx := context.InitCommand(cmd) + ctx := base.InitCommand(cmd) client := ctx.Login.Client() user, _, _ := client.GetMyUserInfo() print.UserDetails(user) diff --git a/modules/context/context.go b/modules/context/context.go index 13c7e50..8472a36 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -6,7 +6,6 @@ package context import ( "errors" "fmt" - "log" "os" "path" "strconv" @@ -16,15 +15,11 @@ import ( "code.gitea.io/tea/modules/config" "code.gitea.io/tea/modules/debug" "code.gitea.io/tea/modules/git" - "code.gitea.io/tea/modules/theme" - "code.gitea.io/tea/modules/utils" - "github.com/charmbracelet/huh" - gogit "github.com/go-git/go-git/v5" "github.com/urfave/cli/v3" ) -var errNotAGiteaRepo = errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository") +var ErrNotAGiteaRepo = errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository") // TeaContext contains all context derived during command initialization and wraps cli.Context type TeaContext struct { @@ -65,108 +60,8 @@ type CtxRequirement struct { 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 -// command flags. If a local git repo can't be found, repo slug values are unset. -func InitCommand(cmd *cli.Command) *TeaContext { - // these flags are used as overrides to the context detection via local git repo - repoFlag := cmd.String("repo") - loginFlag := cmd.String("login") - remoteFlag := cmd.String("remote") - - var ( - c TeaContext - err error - repoPath string // empty means PWD - repoFlagPathExists bool - ) - - // check if repoFlag can be interpreted as path to local repo. - if len(repoFlag) != 0 { - if repoFlagPathExists, err = utils.DirExists(repoFlag); err != nil { - log.Fatal(err.Error()) - } - if repoFlagPathExists { - repoPath = repoFlag - } - } - - if len(remoteFlag) == 0 { - remoteFlag = config.GetPreferences().FlagDefaults.Remote - } - - if repoPath == "" { - if repoPath, err = os.Getwd(); err != nil { - log.Fatal(err.Error()) - } - } - - // try to read local git repo & extract context: if repoFlag specifies a valid path, read repo in that dir, - // otherwise attempt PWD. if no repo is found, continue with default login - if c.LocalRepo, c.Login, c.RepoSlug, err = contextFromLocalRepo(repoPath, remoteFlag); err != nil { - if err == errNotAGiteaRepo || err == gogit.ErrRepositoryNotExists { - // we can deal with that, commands needing the optional values use ctx.Ensure() - } else { - log.Fatal(err.Error()) - } - } - - if len(repoFlag) != 0 && !repoFlagPathExists { - // if repoFlag is not a valid path, use it to override repoSlug - c.RepoSlug = repoFlag - } - - // override config user with env variable - envLogin := GetLoginByEnvVar() - if envLogin != nil { - _, err := utils.ValidateAuthenticationMethod(envLogin.URL, envLogin.Token, "", "", false, "", "") - if err != nil { - log.Fatal(err.Error()) - } - c.Login = envLogin - } - - // override login from flag, or use default login if repo based detection failed - if len(loginFlag) != 0 { - c.Login = config.GetLoginByName(loginFlag) - if c.Login == nil { - log.Fatalf("Login name '%s' does not exist", loginFlag) - } - } else if c.Login == nil { - if c.Login, err = config.GetDefaultLogin(); err != nil { - if err.Error() == "No available login" { - // TODO: maybe we can directly start interact.CreateLogin() (only if - // we're sure we can interactively!), as gh cli does. - fmt.Println(`No gitea login configured. To start using tea, first run - tea login add -and then run your command again.`) - } - os.Exit(1) - } - - fallback := false - if err := huh.NewConfirm(). - Title(fmt.Sprintf("NOTE: no gitea login detected, whether falling back to login '%s'?", c.Login.Name)). - Value(&fallback). - WithTheme(theme.GetTheme()). - Run(); err != nil { - log.Fatalf("Get confirm failed: %v", err) - } - if !fallback { - os.Exit(1) - } - } - - // parse reposlug (owner falling back to login owner if reposlug contains only repo name) - c.Owner, c.Repo = utils.GetOwnerAndRepo(c.RepoSlug, c.Login.User) - c.Command = cmd - c.Output = cmd.String("output") - return &c -} - -// contextFromLocalRepo discovers login & repo slug from the default branch remote of the given local repo -func contextFromLocalRepo(repoPath, remoteValue string) (*git.TeaRepo, *config.Login, string, error) { +// ContextFromLocalRepo discovers login & repo slug from the default branch remote of the given local repo +func ContextFromLocalRepo(repoPath, remoteValue string) (*git.TeaRepo, *config.Login, string, error) { repo, err := git.RepoFromPath(repoPath) if err != nil { return nil, nil, "", err @@ -178,7 +73,7 @@ func contextFromLocalRepo(repoPath, remoteValue string) (*git.TeaRepo, *config.L debug.Printf("Get git config %v of %s in repo %s", gitConfig, remoteValue, repoPath) if len(gitConfig.Remotes) == 0 { - return repo, nil, "", errNotAGiteaRepo + return repo, nil, "", ErrNotAGiteaRepo } // When no preferred value is given, choose a remote to find a @@ -229,7 +124,7 @@ func contextFromLocalRepo(repoPath, remoteValue string) (*git.TeaRepo, *config.L } } - return repo, nil, "", errNotAGiteaRepo + return repo, nil, "", ErrNotAGiteaRepo } // MatchLogins matches the given remoteURL against the provided logins and returns @@ -274,7 +169,7 @@ func MatchLogins(remoteURL string, logins []config.Login) (*config.Login, string } } } - return nil, "", errNotAGiteaRepo + return nil, "", ErrNotAGiteaRepo } // GetLoginByEnvVar returns a login based on environment variables, or nil if no login can be created diff --git a/modules/interact/login.go b/modules/interact/login.go index 58b9002..791b39a 100644 --- a/modules/interact/login.go +++ b/modules/interact/login.go @@ -306,3 +306,39 @@ var tokenScopeOpts = []string{ string(gitea.AccessTokenScopeReadApplication), string(gitea.AccessTokenScopeSudo), } + +func LoginSelect() (*config.Login, error) { + logins, err := config.GetLogins() + if err != nil { + return nil, err + } + if len(logins) == 0 { + return nil, errors.New("no gitea login configured") + } + if len(logins) == 1 { + return &logins[0], nil + } + + loginNames := make([]string, 0, len(logins)) + for _, l := range logins { + loginNames = append(loginNames, l.Name) + } + + defaultLogin, _ := config.GetDefaultLogin() + var defaultValue string + if defaultLogin != nil { + defaultValue = defaultLogin.Name + } + + loginName, err := promptSelect("Select gitea login: ", loginNames, "", "", defaultValue) + if err != nil { + return nil, err + } + + for _, l := range logins { + if l.Name == loginName { + return &l, nil + } + } + return nil, fmt.Errorf("login name '%s' does not exist", loginName) +}