From 0b95e7c8b52ceca93661babf6995aaf0455987b5 Mon Sep 17 00:00:00 2001 From: Taylor Blau Date: Thu, 13 Apr 2017 11:05:57 -0400 Subject: [PATCH] config/url_config: extract paths() function --- config/url_config.go | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/config/url_config.go b/config/url_config.go index d8fe1b34..243e1adc 100644 --- a/config/url_config.go +++ b/config/url_config.go @@ -36,24 +36,16 @@ func (c *URLConfig) get(key, rawurl string) (string, bool) { } hosts := c.hosts(u) + paths := c.paths(u.Path) - pLen := len(u.Path) - if pLen > 2 { - end := pLen - if strings.HasSuffix(u.Path, "/") { - end -= 1 - } - - paths := strings.Split(u.Path[1:end], "/") - 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, ok := c.git.Get(fmt.Sprintf("http.%s/%s/.%s", host, path, key)); ok { - return v, ok - } + 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, ok := c.git.Get(fmt.Sprintf("http.%s/%s/.%s", host, path, key)); ok { + return v, ok } } } @@ -80,3 +72,16 @@ func (c *URLConfig) hosts(u *url.URL) []string { return hosts } + +func (c *URLConfig) paths(path string) []string { + pLen := len(path) + if pLen <= 2 { + return nil + } + + end := pLen + if strings.HasSuffix(path, "/") { + end -= 1 + } + return strings.Split(path[1:end], "/") +}