pkg/urlutil: deprecate, and move to builder/remotecontext/urlutil
pkg/urlutil (despite its poorly chosen name) is not really intended as a generic utility to handle URLs, and should only be used by the builder to handle (remote) build contexts. - IsURL() only does a very rudimentary check for http(s):// prefixes, without any other validation, but due to its name may give incorrect expectations. - IsGitURL() is written specifically with docker build remote git contexts in mind, and has handling for backward-compatibility, where strings that are not URLs, but start with "github.com/" are accepted. Because of the above, this patch: - moves the package inside builder/remotecontext, close to where it's intended to be used (ideally this would be part of build/remotecontext itself, but this package imports many other dependencies, which would introduce those as extra dependencies in the CLI). - deprecates pkg/urlutil, but adds aliases as there are some external consumers. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
46
cli/command/image/build/internal/urlutil/urlutil.go
Normal file
46
cli/command/image/build/internal/urlutil/urlutil.go
Normal file
@ -0,0 +1,46 @@
|
||||
// Package urlutil provides helper function to check urls kind.
|
||||
// It supports http and git urls.
|
||||
package urlutil // import "github.com/docker/docker/builder/remotecontext/urlutil"
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
validPrefixes = map[string][]string{
|
||||
"url": {"http://", "https://"},
|
||||
|
||||
// The github.com/ prefix is a special case used to treat context-paths
|
||||
// starting with `github.com` as a git URL if the given path does not
|
||||
// exist locally. The "github.com/" prefix is kept for backward compatibility,
|
||||
// and is a legacy feature.
|
||||
//
|
||||
// Going forward, no additional prefixes should be added, and users should
|
||||
// be encouraged to use explicit URLs (https://github.com/user/repo.git) instead.
|
||||
"git": {"git://", "github.com/", "git@"},
|
||||
}
|
||||
urlPathWithFragmentSuffix = regexp.MustCompile(".git(?:#.+)?$")
|
||||
)
|
||||
|
||||
// IsURL returns true if the provided str is an HTTP(S) URL.
|
||||
func IsURL(str string) bool {
|
||||
return checkURL(str, "url")
|
||||
}
|
||||
|
||||
// IsGitURL returns true if the provided str is a git repository URL.
|
||||
func IsGitURL(str string) bool {
|
||||
if IsURL(str) && urlPathWithFragmentSuffix.MatchString(str) {
|
||||
return true
|
||||
}
|
||||
return checkURL(str, "git")
|
||||
}
|
||||
|
||||
func checkURL(str, kind string) bool {
|
||||
for _, prefix := range validPrefixes[kind] {
|
||||
if strings.HasPrefix(str, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
41
cli/command/image/build/internal/urlutil/urlutil_test.go
Normal file
41
cli/command/image/build/internal/urlutil/urlutil_test.go
Normal file
@ -0,0 +1,41 @@
|
||||
package urlutil // import "github.com/docker/docker/builder/remotecontext/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",
|
||||
"http://github.com/docker/docker.git#branch",
|
||||
"http://github.com/docker/docker.git#:dir",
|
||||
}
|
||||
incompleteGitUrls = []string{
|
||||
"github.com/docker/docker",
|
||||
}
|
||||
invalidGitUrls = []string{
|
||||
"http://github.com/docker/docker.git:#branch",
|
||||
}
|
||||
)
|
||||
|
||||
func TestIsGIT(t *testing.T) {
|
||||
for _, url := range gitUrls {
|
||||
if !IsGitURL(url) {
|
||||
t.Fatalf("%q should be detected as valid Git url", url)
|
||||
}
|
||||
}
|
||||
|
||||
for _, url := range incompleteGitUrls {
|
||||
if !IsGitURL(url) {
|
||||
t.Fatalf("%q should be detected as valid Git url", url)
|
||||
}
|
||||
}
|
||||
|
||||
for _, url := range invalidGitUrls {
|
||||
if IsGitURL(url) {
|
||||
t.Fatalf("%q should not be detected as valid Git prefix", url)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user