Browse code

Move git and url checks into pkg

This moves the IsGIT and IsURL functions out of the generic `utils`
package and into their own `urlutil` pkg.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

Michael Crosby authored on 2014/11/25 08:10:37
Showing 5 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,30 @@
0
+package urlutil
1
+
2
+import "strings"
3
+
4
+var (
5
+	validPrefixes = []string{
6
+		"git://",
7
+		"github.com/",
8
+		"git@",
9
+	}
10
+)
11
+
12
+// IsGitURL returns true if the provided str is a git repository URL.
13
+func IsGitURL(str string) bool {
14
+	if IsURL(str) && strings.HasSuffix(str, ".git") {
15
+		return true
16
+	}
17
+	for _, prefix := range validPrefixes {
18
+		if strings.HasPrefix(str, prefix) {
19
+			return true
20
+		}
21
+	}
22
+	return false
23
+}
24
+
25
+// IsGitTransport returns true if the provided str is a git transport by inspecting
26
+// the prefix of the string for known protocols used in git.
27
+func IsGitTransport(str string) bool {
28
+	return IsURL(str) || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@")
29
+}
0 30
new file mode 100644
... ...
@@ -0,0 +1,43 @@
0
+package urlutil
1
+
2
+import "testing"
3
+
4
+var (
5
+	gitUrls = []string{
6
+		"git://github.com/docker/docker",
7
+		"git@github.com:docker/docker.git",
8
+		"git@bitbucket.org:atlassianlabs/atlassian-docker.git",
9
+		"https://github.com/docker/docker.git",
10
+		"http://github.com/docker/docker.git",
11
+	}
12
+	incompleteGitUrls = []string{
13
+		"github.com/docker/docker",
14
+	}
15
+)
16
+
17
+func TestValidGitTransport(t *testing.T) {
18
+	for _, url := range gitUrls {
19
+		if IsGitTransport(url) == false {
20
+			t.Fatalf("%q should be detected as valid Git prefix", url)
21
+		}
22
+	}
23
+
24
+	for _, url := range incompleteGitUrls {
25
+		if IsGitTransport(url) == true {
26
+			t.Fatalf("%q should not be detected as valid Git prefix", url)
27
+		}
28
+	}
29
+}
30
+
31
+func TestIsGIT(t *testing.T) {
32
+	for _, url := range gitUrls {
33
+		if IsGitURL(url) == false {
34
+			t.Fatalf("%q should be detected as valid Git url", url)
35
+		}
36
+	}
37
+	for _, url := range incompleteGitUrls {
38
+		if IsGitURL(url) == false {
39
+			t.Fatalf("%q should be detected as valid Git url", url)
40
+		}
41
+	}
42
+}
0 43
new file mode 100644
... ...
@@ -0,0 +1,19 @@
0
+package urlutil
1
+
2
+import "strings"
3
+
4
+var validUrlPrefixes = []string{
5
+	"http://",
6
+	"https://",
7
+}
8
+
9
+// IsURL returns true if the provided str is a valid URL by doing
10
+// a simple change for the transport of the url.
11
+func IsURL(str string) bool {
12
+	for _, prefix := range validUrlPrefixes {
13
+		if strings.HasPrefix(str, prefix) {
14
+			return true
15
+		}
16
+	}
17
+	return false
18
+}
... ...
@@ -288,21 +288,7 @@ func NewHTTPRequestError(msg string, res *http.Response) error {
288 288
 	}
289 289
 }
290 290
 
291
-func IsURL(str string) bool {
292
-	return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
293
-}
294
-
295
-func IsGIT(str string) bool {
296
-	return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/") || strings.HasPrefix(str, "git@") || (strings.HasSuffix(str, ".git") && IsURL(str))
297
-}
298
-
299
-func ValidGitTransport(str string) bool {
300
-	return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "git@") || IsURL(str)
301
-}
302
-
303
-var (
304
-	localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`)
305
-)
291
+var localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`)
306 292
 
307 293
 // RemoveLocalDns looks into the /etc/resolv.conf,
308 294
 // and removes any local nameserver entries.
... ...
@@ -97,43 +97,3 @@ func TestReadSymlinkedDirectoryToFile(t *testing.T) {
97 97
 		t.Errorf("failed to remove symlink: %s", err)
98 98
 	}
99 99
 }
100
-
101
-var (
102
-	gitUrls = []string{
103
-		"git://github.com/docker/docker",
104
-		"git@github.com:docker/docker.git",
105
-		"git@bitbucket.org:atlassianlabs/atlassian-docker.git",
106
-		"https://github.com/docker/docker.git",
107
-		"http://github.com/docker/docker.git",
108
-	}
109
-	incompleteGitUrls = []string{
110
-		"github.com/docker/docker",
111
-	}
112
-)
113
-
114
-func TestValidGitTransport(t *testing.T) {
115
-	for _, url := range gitUrls {
116
-		if ValidGitTransport(url) == false {
117
-			t.Fatalf("%q should be detected as valid Git prefix", url)
118
-		}
119
-	}
120
-
121
-	for _, url := range incompleteGitUrls {
122
-		if ValidGitTransport(url) == true {
123
-			t.Fatalf("%q should not be detected as valid Git prefix", url)
124
-		}
125
-	}
126
-}
127
-
128
-func TestIsGIT(t *testing.T) {
129
-	for _, url := range gitUrls {
130
-		if IsGIT(url) == false {
131
-			t.Fatalf("%q should be detected as valid Git url", url)
132
-		}
133
-	}
134
-	for _, url := range incompleteGitUrls {
135
-		if IsGIT(url) == false {
136
-			t.Fatalf("%q should be detected as valid Git url", url)
137
-		}
138
-	}
139
-}