lfsapi: initial httpconfig type for getting http.{url}.*
git config values
This commit is contained in:
parent
390edc6193
commit
34bdcb055d
68
lfsapi/httpconfig.go
Normal file
68
lfsapi/httpconfig.go
Normal file
@ -0,0 +1,68 @@
|
||||
package lfsapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type httpconfig struct {
|
||||
git Env
|
||||
}
|
||||
|
||||
// Get retrieves a `http.{url}.{key}` for the given key and urls, following the
|
||||
// rules in https://git-scm.com/docs/git-config#git-config-httplturlgt.
|
||||
// The value for `http.{key}` is returned as a fallback if no config keys are
|
||||
// set for the given urls.
|
||||
func (c *httpconfig) Get(key string, rawurl string) (string, bool) {
|
||||
key = strings.ToLower(key)
|
||||
if v, ok := c.get(key, rawurl); ok {
|
||||
return v, ok
|
||||
}
|
||||
return c.git.Get(fmt.Sprintf("http.%s", key))
|
||||
}
|
||||
|
||||
func (c *httpconfig) get(key, rawurl string) (string, bool) {
|
||||
u, err := url.Parse(rawurl)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
hosts := make([]string, 0)
|
||||
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))
|
||||
|
||||
pLen := len(u.Path)
|
||||
if pLen > 2 {
|
||||
end := pLen
|
||||
if strings.HasSuffix(u.Path, slash) {
|
||||
end -= 1
|
||||
}
|
||||
|
||||
paths := strings.Split(u.Path[1:end], slash)
|
||||
for i := len(paths); i > 0; i-- {
|
||||
for _, host := range hosts {
|
||||
path := strings.Join(paths[:i], slash)
|
||||
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 _, host := range hosts {
|
||||
if v, ok := c.git.Get(fmt.Sprintf("http.%s.%s", host, key)); ok {
|
||||
return v, ok
|
||||
}
|
||||
if v, ok := c.git.Get(fmt.Sprintf("http.%s/.%s", host, key)); ok {
|
||||
return v, ok
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
|
||||
}
|
32
lfsapi/httpconfig_test.go
Normal file
32
lfsapi/httpconfig_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
package lfsapi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHTTPConfig(t *testing.T) {
|
||||
c := &httpconfig{git: TestEnv(map[string]string{
|
||||
"http.key": "root",
|
||||
"http.https://host.com.key": "host",
|
||||
"http.https://user@host.com/a.key": "user-a",
|
||||
"http.https://user@host.com.key": "user",
|
||||
"http.https://host.com/a.key": "host-a",
|
||||
"http.https://host.com:8080.key": "port",
|
||||
})}
|
||||
|
||||
tests := map[string]string{
|
||||
"https://root.com/a/b/c": "root",
|
||||
"https://host.com/": "host",
|
||||
"https://host.com/a/b/c": "host-a",
|
||||
"https://user:pass@host.com/a/b/c": "user-a",
|
||||
"https://user:pass@host.com/z/b/c": "user",
|
||||
"https://host.com:8080/a": "port",
|
||||
}
|
||||
|
||||
for rawurl, expected := range tests {
|
||||
value, _ := c.Get("key", rawurl)
|
||||
assert.Equal(t, expected, value, rawurl)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user