undo overzealous find/replace

This commit is contained in:
nate smith 2022-01-14 15:27:05 -06:00
parent 56522f9f14
commit 0a619d422a
18 changed files with 39 additions and 32 deletions

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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")

View file

@ -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 == "" {

View file

@ -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
}

View file

@ -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

View file

@ -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 := ""

View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -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{}

View file

@ -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)
})
}
}

View file

@ -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)
}

View file

@ -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

View file

@ -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+`)

View file

@ -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`")

View file

@ -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
}

View file

@ -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