Merge pull request #3992 from despreston/858-config-browser

add browser option to config
This commit is contained in:
Sam 2021-08-17 14:17:54 -07:00 committed by GitHub
commit 8fb6bb66c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 119 additions and 3 deletions

View file

@ -29,7 +29,7 @@ func New(appVersion string) *cmdutil.Factory {
f.HttpClient = httpClientFunc(f, appVersion) // Depends on Config, IOStreams, and appVersion
f.Remotes = remotesFunc(f) // Depends on Config
f.BaseRepo = BaseRepoFunc(f) // Depends on Remotes
f.Browser = browser(f) // Depends on IOStreams
f.Browser = browser(f) // Depends on Config, and IOStreams
return f
}
@ -91,7 +91,26 @@ func httpClientFunc(f *cmdutil.Factory, appVersion string) func() (*http.Client,
func browser(f *cmdutil.Factory) cmdutil.Browser {
io := f.IOStreams
return cmdutil.NewBrowser(os.Getenv("BROWSER"), io.Out, io.ErrOut)
return cmdutil.NewBrowser(browserLauncher(f), io.Out, io.ErrOut)
}
// Browser precedence
// 1. GH_BROWSER
// 2. browser from config
// 3. BROWSER
func browserLauncher(f *cmdutil.Factory) string {
if ghBrowser := os.Getenv("GH_BROWSER"); ghBrowser != "" {
return ghBrowser
}
cfg, err := f.Config()
if err == nil {
if cfgBrowser, _ := cfg.Get("", "browser"); cfgBrowser != "" {
return cfgBrowser
}
}
return os.Getenv("BROWSER")
}
func executable() string {

View file

@ -378,6 +378,80 @@ func Test_ioStreams_prompt(t *testing.T) {
}
}
func Test_browserLauncher(t *testing.T) {
tests := []struct {
name string
env map[string]string
config config.Config
wantBrowser string
}{
{
name: "GH_BROWSER set",
env: map[string]string{
"GH_BROWSER": "GH_BROWSER",
},
wantBrowser: "GH_BROWSER",
},
{
name: "config browser set",
config: config.NewFromString("browser: CONFIG_BROWSER"),
wantBrowser: "CONFIG_BROWSER",
},
{
name: "BROWSER set",
env: map[string]string{
"BROWSER": "BROWSER",
},
wantBrowser: "BROWSER",
},
{
name: "GH_BROWSER and config browser set",
env: map[string]string{
"GH_BROWSER": "GH_BROWSER",
},
config: config.NewFromString("browser: CONFIG_BROWSER"),
wantBrowser: "GH_BROWSER",
},
{
name: "config browser and BROWSER set",
env: map[string]string{
"BROWSER": "BROWSER",
},
config: config.NewFromString("browser: CONFIG_BROWSER"),
wantBrowser: "CONFIG_BROWSER",
},
{
name: "GH_BROWSER and BROWSER set",
env: map[string]string{
"BROWSER": "BROWSER",
"GH_BROWSER": "GH_BROWSER",
},
wantBrowser: "GH_BROWSER",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.env != nil {
for k, v := range tt.env {
old := os.Getenv(k)
os.Setenv(k, v)
defer os.Setenv(k, old)
}
}
f := New("1")
f.Config = func() (config.Config, error) {
if tt.config == nil {
return config.NewBlankConfig(), nil
} else {
return tt.config, nil
}
}
browser := browserLauncher(f)
assert.Equal(t, tt.wantBrowser, browser)
})
}
}
func defaultConfig() config.Config {
return config.InheritEnv(config.NewFromString(heredoc.Doc(`
hosts:

View file

@ -44,7 +44,7 @@ var HelpTopics = map[string]map[string]string{
GH_EDITOR, GIT_EDITOR, VISUAL, EDITOR (in order of precedence): the editor tool to use
for authoring text.
BROWSER: the web browser to use for opening links.
GH_BROWSER, BROWSER (in order of precedence): the web browser to use for opening links.
DEBUG: set to any value to enable verbose output to standard error. Include values "api"
or "oauth" to print detailed information about HTTP requests or authentication flow.