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

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