X Tutup
Skip to content

Commit ebc1f2f

Browse files
committed
Consistently respect GH_HOST in repo archive/create/garden/view commands
1 parent 5f5df81 commit ebc1f2f

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

pkg/cmd/repo/archive/archive.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77

88
"github.com/cli/cli/v2/api"
9-
"github.com/cli/cli/v2/internal/ghinstance"
9+
"github.com/cli/cli/v2/internal/config"
1010
"github.com/cli/cli/v2/internal/ghrepo"
1111
"github.com/cli/cli/v2/pkg/cmdutil"
1212

@@ -17,13 +17,15 @@ import (
1717
type ArchiveOptions struct {
1818
HttpClient func() (*http.Client, error)
1919
IO *iostreams.IOStreams
20+
Config func() (config.Config, error)
2021
RepoArg string
2122
}
2223

2324
func NewCmdArchive(f *cmdutil.Factory, runF func(*ArchiveOptions) error) *cobra.Command {
2425
opts := &ArchiveOptions{
2526
IO: f.IOStreams,
2627
HttpClient: f.HttpClient,
28+
Config: f.Config,
2729
}
2830

2931
cmd := &cobra.Command{
@@ -57,7 +59,16 @@ func archiveRun(opts *ArchiveOptions) error {
5759

5860
archiveURL := opts.RepoArg
5961
if !strings.Contains(archiveURL, "/") {
60-
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
62+
cfg, err := opts.Config()
63+
if err != nil {
64+
return err
65+
}
66+
hostname, err := cfg.DefaultHost()
67+
if err != nil {
68+
return err
69+
}
70+
71+
currentUser, err := api.CurrentLoginName(apiClient, hostname)
6172
if err != nil {
6273
return err
6374
}

pkg/cmd/repo/create/create.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/cli/cli/v2/api"
1313
"github.com/cli/cli/v2/git"
1414
"github.com/cli/cli/v2/internal/config"
15-
"github.com/cli/cli/v2/internal/ghinstance"
1615
"github.com/cli/cli/v2/internal/ghrepo"
1716
"github.com/cli/cli/v2/internal/run"
1817
"github.com/cli/cli/v2/pkg/cmdutil"
@@ -327,7 +326,11 @@ func createRun(opts *CreateOptions) error {
327326

328327
templateRepoName := opts.Template
329328
if !strings.Contains(templateRepoName, "/") {
330-
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
329+
host, err := cfg.DefaultHost()
330+
if err != nil {
331+
return err
332+
}
333+
currentUser, err := api.CurrentLoginName(apiClient, host)
331334
if err != nil {
332335
return err
333336
}

pkg/cmd/repo/garden/garden.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"strings"
1414

1515
"github.com/cli/cli/v2/api"
16-
"github.com/cli/cli/v2/internal/ghinstance"
16+
"github.com/cli/cli/v2/internal/config"
1717
"github.com/cli/cli/v2/internal/ghrepo"
1818
"github.com/cli/cli/v2/pkg/cmdutil"
1919
"github.com/cli/cli/v2/pkg/iostreams"
@@ -90,6 +90,7 @@ type GardenOptions struct {
9090
HttpClient func() (*http.Client, error)
9191
IO *iostreams.IOStreams
9292
BaseRepo func() (ghrepo.Interface, error)
93+
Config func() (config.Config, error)
9394

9495
RepoArg string
9596
}
@@ -99,6 +100,7 @@ func NewCmdGarden(f *cmdutil.Factory, runF func(*GardenOptions) error) *cobra.Co
99100
IO: f.IOStreams,
100101
HttpClient: f.HttpClient,
101102
BaseRepo: f.BaseRepo,
103+
Config: f.Config,
102104
}
103105

104106
cmd := &cobra.Command{
@@ -149,7 +151,16 @@ func gardenRun(opts *GardenOptions) error {
149151
var err error
150152
viewURL := opts.RepoArg
151153
if !strings.Contains(viewURL, "/") {
152-
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
154+
cfg, err := opts.Config()
155+
if err != nil {
156+
return err
157+
}
158+
hostname, err := cfg.DefaultHost()
159+
if err != nil {
160+
return err
161+
}
162+
163+
currentUser, err := api.CurrentLoginName(apiClient, hostname)
153164
if err != nil {
154165
return err
155166
}

pkg/cmd/repo/view/view.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"github.com/MakeNowJust/heredoc"
1313
"github.com/cli/cli/v2/api"
14-
"github.com/cli/cli/v2/internal/ghinstance"
14+
"github.com/cli/cli/v2/internal/config"
1515
"github.com/cli/cli/v2/internal/ghrepo"
1616
"github.com/cli/cli/v2/pkg/cmdutil"
1717
"github.com/cli/cli/v2/pkg/iostreams"
@@ -30,6 +30,7 @@ type ViewOptions struct {
3030
BaseRepo func() (ghrepo.Interface, error)
3131
Browser browser
3232
Exporter cmdutil.Exporter
33+
Config func() (config.Config, error)
3334

3435
RepoArg string
3536
Web bool
@@ -42,6 +43,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
4243
HttpClient: f.HttpClient,
4344
BaseRepo: f.BaseRepo,
4445
Browser: f.Browser,
46+
Config: f.Config,
4547
}
4648

4749
cmd := &cobra.Command{
@@ -90,10 +92,18 @@ func viewRun(opts *ViewOptions) error {
9092
return err
9193
}
9294
} else {
93-
var err error
9495
viewURL := opts.RepoArg
9596
if !strings.Contains(viewURL, "/") {
96-
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
97+
cfg, err := opts.Config()
98+
if err != nil {
99+
return err
100+
}
101+
hostname, err := cfg.DefaultHost()
102+
if err != nil {
103+
return err
104+
}
105+
106+
currentUser, err := api.CurrentLoginName(apiClient, hostname)
97107
if err != nil {
98108
return err
99109
}

pkg/cmd/repo/view/view_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/MakeNowJust/heredoc"
1010
"github.com/cli/cli/v2/api"
11+
"github.com/cli/cli/v2/internal/config"
1112
"github.com/cli/cli/v2/internal/ghrepo"
1213
"github.com/cli/cli/v2/internal/run"
1314
"github.com/cli/cli/v2/pkg/cmdutil"
@@ -528,6 +529,9 @@ func Test_ViewRun_WithoutUsername(t *testing.T) {
528529
return &http.Client{Transport: reg}, nil
529530
},
530531
IO: io,
532+
Config: func() (config.Config, error) {
533+
return config.NewBlankConfig(), nil
534+
},
531535
}
532536

533537
if err := viewRun(opts); err != nil {

0 commit comments

Comments
 (0)
X Tutup