From 47cef736f4acaf46c4af65529e8120297e642e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 7 Aug 2020 14:47:58 +0200 Subject: [PATCH] Fix GH_REPO override --- command/root.go | 12 ++---------- context/context.go | 3 --- pkg/cmd/issue/issue.go | 11 +---------- pkg/cmd/pr/pr.go | 11 +---------- pkg/cmdutil/repo_override.go | 25 +++++++++++++++++++++++++ 5 files changed, 29 insertions(+), 33 deletions(-) create mode 100644 pkg/cmdutil/repo_override.go diff --git a/command/root.go b/command/root.go index a72b3eae4..38ed2e99e 100644 --- a/command/root.go +++ b/command/root.go @@ -49,11 +49,7 @@ func init() { // overridden in tests var initContext = func() context.Context { - ctx := context.New() - if repo := os.Getenv("GH_REPO"); repo != "" { - ctx.SetBaseRepo(repo) - } - return ctx + return context.New() } // BasicClient returns an API client for github.com only that borrows from but @@ -78,11 +74,7 @@ func BasicClient() (*api.Client, error) { } func contextForCommand(cmd *cobra.Command) context.Context { - ctx := initContext() - if repo, err := cmd.Flags().GetString("repo"); err == nil && repo != "" { - ctx.SetBaseRepo(repo) - } - return ctx + return initContext() } func apiVerboseLog() api.ClientOption { diff --git a/context/context.go b/context/context.go index 21ebc9b1f..06a89067d 100644 --- a/context/context.go +++ b/context/context.go @@ -15,9 +15,6 @@ import ( // Context represents the interface for querying information about the current environment type Context interface { - Branch() (string, error) - SetBranch(string) - SetBaseRepo(string) Config() (config.Config, error) } diff --git a/pkg/cmd/issue/issue.go b/pkg/cmd/issue/issue.go index 91ed04b22..6f2cea6d9 100644 --- a/pkg/cmd/issue/issue.go +++ b/pkg/cmd/issue/issue.go @@ -2,7 +2,6 @@ package issue import ( "github.com/MakeNowJust/heredoc" - "github.com/cli/cli/internal/ghrepo" cmdClose "github.com/cli/cli/pkg/cmd/issue/close" cmdCreate "github.com/cli/cli/pkg/cmd/issue/create" cmdList "github.com/cli/cli/pkg/cmd/issue/list" @@ -31,17 +30,9 @@ func NewCmdIssue(f *cmdutil.Factory) *cobra.Command { - by URL, e.g. "https://github.com/OWNER/REPO/issues/123". `), }, - PersistentPreRun: func(cmd *cobra.Command, args []string) { - if repo, _ := cmd.Flags().GetString("repo"); repo != "" { - // NOTE: this mutates the factory - f.BaseRepo = func() (ghrepo.Interface, error) { - return ghrepo.FromFullName(repo) - } - } - }, } - cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") + cmdutil.EnableRepoOverride(cmd, f) cmd.AddCommand(cmdClose.NewCmdClose(f, nil)) cmd.AddCommand(cmdCreate.NewCmdCreate(f, nil)) diff --git a/pkg/cmd/pr/pr.go b/pkg/cmd/pr/pr.go index 4a50f8356..a4b746656 100644 --- a/pkg/cmd/pr/pr.go +++ b/pkg/cmd/pr/pr.go @@ -2,7 +2,6 @@ package pr import ( "github.com/MakeNowJust/heredoc" - "github.com/cli/cli/internal/ghrepo" cmdCheckout "github.com/cli/cli/pkg/cmd/pr/checkout" cmdClose "github.com/cli/cli/pkg/cmd/pr/close" cmdCreate "github.com/cli/cli/pkg/cmd/pr/create" @@ -37,17 +36,9 @@ func NewCmdPR(f *cmdutil.Factory) *cobra.Command { - by the name of its head branch, e.g. "patch-1" or "OWNER:patch-1". `), }, - PersistentPreRun: func(cmd *cobra.Command, args []string) { - if repo, _ := cmd.Flags().GetString("repo"); repo != "" { - // NOTE: this mutates the factory - f.BaseRepo = func() (ghrepo.Interface, error) { - return ghrepo.FromFullName(repo) - } - } - }, } - cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") + cmdutil.EnableRepoOverride(cmd, f) cmd.AddCommand(cmdCheckout.NewCmdCheckout(f, nil)) cmd.AddCommand(cmdClose.NewCmdClose(f, nil)) diff --git a/pkg/cmdutil/repo_override.go b/pkg/cmdutil/repo_override.go new file mode 100644 index 000000000..aede57ea6 --- /dev/null +++ b/pkg/cmdutil/repo_override.go @@ -0,0 +1,25 @@ +package cmdutil + +import ( + "os" + + "github.com/cli/cli/internal/ghrepo" + "github.com/spf13/cobra" +) + +func EnableRepoOverride(cmd *cobra.Command, f *Factory) { + cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format") + + cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { + repoOverride, _ := cmd.Flags().GetString("repo") + if repoFromEnv := os.Getenv("GH_REPO"); repoOverride == "" && repoFromEnv != "" { + repoOverride = repoFromEnv + } + if repoOverride != "" { + // NOTE: this mutates the factory + f.BaseRepo = func() (ghrepo.Interface, error) { + return ghrepo.FromFullName(repoOverride) + } + } + } +}