Fix GH_REPO override

This commit is contained in:
Mislav Marohnić 2020-08-07 14:47:58 +02:00
parent aef1a4ba4d
commit 47cef736f4
5 changed files with 29 additions and 33 deletions

View file

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

View file

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

View file

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

View file

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

View file

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