X Tutup
Skip to content

Commit 8fb6bb6

Browse files
authored
Merge pull request cli#3992 from despreston/858-config-browser
add browser option to config
2 parents 2c02c28 + b943801 commit 8fb6bb6

File tree

5 files changed

+119
-3
lines changed

5 files changed

+119
-3
lines changed

internal/config/config_type.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ var configOptions = []ConfigOption{
5555
Description: "the path to a unix socket through which to make HTTP connection",
5656
DefaultValue: "",
5757
},
58+
{
59+
Key: "browser",
60+
Description: "the web browser to use for opening URLs",
61+
DefaultValue: "",
62+
},
5863
}
5964

6065
func ConfigOptions() []ConfigOption {
@@ -193,6 +198,15 @@ func NewBlankRoot() *yaml.Node {
193198
Kind: yaml.ScalarNode,
194199
Value: "",
195200
},
201+
{
202+
HeadComment: "What web browser gh should use when opening URLs. If blank, will refer to environment.",
203+
Kind: yaml.ScalarNode,
204+
Value: "browser",
205+
},
206+
{
207+
Kind: yaml.ScalarNode,
208+
Value: "",
209+
},
196210
},
197211
},
198212
},

internal/config/config_type_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func Test_defaultConfig(t *testing.T) {
5252
co: pr checkout
5353
# The path to a unix socket through which send HTTP connections. If blank, HTTP traffic will be handled by net/http.DefaultTransport.
5454
http_unix_socket:
55+
# What web browser gh should use when opening URLs. If blank, will refer to environment.
56+
browser:
5557
`)
5658
assert.Equal(t, expected, mainBuf.String())
5759
assert.Equal(t, "", hostsBuf.String())
@@ -69,6 +71,10 @@ func Test_defaultConfig(t *testing.T) {
6971
assert.Equal(t, len(aliases.All()), 1)
7072
expansion, _ := aliases.Get("co")
7173
assert.Equal(t, expansion, "pr checkout")
74+
75+
browser, err := cfg.Get("", "browser")
76+
assert.NoError(t, err)
77+
assert.Equal(t, "", browser)
7278
}
7379

7480
func Test_ValidateValue(t *testing.T) {
@@ -106,4 +112,7 @@ func Test_ValidateKey(t *testing.T) {
106112

107113
err = ValidateKey("http_unix_socket")
108114
assert.NoError(t, err)
115+
116+
err = ValidateKey("browser")
117+
assert.NoError(t, err)
109118
}

pkg/cmd/factory/default.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func New(appVersion string) *cmdutil.Factory {
2929
f.HttpClient = httpClientFunc(f, appVersion) // Depends on Config, IOStreams, and appVersion
3030
f.Remotes = remotesFunc(f) // Depends on Config
3131
f.BaseRepo = BaseRepoFunc(f) // Depends on Remotes
32-
f.Browser = browser(f) // Depends on IOStreams
32+
f.Browser = browser(f) // Depends on Config, and IOStreams
3333

3434
return f
3535
}
@@ -91,7 +91,26 @@ func httpClientFunc(f *cmdutil.Factory, appVersion string) func() (*http.Client,
9191

9292
func browser(f *cmdutil.Factory) cmdutil.Browser {
9393
io := f.IOStreams
94-
return cmdutil.NewBrowser(os.Getenv("BROWSER"), io.Out, io.ErrOut)
94+
return cmdutil.NewBrowser(browserLauncher(f), io.Out, io.ErrOut)
95+
}
96+
97+
// Browser precedence
98+
// 1. GH_BROWSER
99+
// 2. browser from config
100+
// 3. BROWSER
101+
func browserLauncher(f *cmdutil.Factory) string {
102+
if ghBrowser := os.Getenv("GH_BROWSER"); ghBrowser != "" {
103+
return ghBrowser
104+
}
105+
106+
cfg, err := f.Config()
107+
if err == nil {
108+
if cfgBrowser, _ := cfg.Get("", "browser"); cfgBrowser != "" {
109+
return cfgBrowser
110+
}
111+
}
112+
113+
return os.Getenv("BROWSER")
95114
}
96115

97116
func executable() string {

pkg/cmd/factory/default_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,80 @@ func Test_ioStreams_prompt(t *testing.T) {
378378
}
379379
}
380380

381+
func Test_browserLauncher(t *testing.T) {
382+
tests := []struct {
383+
name string
384+
env map[string]string
385+
config config.Config
386+
wantBrowser string
387+
}{
388+
{
389+
name: "GH_BROWSER set",
390+
env: map[string]string{
391+
"GH_BROWSER": "GH_BROWSER",
392+
},
393+
wantBrowser: "GH_BROWSER",
394+
},
395+
{
396+
name: "config browser set",
397+
config: config.NewFromString("browser: CONFIG_BROWSER"),
398+
wantBrowser: "CONFIG_BROWSER",
399+
},
400+
{
401+
name: "BROWSER set",
402+
env: map[string]string{
403+
"BROWSER": "BROWSER",
404+
},
405+
wantBrowser: "BROWSER",
406+
},
407+
{
408+
name: "GH_BROWSER and config browser set",
409+
env: map[string]string{
410+
"GH_BROWSER": "GH_BROWSER",
411+
},
412+
config: config.NewFromString("browser: CONFIG_BROWSER"),
413+
wantBrowser: "GH_BROWSER",
414+
},
415+
{
416+
name: "config browser and BROWSER set",
417+
env: map[string]string{
418+
"BROWSER": "BROWSER",
419+
},
420+
config: config.NewFromString("browser: CONFIG_BROWSER"),
421+
wantBrowser: "CONFIG_BROWSER",
422+
},
423+
{
424+
name: "GH_BROWSER and BROWSER set",
425+
env: map[string]string{
426+
"BROWSER": "BROWSER",
427+
"GH_BROWSER": "GH_BROWSER",
428+
},
429+
wantBrowser: "GH_BROWSER",
430+
},
431+
}
432+
for _, tt := range tests {
433+
t.Run(tt.name, func(t *testing.T) {
434+
if tt.env != nil {
435+
for k, v := range tt.env {
436+
old := os.Getenv(k)
437+
os.Setenv(k, v)
438+
defer os.Setenv(k, old)
439+
}
440+
}
441+
f := New("1")
442+
f.Config = func() (config.Config, error) {
443+
if tt.config == nil {
444+
return config.NewBlankConfig(), nil
445+
} else {
446+
return tt.config, nil
447+
}
448+
}
449+
browser := browserLauncher(f)
450+
assert.Equal(t, tt.wantBrowser, browser)
451+
})
452+
}
453+
}
454+
381455
func defaultConfig() config.Config {
382456
return config.InheritEnv(config.NewFromString(heredoc.Doc(`
383457
hosts:

pkg/cmd/root/help_topic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var HelpTopics = map[string]map[string]string{
4444
GH_EDITOR, GIT_EDITOR, VISUAL, EDITOR (in order of precedence): the editor tool to use
4545
for authoring text.
4646
47-
BROWSER: the web browser to use for opening links.
47+
GH_BROWSER, BROWSER (in order of precedence): the web browser to use for opening links.
4848
4949
DEBUG: set to any value to enable verbose output to standard error. Include values "api"
5050
or "oauth" to print detailed information about HTTP requests or authentication flow.

0 commit comments

Comments
 (0)
X Tutup