Jelajahi Sumber

Merge pull request #9324 from crosbymichael/git-urls

Move IsGIT and IsURL to urlutil package
Jessie Frazelle 10 tahun lalu
induk
melakukan
2ad98b0cfa
8 mengubah file dengan 103 tambahan dan 62 penghapusan
  1. 4 3
      api/client/commands.go
  2. 2 1
      builder/internals.go
  3. 4 3
      builder/job.go
  4. 30 0
      pkg/urlutil/git.go
  5. 43 0
      pkg/urlutil/git_test.go
  6. 19 0
      pkg/urlutil/url.go
  7. 1 15
      utils/utils.go
  8. 0 40
      utils/utils_test.go

+ 4 - 3
api/client/commands.go

@@ -38,6 +38,7 @@ import (
 	"github.com/docker/docker/pkg/term"
 	"github.com/docker/docker/pkg/timeutils"
 	"github.com/docker/docker/pkg/units"
+	"github.com/docker/docker/pkg/urlutil"
 	"github.com/docker/docker/registry"
 	"github.com/docker/docker/runconfig"
 	"github.com/docker/docker/utils"
@@ -115,13 +116,13 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
 		} else {
 			context = ioutil.NopCloser(buf)
 		}
-	} else if utils.IsURL(cmd.Arg(0)) && (!utils.IsGIT(cmd.Arg(0)) || !hasGit) {
+	} else if urlutil.IsURL(cmd.Arg(0)) && (!urlutil.IsGitURL(cmd.Arg(0)) || !hasGit) {
 		isRemote = true
 	} else {
 		root := cmd.Arg(0)
-		if utils.IsGIT(root) {
+		if urlutil.IsGitURL(root) {
 			remoteURL := cmd.Arg(0)
-			if !utils.ValidGitTransport(remoteURL) {
+			if !urlutil.IsGitTransport(remoteURL) {
 				remoteURL = "https://" + remoteURL
 			}
 

+ 2 - 1
builder/internals.go

@@ -28,6 +28,7 @@ import (
 	"github.com/docker/docker/pkg/symlink"
 	"github.com/docker/docker/pkg/system"
 	"github.com/docker/docker/pkg/tarsum"
+	"github.com/docker/docker/pkg/urlutil"
 	"github.com/docker/docker/registry"
 	"github.com/docker/docker/utils"
 )
@@ -215,7 +216,7 @@ func calcCopyInfo(b *Builder, cmdName string, cInfos *[]*copyInfo, origPath stri
 	origPath = strings.TrimPrefix(origPath, "./")
 
 	// In the remote/URL case, download it and gen its hashcode
-	if utils.IsURL(origPath) {
+	if urlutil.IsURL(origPath) {
 		if !allowRemote {
 			return fmt.Errorf("Source can't be a URL for %s", cmdName)
 		}

+ 4 - 3
builder/job.go

@@ -11,6 +11,7 @@ import (
 	"github.com/docker/docker/graph"
 	"github.com/docker/docker/pkg/archive"
 	"github.com/docker/docker/pkg/parsers"
+	"github.com/docker/docker/pkg/urlutil"
 	"github.com/docker/docker/registry"
 	"github.com/docker/docker/utils"
 )
@@ -58,8 +59,8 @@ func (b *BuilderJob) CmdBuild(job *engine.Job) engine.Status {
 
 	if remoteURL == "" {
 		context = ioutil.NopCloser(job.Stdin)
-	} else if utils.IsGIT(remoteURL) {
-		if !utils.ValidGitTransport(remoteURL) {
+	} else if urlutil.IsGitURL(remoteURL) {
+		if !urlutil.IsGitTransport(remoteURL) {
 			remoteURL = "https://" + remoteURL
 		}
 		root, err := ioutil.TempDir("", "docker-build-git")
@@ -77,7 +78,7 @@ func (b *BuilderJob) CmdBuild(job *engine.Job) engine.Status {
 			return job.Error(err)
 		}
 		context = c
-	} else if utils.IsURL(remoteURL) {
+	} else if urlutil.IsURL(remoteURL) {
 		f, err := utils.Download(remoteURL)
 		if err != nil {
 			return job.Error(err)

+ 30 - 0
pkg/urlutil/git.go

@@ -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 - 0
pkg/urlutil/git_test.go

@@ -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 - 0
pkg/urlutil/url.go

@@ -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
+}

+ 1 - 15
utils/utils.go

@@ -288,21 +288,7 @@ func NewHTTPRequestError(msg string, res *http.Response) error {
 	}
 }
 
-func IsURL(str string) bool {
-	return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
-}
-
-func IsGIT(str string) bool {
-	return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/") || strings.HasPrefix(str, "git@") || (strings.HasSuffix(str, ".git") && IsURL(str))
-}
-
-func ValidGitTransport(str string) bool {
-	return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@") || IsURL(str)
-}
-
-var (
-	localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`)
-)
+var localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`)
 
 // RemoveLocalDns looks into the /etc/resolv.conf,
 // and removes any local nameserver entries.

+ 0 - 40
utils/utils_test.go

@@ -97,43 +97,3 @@ func TestReadSymlinkedDirectoryToFile(t *testing.T) {
 		t.Errorf("failed to remove symlink: %s", err)
 	}
 }
-
-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 ValidGitTransport(url) == false {
-			t.Fatalf("%q should be detected as valid Git prefix", url)
-		}
-	}
-
-	for _, url := range incompleteGitUrls {
-		if ValidGitTransport(url) == true {
-			t.Fatalf("%q should not be detected as valid Git prefix", url)
-		}
-	}
-}
-
-func TestIsGIT(t *testing.T) {
-	for _, url := range gitUrls {
-		if IsGIT(url) == false {
-			t.Fatalf("%q should be detected as valid Git url", url)
-		}
-	}
-	for _, url := range incompleteGitUrls {
-		if IsGIT(url) == false {
-			t.Fatalf("%q should be detected as valid Git url", url)
-		}
-	}
-}