Skip to content

Commit

Permalink
Avoid erroring when looking up config keys for nonexistent host
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Aug 12, 2020
1 parent 55a13a3 commit 7908c21
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
25 changes: 18 additions & 7 deletions internal/config/config_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"bytes"
"errors"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -71,17 +70,29 @@ github.com:
eq(t, token, "OTOKEN")
}

func Test_parseConfig_notFound(t *testing.T) {
func Test_parseConfig_hostFallback(t *testing.T) {
defer StubConfig(`---
hosts:
example.com:
git_protocol: ssh
`, `---
github.com:
user: monalisa
oauth_token: OTOKEN
example.com:
user: wronguser
oauth_token: NOTTHIS
`, "")()
git_protocol: https
`)()
config, err := ParseConfig("config.yml")
eq(t, err, nil)
_, err = config.Get("github.com", "user")
eq(t, err, &NotFoundError{errors.New(`could not find config entry for "github.com"`)})
val, err := config.Get("example.com", "git_protocol")
eq(t, err, nil)
eq(t, val, "https")
val, err = config.Get("github.com", "git_protocol")
eq(t, err, nil)
eq(t, val, "ssh")
val, err = config.Get("nonexist.io", "git_protocol")
eq(t, err, nil)
eq(t, val, "ssh")
}

func Test_ParseConfig_migrateConfig(t *testing.T) {
Expand Down
17 changes: 10 additions & 7 deletions internal/config/config_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,21 @@ func (c *fileConfig) Root() *yaml.Node {

func (c *fileConfig) Get(hostname, key string) (string, error) {
if hostname != "" {
hostCfg, err := c.configForHost(hostname)
if err != nil {
return "", err
}

hostValue, err := hostCfg.GetStringValue(key)
var notFound *NotFoundError

hostCfg, err := c.configForHost(hostname)
if err != nil && !errors.As(err, &notFound) {
return "", err
}

var hostValue string
if hostCfg != nil {
hostValue, err = hostCfg.GetStringValue(key)
if err != nil && !errors.As(err, &notFound) {
return "", err
}
}

if hostValue != "" {
return hostValue, nil
}
Expand Down Expand Up @@ -385,7 +388,7 @@ func (c *fileConfig) hostEntries() ([]*HostConfig, error) {
return hostConfigs, nil
}

// Hosts returns a list of all known hostnames configred in hosts.yml
// Hosts returns a list of all known hostnames configured in hosts.yml
func (c *fileConfig) Hosts() ([]string, error) {
entries, err := c.hostEntries()
if err != nil {
Expand Down

0 comments on commit 7908c21

Please sign in to comment.