Skip to content

Commit 44ae7ae

Browse files
authored
Merge pull request cli#3279 from cli/browser-refactor
Pass web browser to each individual command
2 parents b70b040 + e53d02b commit 44ae7ae

File tree

36 files changed

+655
-495
lines changed

36 files changed

+655
-495
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/MakeNowJust/heredoc v1.0.0
88
github.com/briandowns/spinner v1.11.1
99
github.com/charmbracelet/glamour v0.2.1-0.20200724174618-1246d13c1684
10+
github.com/cli/browser v1.1.0
1011
github.com/cli/oauth v0.8.0
1112
github.com/cli/safeexec v1.0.0
1213
github.com/cpuguy83/go-md2man/v2 v2.0.0

go.sum

Lines changed: 139 additions & 1 deletion
Large diffs are not rendered by default.

internal/authflow/flow.go

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

1111
"github.com/cli/cli/api"
1212
"github.com/cli/cli/internal/ghinstance"
13-
"github.com/cli/cli/pkg/browser"
13+
"github.com/cli/cli/pkg/cmdutil"
1414
"github.com/cli/cli/pkg/iostreams"
1515
"github.com/cli/oauth"
1616
)
@@ -93,11 +93,9 @@ func authFlow(oauthHost string, IO *iostreams.IOStreams, notice string, addition
9393
fmt.Fprintf(w, "- %s to open %s in your browser... ", cs.Bold("Press Enter"), oauthHost)
9494
_ = waitForEnter(IO.In)
9595

96-
browseCmd, err := browser.Command(url)
97-
if err != nil {
98-
return err
99-
}
100-
if err := browseCmd.Run(); err != nil {
96+
// FIXME: read the browser from cmd Factory rather than recreating it
97+
browser := cmdutil.NewBrowser(os.Getenv("BROWSER"), IO.Out, IO.ErrOut)
98+
if err := browser.Browse(url); err != nil {
10199
fmt.Fprintf(w, "%s Failed opening a web browser at %s\n", cs.Red("!"), url)
102100
fmt.Fprintf(w, " %s\n", err)
103101
fmt.Fprint(w, " Please try entering the URL in your browser manually\n")

pkg/browser/browser.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

pkg/browser/browser_test.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

pkg/cmd/factory/default.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@ func New(appVersion string) *cmdutil.Factory {
6969
return currentBranch, nil
7070
},
7171
Executable: ghExecutable,
72+
Browser: cmdutil.NewBrowser(os.Getenv("BROWSER"), io.Out, io.ErrOut),
7273
}
7374
}

pkg/cmd/gist/create/create.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import (
2323
"github.com/spf13/cobra"
2424
)
2525

26+
type browser interface {
27+
Browse(string) error
28+
}
29+
2630
type CreateOptions struct {
2731
IO *iostreams.IOStreams
2832

@@ -34,13 +38,15 @@ type CreateOptions struct {
3438

3539
Config func() (config.Config, error)
3640
HttpClient func() (*http.Client, error)
41+
Browser browser
3742
}
3843

3944
func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Command {
4045
opts := CreateOptions{
4146
IO: f.IOStreams,
4247
Config: f.Config,
4348
HttpClient: f.HttpClient,
49+
Browser: f.Browser,
4450
}
4551

4652
cmd := &cobra.Command{
@@ -156,7 +162,7 @@ func createRun(opts *CreateOptions) error {
156162
if opts.WebMode {
157163
fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", utils.DisplayURL(gist.HTMLURL))
158164

159-
return utils.OpenInBrowser(gist.HTMLURL)
165+
return opts.Browser.Browse(gist.HTMLURL)
160166
}
161167

162168
fmt.Fprintln(opts.IO.Out, gist.HTMLURL)

pkg/cmd/gist/create/create_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ func Test_createRun(t *testing.T) {
172172
wantStderr string
173173
wantParams map[string]interface{}
174174
wantErr bool
175+
wantBrowse string
175176
}{
176177
{
177178
name: "public",
@@ -265,6 +266,7 @@ func Test_createRun(t *testing.T) {
265266
wantOut: "Opening gist.github.com/aa5a315d61ae9438b18d in your browser.\n",
266267
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
267268
wantErr: false,
269+
wantBrowse: "https://gist.github.com/aa5a315d61ae9438b18d",
268270
wantParams: map[string]interface{}{
269271
"description": "",
270272
"updated_at": "0001-01-01T00:00:00Z",
@@ -296,11 +298,11 @@ func Test_createRun(t *testing.T) {
296298
io, stdin, stdout, stderr := iostreams.Test()
297299
tt.opts.IO = io
298300

299-
cs, teardown := run.Stub()
301+
browser := &cmdutil.TestBrowser{}
302+
tt.opts.Browser = browser
303+
304+
_, teardown := run.Stub()
300305
defer teardown(t)
301-
if tt.opts.WebMode {
302-
cs.Register(`https://gist\.github\.com/aa5a315d61ae9438b18d$`, 0, "")
303-
}
304306

305307
t.Run(tt.name, func(t *testing.T) {
306308
stdin.WriteString(tt.stdin)
@@ -318,6 +320,7 @@ func Test_createRun(t *testing.T) {
318320
assert.Equal(t, tt.wantStderr, stderr.String())
319321
assert.Equal(t, tt.wantParams, reqBody)
320322
reg.Verify(t)
323+
browser.Verify(t, tt.wantBrowse)
321324
})
322325
}
323326
}

pkg/cmd/gist/view/view.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ import (
2020
"github.com/spf13/cobra"
2121
)
2222

23+
type browser interface {
24+
Browse(string) error
25+
}
26+
2327
type ViewOptions struct {
2428
IO *iostreams.IOStreams
2529
Config func() (config.Config, error)
2630
HttpClient func() (*http.Client, error)
31+
Browser browser
2732

2833
Selector string
2934
Filename string
@@ -37,6 +42,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
3742
IO: f.IOStreams,
3843
Config: f.Config,
3944
HttpClient: f.HttpClient,
45+
Browser: f.Browser,
4046
}
4147

4248
cmd := &cobra.Command{
@@ -106,7 +112,7 @@ func viewRun(opts *ViewOptions) error {
106112
if opts.IO.IsStderrTTY() {
107113
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(gistURL))
108114
}
109-
return utils.OpenInBrowser(gistURL)
115+
return opts.Browser.Browse(gistURL)
110116
}
111117

112118
if strings.Contains(gistID, "/") {

pkg/cmd/issue/comment/comment.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
issueShared "github.com/cli/cli/pkg/cmd/issue/shared"
1010
prShared "github.com/cli/cli/pkg/cmd/pr/shared"
1111
"github.com/cli/cli/pkg/cmdutil"
12-
"github.com/cli/cli/utils"
1312
"github.com/spf13/cobra"
1413
)
1514

@@ -20,7 +19,7 @@ func NewCmdComment(f *cmdutil.Factory, runF func(*prShared.CommentableOptions) e
2019
EditSurvey: prShared.CommentableEditSurvey(f.Config, f.IOStreams),
2120
InteractiveEditSurvey: prShared.CommentableInteractiveEditSurvey(f.Config, f.IOStreams),
2221
ConfirmSubmitSurvey: prShared.CommentableConfirmSubmitSurvey,
23-
OpenInBrowser: utils.OpenInBrowser,
22+
OpenInBrowser: f.Browser.Browse,
2423
}
2524

2625
var bodyFile string

0 commit comments

Comments
 (0)