config/url_config: implement GetAll on URLConfig type
This commit is contained in:
parent
473ccb7a17
commit
5936cf4f29
@ -23,39 +23,47 @@ func NewURLConfig(git Environment) *URLConfig {
|
|||||||
func (c *URLConfig) Get(prefix, key string, rawurl string) (string, bool) {
|
func (c *URLConfig) Get(prefix, key string, rawurl string) (string, bool) {
|
||||||
key = strings.ToLower(key)
|
key = strings.ToLower(key)
|
||||||
prefix = strings.ToLower(prefix)
|
prefix = strings.ToLower(prefix)
|
||||||
if v, ok := c.get(key, rawurl); ok {
|
if v := c.getAll(key, rawurl); len(v) > 0 {
|
||||||
return v, ok
|
return v[0], true
|
||||||
}
|
}
|
||||||
return c.git.Get(strings.Join([]string{prefix, key}, "."))
|
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)
|
hosts, paths := c.hostsAndPaths(rawurl)
|
||||||
|
|
||||||
for i := len(paths); i > 0; i-- {
|
for i := len(paths); i > 0; i-- {
|
||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
path := strings.Join(paths[:i], "/")
|
path := strings.Join(paths[:i], "/")
|
||||||
if v, ok := c.git.Get(fmt.Sprintf("http.%s/%s.%s", host, path, key)); ok {
|
if v := c.git.GetAll(fmt.Sprintf("http.%s/%s.%s", host, path, key)); len(v) > 0 {
|
||||||
return v, ok
|
return v
|
||||||
}
|
}
|
||||||
if v, ok := c.git.Get(fmt.Sprintf("http.%s/%s/.%s", host, path, key)); ok {
|
if v := c.git.GetAll(fmt.Sprintf("http.%s/%s/.%s", host, path, key)); len(v) > 0 {
|
||||||
return v, ok
|
return v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
if v, ok := c.git.Get(fmt.Sprintf("http.%s.%s", host, key)); ok {
|
if v := c.git.GetAll(fmt.Sprintf("http.%s.%s", host, key)); len(v) > 0 {
|
||||||
return v, ok
|
return v
|
||||||
}
|
}
|
||||||
if v, ok := c.git.Get(fmt.Sprintf("http.%s/.%s", host, key)); ok {
|
if v := c.git.GetAll(fmt.Sprintf("http.%s/.%s", host, key)); len(v) > 0 {
|
||||||
return v, ok
|
return v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", false
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *URLConfig) hostsAndPaths(rawurl string) (hosts, paths []string) {
|
func (c *URLConfig) hostsAndPaths(rawurl string) (hosts, paths []string) {
|
||||||
u, err := url.Parse(rawurl)
|
u, err := url.Parse(rawurl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -8,15 +8,15 @@ import (
|
|||||||
|
|
||||||
func TestURLConfig(t *testing.T) {
|
func TestURLConfig(t *testing.T) {
|
||||||
u := NewURLConfig(EnvironmentOf(MapFetcher(map[string][]string{
|
u := NewURLConfig(EnvironmentOf(MapFetcher(map[string][]string{
|
||||||
"http.key": []string{"root"},
|
"http.key": []string{"root", "root-2"},
|
||||||
"http.https://host.com.key": []string{"host"},
|
"http.https://host.com.key": []string{"host", "host-2"},
|
||||||
"http.https://user@host.com/a.key": []string{"user-a"},
|
"http.https://user@host.com/a.key": []string{"user-a", "user-b"},
|
||||||
"http.https://user@host.com.key": []string{"user"},
|
"http.https://user@host.com.key": []string{"user", "user-2"},
|
||||||
"http.https://host.com/a.key": []string{"host-a"},
|
"http.https://host.com/a.key": []string{"host-a", "host-b"},
|
||||||
"http.https://host.com:8080.key": []string{"port"},
|
"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://root.com/a/b/c": "root",
|
||||||
"https://host.com/": "host",
|
"https://host.com/": "host",
|
||||||
"https://host.com/a/b/c": "host-a",
|
"https://host.com/a/b/c": "host-a",
|
||||||
@ -25,8 +25,22 @@ func TestURLConfig(t *testing.T) {
|
|||||||
"https://host.com:8080/a": "port",
|
"https://host.com:8080/a": "port",
|
||||||
}
|
}
|
||||||
|
|
||||||
for rawurl, expected := range tests {
|
for rawurl, expected := range getOne {
|
||||||
value, _ := u.Get("http", "key", rawurl)
|
value, _ := u.Get("http", "key", rawurl)
|
||||||
assert.Equal(t, expected, value, 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user