Skip to content

Commit f0deaf7

Browse files
authored
Merge pull request #287 from emosbaugh/fix-gopath-isrepourl
Fix loader loader when run from gopath
2 parents 1cf9131 + e113944 commit f0deaf7

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

pkg/loader/githubloader.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ func newGithubLoader(repoUrl string, fs fs.FileSystem) (*githubLoader, error) {
7575

7676
// isRepoUrl checks if a string is a repo Url
7777
func isRepoUrl(s string) bool {
78-
return strings.Contains(s, ".com") || strings.Contains(s, ".org") || strings.Contains(s, "https://")
78+
if strings.HasPrefix(s, "https://") {
79+
return true
80+
}
81+
host := strings.SplitN(s, "/", 2)[0]
82+
return strings.Contains(host, ".com") || strings.Contains(host, ".org")
7983
}
8084

8185
// Checkout clones a github repo with specified commit/tag/branch

pkg/loader/githubloader_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package loader
18+
19+
import (
20+
"testing"
21+
)
22+
23+
func TestIsRepoURL(t *testing.T) {
24+
25+
testcases := []struct {
26+
input string
27+
expected bool
28+
}{
29+
{
30+
input: "https://github.com/org/repo",
31+
expected: true,
32+
},
33+
{
34+
input: "github.com/org/repo",
35+
expected: true,
36+
},
37+
{
38+
input: "/github.com/org/repo",
39+
expected: false,
40+
},
41+
{
42+
input: "/abs/path/to/file",
43+
expected: false,
44+
},
45+
{
46+
input: "../relative",
47+
expected: false,
48+
},
49+
}
50+
for _, tc := range testcases {
51+
actual := isRepoUrl(tc.input)
52+
if actual != tc.expected {
53+
t.Errorf("unexpected error: unexpected result %t for input %s", actual, tc.input)
54+
}
55+
}
56+
}

pkg/loader/loader.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func NewLoader(target, r string, fSys fs.FileSystem) (Loader, error) {
4343
return nil, fmt.Errorf("Not valid path: root='%s', loc='%s'\n", r, target)
4444
}
4545

46-
if isRepoUrl(target) {
46+
if !isLocalTarget(target, fSys) && isRepoUrl(target) {
4747
return newGithubLoader(target, fSys)
4848
}
4949

@@ -73,3 +73,8 @@ func isValidLoaderPath(target, root string) bool {
7373
func isRootLoaderPath(root string) bool {
7474
return root == ""
7575
}
76+
77+
// isLocalTarget checks if a file exists in the filesystem
78+
func isLocalTarget(s string, fs fs.FileSystem) bool {
79+
return fs.Exists(s)
80+
}

0 commit comments

Comments
 (0)