From c95f30af800ecae9be99e1348044e277c33e0e37 Mon Sep 17 00:00:00 2001 From: Des Preston Date: Tue, 13 Jul 2021 15:06:40 -0400 Subject: [PATCH] add browser option to config Allows setting the path to the browser using the config. Closes #858 --- internal/config/config_type.go | 9 +++++++-- pkg/cmd/factory/default.go | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/config/config_type.go b/internal/config/config_type.go index 3714698bd..61bda9847 100644 --- a/internal/config/config_type.go +++ b/internal/config/config_type.go @@ -8,8 +8,8 @@ import ( // This interface describes interacting with some persistent configuration for gh. type Config interface { - Get(string, string) (string, error) - GetWithSource(string, string) (string, string, error) + Get(hostname string, key string) (string, error) + GetWithSource(hostname string, key string) (string, string, error) Set(string, string, string) error UnsetHost(string) Hosts() ([]string, error) @@ -55,6 +55,11 @@ var configOptions = []ConfigOption{ Description: "the path to a unix socket through which to make HTTP connection", DefaultValue: "", }, + { + Key: "browser", + Description: "the path to the browser to use when opening URLs", + DefaultValue: "", + }, } func ConfigOptions() []ConfigOption { diff --git a/pkg/cmd/factory/default.go b/pkg/cmd/factory/default.go index 32187689b..95eca69a8 100644 --- a/pkg/cmd/factory/default.go +++ b/pkg/cmd/factory/default.go @@ -89,8 +89,21 @@ func httpClientFunc(f *cmdutil.Factory, appVersion string) func() (*http.Client, } } +// browser returns the path to the browser. Attempts to read a path from Config, +// and falls back to the $BROWSER env var if Config value is blank. func browser(f *cmdutil.Factory) cmdutil.Browser { io := f.IOStreams + + cfg, err := f.Config() + if err != nil { + fmt.Fprintf(io.ErrOut, "problem loading config %s", err) + } + + fromConfig, err := cfg.Get("", "browser") + if err == nil && fromConfig != "" { + return cmdutil.NewBrowser(fromConfig, io.Out, io.ErrOut) + } + return cmdutil.NewBrowser(os.Getenv("BROWSER"), io.Out, io.ErrOut) }