diff --git a/config/url_config.go b/config/url_config.go index ad3a899d..1302c796 100644 --- a/config/url_config.go +++ b/config/url_config.go @@ -23,39 +23,47 @@ func NewURLConfig(git Environment) *URLConfig { func (c *URLConfig) Get(prefix, key string, rawurl string) (string, bool) { key = strings.ToLower(key) prefix = strings.ToLower(prefix) - if v, ok := c.get(key, rawurl); ok { - return v, ok + if v := c.getAll(key, rawurl); len(v) > 0 { + return v[0], true } return c.git.Get(strings.Join([]string{prefix, key}, ".")) } -func (c *URLConfig) get(key, rawurl string) (string, bool) { +func (c *URLConfig) GetAll(prefix, key string, rawurl string) []string { + key = strings.ToLower(key) + prefix = strings.ToLower(prefix) + if v := c.getAll(key, rawurl); len(v) > 0 { + return v + } + return c.git.GetAll(strings.Join([]string{prefix, key}, ".")) +} + +func (c *URLConfig) getAll(key, rawurl string) []string { hosts, paths := c.hostsAndPaths(rawurl) for i := len(paths); i > 0; i-- { for _, host := range hosts { path := strings.Join(paths[:i], "/") - if v, ok := c.git.Get(fmt.Sprintf("http.%s/%s.%s", host, path, key)); ok { - return v, ok + if v := c.git.GetAll(fmt.Sprintf("http.%s/%s.%s", host, path, key)); len(v) > 0 { + return v } - if v, ok := c.git.Get(fmt.Sprintf("http.%s/%s/.%s", host, path, key)); ok { - return v, ok + if v := c.git.GetAll(fmt.Sprintf("http.%s/%s/.%s", host, path, key)); len(v) > 0 { + return v } } } for _, host := range hosts { - if v, ok := c.git.Get(fmt.Sprintf("http.%s.%s", host, key)); ok { - return v, ok + if v := c.git.GetAll(fmt.Sprintf("http.%s.%s", host, key)); len(v) > 0 { + return v } - if v, ok := c.git.Get(fmt.Sprintf("http.%s/.%s", host, key)); ok { - return v, ok + if v := c.git.GetAll(fmt.Sprintf("http.%s/.%s", host, key)); len(v) > 0 { + return v } } - return "", false + return nil } - func (c *URLConfig) hostsAndPaths(rawurl string) (hosts, paths []string) { u, err := url.Parse(rawurl) if err != nil { diff --git a/config/url_config_test.go b/config/url_config_test.go index d80aa8d8..9971586f 100644 --- a/config/url_config_test.go +++ b/config/url_config_test.go @@ -8,15 +8,15 @@ import ( func TestURLConfig(t *testing.T) { u := NewURLConfig(EnvironmentOf(MapFetcher(map[string][]string{ - "http.key": []string{"root"}, - "http.https://host.com.key": []string{"host"}, - "http.https://user@host.com/a.key": []string{"user-a"}, - "http.https://user@host.com.key": []string{"user"}, - "http.https://host.com/a.key": []string{"host-a"}, - "http.https://host.com:8080.key": []string{"port"}, + "http.key": []string{"root", "root-2"}, + "http.https://host.com.key": []string{"host", "host-2"}, + "http.https://user@host.com/a.key": []string{"user-a", "user-b"}, + "http.https://user@host.com.key": []string{"user", "user-2"}, + "http.https://host.com/a.key": []string{"host-a", "host-b"}, + "http.https://host.com:8080.key": []string{"port", "port-2"}, }))) - tests := map[string]string{ + getOne := map[string]string{ "https://root.com/a/b/c": "root", "https://host.com/": "host", "https://host.com/a/b/c": "host-a", @@ -25,8 +25,22 @@ func TestURLConfig(t *testing.T) { "https://host.com:8080/a": "port", } - for rawurl, expected := range tests { + for rawurl, expected := range getOne { value, _ := u.Get("http", "key", rawurl) assert.Equal(t, expected, value, rawurl) } + + getAll := map[string][]string{ + "https://root.com/a/b/c": []string{"root", "root-2"}, + "https://host.com/": []string{"host", "host-2"}, + "https://host.com/a/b/c": []string{"host-a", "host-b"}, + "https://user:pass@host.com/a/b/c": []string{"user-a", "user-b"}, + "https://user:pass@host.com/z/b/c": []string{"user", "user-2"}, + "https://host.com:8080/a": []string{"port", "port-2"}, + } + + for rawurl, expected := range getAll { + values := u.GetAll("http", "key", rawurl) + assert.Equal(t, expected, values, rawurl) + } }