diff --git a/internal/config/config_file_test.go b/internal/config/config_file_test.go index 8ee938f31..100c065f3 100644 --- a/internal/config/config_file_test.go +++ b/internal/config/config_file_test.go @@ -22,10 +22,10 @@ hosts: `, "")() config, err := parseConfig("config.yml") assert.NoError(t, err) - user, err := config.GetOrDefault("github.com", "user") + user, err := config.Get("github.com", "user") assert.NoError(t, err) assert.Equal(t, "monalisa", user) - token, err := config.GetOrDefault("github.com", "oauth_token") + token, err := config.Get("github.com", "oauth_token") assert.NoError(t, err) assert.Equal(t, "OTOKEN", token) } @@ -42,10 +42,10 @@ hosts: `, "")() config, err := parseConfig("config.yml") assert.NoError(t, err) - user, err := config.GetOrDefault("github.com", "user") + user, err := config.Get("github.com", "user") assert.NoError(t, err) assert.Equal(t, "monalisa", user) - token, err := config.GetOrDefault("github.com", "oauth_token") + token, err := config.Get("github.com", "oauth_token") assert.NoError(t, err) assert.Equal(t, "OTOKEN", token) } @@ -58,10 +58,10 @@ github.com: `)() config, err := parseConfig("config.yml") assert.NoError(t, err) - user, err := config.GetOrDefault("github.com", "user") + user, err := config.Get("github.com", "user") assert.NoError(t, err) assert.Equal(t, "monalisa", user) - token, err := config.GetOrDefault("github.com", "oauth_token") + token, err := config.Get("github.com", "oauth_token") assert.NoError(t, err) assert.Equal(t, "OTOKEN", token) } diff --git a/internal/config/config_type_test.go b/internal/config/config_type_test.go index dd46a7c2e..c16455bcc 100644 --- a/internal/config/config_type_test.go +++ b/internal/config/config_type_test.go @@ -62,7 +62,7 @@ func Test_defaultConfig(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "https", proto) - editor, err := cfg.GetOrDefault("", "editor") + editor, err := cfg.Get("", "editor") assert.NoError(t, err) assert.Equal(t, "", editor) @@ -72,7 +72,7 @@ func Test_defaultConfig(t *testing.T) { expansion, _ := aliases.Get("co") assert.Equal(t, expansion, "pr checkout") - browser, err := cfg.GetOrDefault("", "browser") + browser, err := cfg.Get("", "browser") assert.NoError(t, err) assert.Equal(t, "", browser) } diff --git a/internal/config/from_env_test.go b/internal/config/from_env_test.go index 59856021d..bf81c7976 100644 --- a/internal/config/from_env_test.go +++ b/internal/config/from_env_test.go @@ -301,11 +301,11 @@ func TestInheritEnv(t *testing.T) { hosts, _ := cfg.Hosts() assert.Equal(t, tt.wants.hosts, hosts) - val, source, _ := cfg.GetOrDefaultWithSource(tt.hostname, "oauth_token") + val, source, _ := cfg.GetWithSource(tt.hostname, "oauth_token") assert.Equal(t, tt.wants.token, val) assert.Regexp(t, tt.wants.source, source) - val, _ = cfg.GetOrDefault(tt.hostname, "oauth_token") + val, _ = cfg.Get(tt.hostname, "oauth_token") assert.Equal(t, tt.wants.token, val) err := cfg.CheckWriteable(tt.hostname, "oauth_token") diff --git a/pkg/cmd/auth/gitcredential/helper.go b/pkg/cmd/auth/gitcredential/helper.go index 67c1c28c4..8d1ab7ff3 100644 --- a/pkg/cmd/auth/gitcredential/helper.go +++ b/pkg/cmd/auth/gitcredential/helper.go @@ -14,7 +14,7 @@ import ( const tokenUser = "x-access-token" type config interface { - GetOrDefaultWithSource(string, string) (string, string, error) + GetWithSource(string, string) (string, string, error) } type CredentialOptions struct { @@ -101,11 +101,11 @@ func helperRun(opts *CredentialOptions) error { } var gotUser string - gotToken, source, _ := cfg.GetOrDefaultWithSource(wants["host"], "oauth_token") + gotToken, source, _ := cfg.GetWithSource(wants["host"], "oauth_token") if strings.HasSuffix(source, "_TOKEN") { gotUser = tokenUser } else { - gotUser, _, _ = cfg.GetOrDefaultWithSource(wants["host"], "user") + gotUser, _, _ = cfg.GetWithSource(wants["host"], "user") } if gotUser == "" || gotToken == "" { diff --git a/pkg/cmd/auth/gitcredential/helper_test.go b/pkg/cmd/auth/gitcredential/helper_test.go index 053dd8f69..2a2b8de38 100644 --- a/pkg/cmd/auth/gitcredential/helper_test.go +++ b/pkg/cmd/auth/gitcredential/helper_test.go @@ -11,12 +11,12 @@ import ( // why not just use the config stub argh type tinyConfig map[string]string -func (c tinyConfig) GetOrDefaultWithSource(host, key string) (string, string, error) { +func (c tinyConfig) GetWithSource(host, key string) (string, string, error) { return c[fmt.Sprintf("%s:%s", host, key)], c["_source"], nil } -func (c tinyConfig) GetOrDefault(host, key string) (val string, err error) { - val, _, err = c.GetOrDefaultWithSource(host, key) +func (c tinyConfig) Get(host, key string) (val string, err error) { + val, _, err = c.GetWithSource(host, key) return } diff --git a/pkg/cmd/auth/login/login.go b/pkg/cmd/auth/login/login.go index f8896de1a..f591fcbc6 100644 --- a/pkg/cmd/auth/login/login.go +++ b/pkg/cmd/auth/login/login.go @@ -165,7 +165,7 @@ func loginRun(opts *LoginOptions) error { return cfg.Write() } - existingToken, _ := cfg.GetOrDefault(hostname, "oauth_token") + existingToken, _ := cfg.Get(hostname, "oauth_token") if existingToken != "" && opts.Interactive { if err := shared.HasMinimumScopes(httpClient, hostname, existingToken); err == nil { var keepGoing bool diff --git a/pkg/cmd/auth/logout/logout.go b/pkg/cmd/auth/logout/logout.go index f48e59db2..3873da324 100644 --- a/pkg/cmd/auth/logout/logout.go +++ b/pkg/cmd/auth/logout/logout.go @@ -127,7 +127,7 @@ func logoutRun(opts *LogoutOptions) error { if err != nil { // suppressing; the user is trying to delete this token and it might be bad. // we'll see if the username is in the config and fall back to that. - username, _ = cfg.GetOrDefault(hostname, "user") + username, _ = cfg.Get(hostname, "user") } usernameStr := "" diff --git a/pkg/cmd/auth/refresh/refresh.go b/pkg/cmd/auth/refresh/refresh.go index c4b15003e..402646aff 100644 --- a/pkg/cmd/auth/refresh/refresh.go +++ b/pkg/cmd/auth/refresh/refresh.go @@ -132,7 +132,7 @@ func refreshRun(opts *RefreshOptions) error { } var additionalScopes []string - if oldToken, _ := cfg.GetOrDefault(hostname, "oauth_token"); oldToken != "" { + if oldToken, _ := cfg.Get(hostname, "oauth_token"); oldToken != "" { if oldScopes, err := shared.GetScopes(opts.httpClient, hostname, oldToken); err == nil { for _, s := range strings.Split(oldScopes, ",") { s = strings.TrimSpace(s) @@ -159,8 +159,8 @@ func refreshRun(opts *RefreshOptions) error { } if credentialFlow.ShouldSetup() { - username, _ := cfg.GetOrDefault(hostname, "user") - password, _ := cfg.GetOrDefault(hostname, "oauth_token") + username, _ := cfg.Get(hostname, "user") + password, _ := cfg.Get(hostname, "oauth_token") if err := credentialFlow.Setup(hostname, username, password); err != nil { return err } diff --git a/pkg/cmd/auth/shared/login_flow.go b/pkg/cmd/auth/shared/login_flow.go index 2ba31b1a9..0bac49b35 100644 --- a/pkg/cmd/auth/shared/login_flow.go +++ b/pkg/cmd/auth/shared/login_flow.go @@ -15,7 +15,7 @@ import ( ) type iconfig interface { - GetOrDefault(string, string) (string, error) + Get(string, string) (string, error) Set(string, string, string) error Write() error } @@ -147,7 +147,7 @@ func Login(opts *LoginOptions) error { var username string if userValidated { - username, _ = cfg.GetOrDefault(hostname, "user") + username, _ = cfg.Get(hostname, "user") } else { apiClient := api.NewClientFromHTTP(httpClient) var err error diff --git a/pkg/cmd/auth/shared/login_flow_test.go b/pkg/cmd/auth/shared/login_flow_test.go index 1290aa176..530e34045 100644 --- a/pkg/cmd/auth/shared/login_flow_test.go +++ b/pkg/cmd/auth/shared/login_flow_test.go @@ -17,7 +17,7 @@ import ( type tinyConfig map[string]string -func (c tinyConfig) GetOrDefault(host, key string) (string, error) { +func (c tinyConfig) Get(host, key string) (string, error) { return c[fmt.Sprintf("%s:%s", host, key)], nil } diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index a7578f1e2..e09273e99 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -92,7 +92,7 @@ func statusRun(opts *StatusOptions) error { } isHostnameFound = true - token, tokenSource, _ := cfg.GetOrDefaultWithSource(hostname, "oauth_token") + token, tokenSource, _ := cfg.GetWithSource(hostname, "oauth_token") tokenIsWriteable := cfg.CheckWriteable(hostname, "oauth_token") == nil statusInfo[hostname] = []string{} diff --git a/pkg/cmd/config/get/get_test.go b/pkg/cmd/config/get/get_test.go index f376c773d..46f187394 100644 --- a/pkg/cmd/config/get/get_test.go +++ b/pkg/cmd/config/get/get_test.go @@ -117,6 +117,8 @@ func Test_getRun(t *testing.T) { assert.Equal(t, tt.stderr, stderr.String()) _, err = tt.input.Config.GetOrDefault("", "_written") assert.Error(t, err) + _, err = tt.input.Config.Get("", "_written") + assert.Error(t, err) }) } } diff --git a/pkg/cmd/factory/default.go b/pkg/cmd/factory/default.go index 8c15e0235..0ede60f61 100644 --- a/pkg/cmd/factory/default.go +++ b/pkg/cmd/factory/default.go @@ -113,7 +113,7 @@ func browserLauncher(f *cmdutil.Factory) string { cfg, err := f.Config() if err == nil { - if cfgBrowser, _ := cfg.GetOrDefault("", "browser"); cfgBrowser != "" { + if cfgBrowser, _ := cfg.Get("", "browser"); cfgBrowser != "" { return cfgBrowser } } @@ -230,7 +230,7 @@ func ioStreams(f *cmdutil.Factory) *iostreams.IOStreams { // 3. PAGER if ghPager, ghPagerExists := os.LookupEnv("GH_PAGER"); ghPagerExists { io.SetPager(ghPager) - } else if pager, _ := cfg.GetOrDefault("", "pager"); pager != "" { + } else if pager, _ := cfg.Get("", "pager"); pager != "" { io.SetPager(pager) } diff --git a/pkg/cmd/factory/http.go b/pkg/cmd/factory/http.go index 2db083557..d1b8b54ed 100644 --- a/pkg/cmd/factory/http.go +++ b/pkg/cmd/factory/http.go @@ -55,6 +55,7 @@ var timezoneNames = map[int]string{ type configGetter interface { GetOrDefault(string, string) (string, error) + Get(string, string) (string, error) } // generic authenticated HTTP client for commands @@ -73,7 +74,7 @@ func NewHTTPClient(io *iostreams.IOStreams, cfg configGetter, appVersion string, // which would use that non-default behavior is right here, and it doesn't // seem worth the cognitive overhead everywhere else just to serve this one // use case. - unixSocket, err := cfg.GetOrDefault("", "http_unix_socket") + unixSocket, err := cfg.Get("", "http_unix_socket") if err != nil { return nil, err } @@ -92,7 +93,7 @@ func NewHTTPClient(io *iostreams.IOStreams, cfg configGetter, appVersion string, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", appVersion)), api.AddHeaderFunc("Authorization", func(req *http.Request) (string, error) { hostname := ghinstance.NormalizeHostname(getHost(req)) - if token, err := cfg.GetOrDefault(hostname, "oauth_token"); err == nil && token != "" { + if token, err := cfg.Get(hostname, "oauth_token"); err == nil && token != "" { return fmt.Sprintf("token %s", token), nil } return "", nil diff --git a/pkg/cmd/factory/http_test.go b/pkg/cmd/factory/http_test.go index f5e11bd1e..0039289a7 100644 --- a/pkg/cmd/factory/http_test.go +++ b/pkg/cmd/factory/http_test.go @@ -161,6 +161,10 @@ func (c tinyConfig) GetOrDefault(host, key string) (string, error) { return c[fmt.Sprintf("%s:%s", host, key)], nil } +func (c tinyConfig) Get(host, key string) (string, error) { + return c[fmt.Sprintf("%s:%s", host, key)], nil +} + var requestAtRE = regexp.MustCompile(`(?m)^\* Request at .+`) var dateRE = regexp.MustCompile(`(?m)^< Date: .+`) var hostWithPortRE = regexp.MustCompile(`127\.0\.0\.1:\d+`) diff --git a/pkg/cmd/factory/remote_resolver.go b/pkg/cmd/factory/remote_resolver.go index a197c41a2..44cd0242d 100644 --- a/pkg/cmd/factory/remote_resolver.go +++ b/pkg/cmd/factory/remote_resolver.go @@ -83,7 +83,7 @@ func (rr *remoteResolver) Resolver() func() (context.Remotes, error) { dummyHostname := "example.com" // any non-github.com hostname is fine here if config.IsHostEnv(src) { return nil, fmt.Errorf("none of the git remotes configured for this repository correspond to the %s environment variable. Try adding a matching remote or unsetting the variable.", src) - } else if v, src, _ := cfg.GetOrDefaultWithSource(dummyHostname, "oauth_token"); v != "" && config.IsEnterpriseEnv(src) { + } else if v, src, _ := cfg.GetWithSource(dummyHostname, "oauth_token"); v != "" && config.IsEnterpriseEnv(src) { return nil, errors.New("set the GH_HOST environment variable to specify which GitHub host to use") } return nil, errors.New("none of the git remotes configured for this repository point to a known GitHub host. To tell gh about a new GitHub host, please use `gh auth login`") diff --git a/pkg/cmdutil/auth_check.go b/pkg/cmdutil/auth_check.go index dc340dc51..3da9cfcfd 100644 --- a/pkg/cmdutil/auth_check.go +++ b/pkg/cmdutil/auth_check.go @@ -24,7 +24,7 @@ func CheckAuth(cfg config.Config) bool { } for _, hostname := range hosts { - token, _ := cfg.GetOrDefault(hostname, "oauth_token") + token, _ := cfg.Get(hostname, "oauth_token") if token != "" { return true } diff --git a/pkg/cmdutil/legacy.go b/pkg/cmdutil/legacy.go index 617d359b4..19400f1cc 100644 --- a/pkg/cmdutil/legacy.go +++ b/pkg/cmdutil/legacy.go @@ -16,7 +16,7 @@ func DetermineEditor(cf func() (config.Config, error)) (string, error) { if err != nil { return "", fmt.Errorf("could not read config: %w", err) } - editorCommand, _ = cfg.GetOrDefault("", "editor") + editorCommand, _ = cfg.Get("", "editor") } return editorCommand, nil