package config
import (
"fmt"
"net/url"
"regexp"
"strings"
)
type URLConfig struct {
git Environment
}
func NewURLConfig(git Environment) *URLConfig {
if git == nil {
git = EnvironmentOf(make(mapFetcher))
}
return &URLConfig{
git: git,
}
}
func (c *URLConfig) Get(prefix, rawurl, key string) (string, bool) {
if c == nil {
return "", false
}
key = strings.ToLower(key)
prefix = strings.ToLower(prefix)
if v := c.getAll(prefix, rawurl, key); len(v) > 0 {
return v[len(v)-1], true
}
return c.git.Get(strings.Join([]string{prefix, key}, "."))
}
func (c *URLConfig) GetAll(prefix, rawurl, key string) []string {
if c == nil {
return nil
}
key = strings.ToLower(key)
prefix = strings.ToLower(prefix)
if v := c.getAll(prefix, rawurl, key); len(v) > 0 {
return v
}
return c.git.GetAll(strings.Join([]string{prefix, key}, "."))
}
func (c *URLConfig) Bool(prefix, rawurl, key string, def bool) bool {
s, _ := c.Get(prefix, rawurl, key)
return Bool(s, def)
}
func (c *URLConfig) getAll(prefix, rawurl, key string) []string {
type urlMatch struct {
key string
hostScore int
pathScore int
userMatch int
}
searchURL, err := url.Parse(rawurl)
if err != nil {
return nil
}
config := c.git.All()
re := regexp.MustCompile(fmt.Sprintf(`\A%s\.(\S+)\.%s\z`, prefix, key))
bestMatch := urlMatch{
key: "",
hostScore: 0,
pathScore: 0,
userMatch: 0,
}
for k := range config {
matches := re.FindStringSubmatch(k)
if matches == nil {
continue
}
configURL, err := url.Parse(matches[1])
if err != nil {
continue
}
match := urlMatch{
key: k,
}
if searchURL.Scheme != configURL.Scheme {
continue
}
match.hostScore = compareHosts(searchURL.Hostname(), configURL.Hostname())
if match.hostScore == 0 {
continue
}
if match.hostScore < bestMatch.hostScore {
continue
}
if portForURL(searchURL) != portForURL(configURL) {
continue
}
match.pathScore = comparePaths(searchURL.Path, configURL.Path)
if match.pathScore == 0 {
continue
}
if configURL.User != nil {
if searchURL.User == nil {
continue
}
if searchURL.User.Username() != configURL.User.Username() {
continue
}
match.userMatch = 1
}
if match.hostScore > bestMatch.hostScore {
bestMatch = match
continue
}
if match.pathScore > bestMatch.pathScore {
bestMatch = match
continue
}
if match.pathScore == bestMatch.pathScore && match.userMatch > bestMatch.userMatch {
bestMatch = match
continue
}
}
if bestMatch.key == "" {
return nil
}
return c.git.GetAll(bestMatch.key)
}
func portForURL(u *url.URL) string {
port := u.Port()
if port != "" {
return port
}
switch u.Scheme {
case "http":
return "80"
case "https":
return "443"
case "ssh":
return "22"
default:
return ""
}
}
func compareHosts(searchHostname, configHostname string) int {
searchHost := strings.Split(searchHostname, ".")
configHost := strings.Split(configHostname, ".")
if len(searchHost) != len(configHost) {
return 0
}
score := len(searchHost) + 1
for i, subdomain := range searchHost {
if configHost[i] == "*" {
score--
continue
}
if subdomain != configHost[i] {
return 0
}
}
return score
}
func comparePaths(rawSearchPath, rawConfigPath string) int {
f := func(c rune) bool {
return c == '/'
}
searchPath := strings.FieldsFunc(rawSearchPath, f)
configPath := strings.FieldsFunc(rawConfigPath, f)
if len(searchPath) < len(configPath) {
return 0
}
score := 1
for i, element := range configPath {
searchElement := searchPath[i]
if element == searchElement {
score += 2
continue
}
if isDefaultLFSUrl(searchElement, searchPath, i+1) {
if searchElement[0:len(searchElement)-4] == element {
score++
continue
}
}
return 0
}
return score
}
func (c *URLConfig) hostsAndPaths(rawurl string) (hosts, paths []string) {
u, err := url.Parse(rawurl)
if err != nil {
return nil, nil
}
return c.hosts(u), c.paths(u.Path)
}
func (c *URLConfig) hosts(u *url.URL) []string {
hosts := make([]string, 0, 1)
if u.User != nil {
hosts = append(hosts, fmt.Sprintf("%s://%s@%s", u.Scheme, u.User.Username(), u.Host))
}
hosts = append(hosts, fmt.Sprintf("%s://%s", u.Scheme, u.Host))
return hosts
}
func (c *URLConfig) paths(path string) []string {
pLen := len(path)
if pLen <= 2 {
return nil
}
end := pLen
if strings.HasSuffix(path, slash) {
end--
}
return strings.Split(path[1:end], slash)
}
const (
gitExt = ".git"
infoPart = "info"
lfsPart = "lfs"
slash = "/"
)
func isDefaultLFSUrl(path string, parts []string, index int) bool {
if len(path) < 5 {
return false
}
if !strings.HasSuffix(path, gitExt) {
return false
}
if index > len(parts)-2 {
return false
}
return parts[index] == infoPart && parts[index+1] == lfsPart
}