Merge pull request #9324 from crosbymichael/git-urls
Move IsGIT and IsURL to urlutil package
This commit is contained in:
commit
201d4c614e
3 changed files with 92 additions and 0 deletions
30
urlutil/git.go
Normal file
30
urlutil/git.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package urlutil
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
var (
|
||||||
|
validPrefixes = []string{
|
||||||
|
"git://",
|
||||||
|
"github.com/",
|
||||||
|
"git@",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsGitURL returns true if the provided str is a git repository URL.
|
||||||
|
func IsGitURL(str string) bool {
|
||||||
|
if IsURL(str) && strings.HasSuffix(str, ".git") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
for _, prefix := range validPrefixes {
|
||||||
|
if strings.HasPrefix(str, prefix) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsGitTransport returns true if the provided str is a git transport by inspecting
|
||||||
|
// the prefix of the string for known protocols used in git.
|
||||||
|
func IsGitTransport(str string) bool {
|
||||||
|
return IsURL(str) || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@")
|
||||||
|
}
|
43
urlutil/git_test.go
Normal file
43
urlutil/git_test.go
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package urlutil
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
var (
|
||||||
|
gitUrls = []string{
|
||||||
|
"git://github.com/docker/docker",
|
||||||
|
"git@github.com:docker/docker.git",
|
||||||
|
"git@bitbucket.org:atlassianlabs/atlassian-docker.git",
|
||||||
|
"https://github.com/docker/docker.git",
|
||||||
|
"http://github.com/docker/docker.git",
|
||||||
|
}
|
||||||
|
incompleteGitUrls = []string{
|
||||||
|
"github.com/docker/docker",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestValidGitTransport(t *testing.T) {
|
||||||
|
for _, url := range gitUrls {
|
||||||
|
if IsGitTransport(url) == false {
|
||||||
|
t.Fatalf("%q should be detected as valid Git prefix", url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, url := range incompleteGitUrls {
|
||||||
|
if IsGitTransport(url) == true {
|
||||||
|
t.Fatalf("%q should not be detected as valid Git prefix", url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsGIT(t *testing.T) {
|
||||||
|
for _, url := range gitUrls {
|
||||||
|
if IsGitURL(url) == false {
|
||||||
|
t.Fatalf("%q should be detected as valid Git url", url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, url := range incompleteGitUrls {
|
||||||
|
if IsGitURL(url) == false {
|
||||||
|
t.Fatalf("%q should be detected as valid Git url", url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
urlutil/url.go
Normal file
19
urlutil/url.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package urlutil
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
var validUrlPrefixes = []string{
|
||||||
|
"http://",
|
||||||
|
"https://",
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsURL returns true if the provided str is a valid URL by doing
|
||||||
|
// a simple change for the transport of the url.
|
||||||
|
func IsURL(str string) bool {
|
||||||
|
for _, prefix := range validUrlPrefixes {
|
||||||
|
if strings.HasPrefix(str, prefix) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
Loading…
Reference in a new issue