Skip to content

Commit 1d9a20b

Browse files
committed
Move git code to its own pkg.
1 parent d953eca commit 1d9a20b

File tree

5 files changed

+129
-128
lines changed

5 files changed

+129
-128
lines changed

pkg/loader/gitcloner.go renamed to pkg/git/cloner.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package loader
17+
package git
1818

1919
import (
2020
"bytes"
@@ -27,8 +27,8 @@ import (
2727
"github.com/pkg/errors"
2828
)
2929

30-
// gitCloner is a function that can clone a git repo.
31-
type gitCloner func(url string) (
30+
// Cloner is a function that can clone a git repo.
31+
type Cloner func(url string) (
3232
// Directory where the repo is cloned to.
3333
checkoutDir string,
3434
// Relative path in the checkoutDir to location
@@ -37,8 +37,8 @@ type gitCloner func(url string) (
3737
// Any error encountered when cloning.
3838
err error)
3939

40-
// isRepoUrl checks if a string is likely a github repo Url.
41-
func isRepoUrl(arg string) bool {
40+
// IsRepoUrl checks if a string is likely a github repo Url.
41+
func IsRepoUrl(arg string) bool {
4242
arg = strings.ToLower(arg)
4343
return !filepath.IsAbs(arg) &&
4444
(strings.HasPrefix(arg, "git::") ||
@@ -54,7 +54,7 @@ func makeTmpDir() (string, error) {
5454
return ioutil.TempDir("", "kustomize-")
5555
}
5656

57-
func simpleGitCloner(spec string) (
57+
func ClonerUsingGitExec(spec string) (
5858
checkoutDir string, pathInCoDir string, err error) {
5959
gitProgram, err := exec.LookPath("git")
6060
if err != nil {

pkg/loader/gitcloner_test.go renamed to pkg/git/cloner_test.go

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package loader
17+
package git
1818

1919
import (
2020
"fmt"
2121
"path/filepath"
22-
"strings"
2322
"testing"
24-
25-
"sigs.k8s.io/kustomize/pkg/constants"
26-
"sigs.k8s.io/kustomize/pkg/fs"
2723
)
2824

2925
func TestIsRepoURL(t *testing.T) {
@@ -98,107 +94,13 @@ func TestIsRepoURL(t *testing.T) {
9894
},
9995
}
10096
for _, tc := range testcases {
101-
actual := isRepoUrl(tc.input)
97+
actual := IsRepoUrl(tc.input)
10298
if actual != tc.expected {
10399
t.Errorf("unexpected error: unexpected result %t for input %s", actual, tc.input)
104100
}
105101
}
106102
}
107103

108-
func splitOnNthSlash(v string, n int) (string, string) {
109-
left := ""
110-
for i := 0; i < n; i++ {
111-
k := strings.Index(v, "/")
112-
if k < 0 {
113-
break
114-
}
115-
left = left + v[:k+1]
116-
v = v[k+1:]
117-
}
118-
return left[:len(left)-1], v
119-
}
120-
121-
func TestSplit(t *testing.T) {
122-
path := "a/b/c/d/e/f/g"
123-
if left, right := splitOnNthSlash(path, 2); left != "a/b" || right != "c/d/e/f/g" {
124-
t.Fatalf("got left='%s', right='%s'", left, right)
125-
}
126-
if left, right := splitOnNthSlash(path, 3); left != "a/b/c" || right != "d/e/f/g" {
127-
t.Fatalf("got left='%s', right='%s'", left, right)
128-
}
129-
if left, right := splitOnNthSlash(path, 6); left != "a/b/c/d/e/f" || right != "g" {
130-
t.Fatalf("got left='%s', right='%s'", left, right)
131-
}
132-
}
133-
134-
// makeFakeGitCloner returns a cloner that ignores the
135-
// URL argument and returns a path in a fake file system
136-
// that should already hold the 'repo' contents.
137-
func makeFakeGitCloner(t *testing.T, fSys fs.FileSystem, coRoot string) gitCloner {
138-
if !fSys.IsDir(coRoot) {
139-
t.Fatalf("expecting a directory at '%s'", coRoot)
140-
}
141-
return func(url string) (
142-
checkoutDir string, pathInCoDir string, err error) {
143-
_, path := splitOnNthSlash(url, 3)
144-
if !fSys.IsDir(coRoot + "/" + path) {
145-
t.Fatalf("expecting a directory at '%s'/'%s'",
146-
coRoot, path)
147-
}
148-
return coRoot, path, nil
149-
}
150-
}
151-
152-
func TestGitLoader(t *testing.T) {
153-
rootUrl := "github.com/someOrg/someRepo"
154-
pathInRepo := "foo/base"
155-
url := rootUrl + "/" + pathInRepo
156-
if !isRepoUrl(url) {
157-
t.Fatalf("'%s' should be accepted as a repo url", url)
158-
}
159-
160-
coRoot := "/tmp"
161-
fSys := fs.MakeFakeFS()
162-
fSys.MkdirAll(coRoot)
163-
fSys.MkdirAll(coRoot + "/" + pathInRepo)
164-
fSys.WriteFile(
165-
coRoot+"/"+pathInRepo+"/"+constants.KustomizationFileNames[0],
166-
[]byte(`
167-
whatever
168-
`))
169-
l, err := newGitLoader(
170-
url, fSys, nil,
171-
makeFakeGitCloner(t, fSys, coRoot))
172-
if err != nil {
173-
t.Fatalf("unexpected err: %v\n", err)
174-
}
175-
if coRoot+"/"+pathInRepo != l.Root() {
176-
t.Fatalf("expected root '%s', got '%s'\n",
177-
coRoot+"/"+pathInRepo, l.Root())
178-
}
179-
if _, err = l.New(url); err == nil {
180-
t.Fatalf("expected cycle error 1")
181-
}
182-
if _, err = l.New(rootUrl + "/" + "foo"); err == nil {
183-
t.Fatalf("expected cycle error 2")
184-
}
185-
186-
pathInRepo = "foo/overlay"
187-
fSys.MkdirAll(coRoot + "/" + pathInRepo)
188-
url = rootUrl + "/" + pathInRepo
189-
if !isRepoUrl(url) {
190-
t.Fatalf("'%s' should be accepted as a repo url", url)
191-
}
192-
l2, err := l.New(url)
193-
if err != nil {
194-
t.Fatalf("unexpected error: %v", err)
195-
}
196-
if coRoot+"/"+pathInRepo != l2.Root() {
197-
t.Fatalf("expected root '%s', got '%s'\n",
198-
coRoot+"/"+pathInRepo, l2.Root())
199-
}
200-
}
201-
202104
var repoNames = []string{"someOrg/someRepo", "kubernetes/website"}
203105

204106
var paths = []string{"README.md", "foo/krusty.txt", ""}
@@ -231,7 +133,7 @@ func TestParseGithubUrl(t *testing.T) {
231133
if hrefArg != "" {
232134
input = input + refQuery + hrefArg
233135
}
234-
if !isRepoUrl(input) {
136+
if !IsRepoUrl(input) {
235137
t.Errorf("Should smell like github arg: %s\n", input)
236138
continue
237139
}

pkg/loader/fileloader.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"sigs.k8s.io/kustomize/pkg/fs"
26+
"sigs.k8s.io/kustomize/pkg/git"
2627
"sigs.k8s.io/kustomize/pkg/ifc"
2728
)
2829

@@ -91,7 +92,7 @@ type fileLoader struct {
9192
// File system utilities.
9293
fSys fs.FileSystem
9394
// Used to clone repositories.
94-
cloner gitCloner
95+
cloner git.Cloner
9596
// Used to clean up, as needed.
9697
cleaner func() error
9798
}
@@ -113,18 +114,18 @@ func (l *fileLoader) Root() string {
113114
}
114115

115116
func newLoaderOrDie(fSys fs.FileSystem, path string) *fileLoader {
116-
l, err := newFileLoaderAt(
117-
path, fSys, nil, simpleGitCloner)
117+
l, err := newLoaderAtConfirmedDir(
118+
path, fSys, nil, git.ClonerUsingGitExec)
118119
if err != nil {
119120
log.Fatalf("unable to make loader at '%s'; %v", path, err)
120121
}
121122
return l
122123
}
123124

124-
// newFileLoaderAt returns a new fileLoader with given root.
125-
func newFileLoaderAt(
125+
// newLoaderAtConfirmedDir returns a new fileLoader with given root.
126+
func newLoaderAtConfirmedDir(
126127
possibleRoot string, fSys fs.FileSystem,
127-
referrer *fileLoader, cloner gitCloner) (*fileLoader, error) {
128+
referrer *fileLoader, cloner git.Cloner) (*fileLoader, error) {
128129
if possibleRoot == "" {
129130
return nil, fmt.Errorf(
130131
"loader root cannot be empty")
@@ -159,25 +160,25 @@ func (l *fileLoader) New(path string) (ifc.Loader, error) {
159160
if path == "" {
160161
return nil, fmt.Errorf("new root cannot be empty")
161162
}
162-
if isRepoUrl(path) {
163+
if git.IsRepoUrl(path) {
163164
// Avoid cycles.
164165
if err := l.errIfPreviouslySeenUri(path); err != nil {
165166
return nil, err
166167
}
167-
return newGitLoader(path, l.fSys, l.referrer, l.cloner)
168+
return newLoaderAtGitClone(path, l.fSys, l.referrer, l.cloner)
168169
}
169170
if filepath.IsAbs(path) {
170171
return nil, fmt.Errorf("new root '%s' cannot be absolute", path)
171172
}
172-
return newFileLoaderAt(
173+
return newLoaderAtConfirmedDir(
173174
l.root.Join(path), l.fSys, l, l.cloner)
174175
}
175176

176-
// newGitLoader returns a new Loader pinned to a temporary
177+
// newLoaderAtGitClone returns a new Loader pinned to a temporary
177178
// directory holding a cloned git repo.
178-
func newGitLoader(
179+
func newLoaderAtGitClone(
179180
uri string, fSys fs.FileSystem,
180-
referrer *fileLoader, cloner gitCloner) (ifc.Loader, error) {
181+
referrer *fileLoader, cloner git.Cloner) (ifc.Loader, error) {
181182
tmpDirForRepo, pathInRepo, err := cloner(uri)
182183
if err != nil {
183184
return nil, err

pkg/loader/fileloader_test.go

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import (
2525
"strings"
2626
"testing"
2727

28+
"sigs.k8s.io/kustomize/pkg/constants"
29+
"sigs.k8s.io/kustomize/pkg/git"
30+
2831
"sigs.k8s.io/kustomize/pkg/fs"
2932
"sigs.k8s.io/kustomize/pkg/ifc"
3033
)
@@ -61,17 +64,17 @@ func MakeFakeFs(td []testData) fs.FileSystem {
6164
return fSys
6265
}
6366

64-
func TestNewFileLoaderAt_DemandsDirectory(t *testing.T) {
67+
func TestNewLoaderAtConfirmedDir_DemandsDirectory(t *testing.T) {
6568
fSys := MakeFakeFs(testCases)
66-
_, err := newFileLoaderAt("/foo", fSys, nil, nil)
69+
_, err := newLoaderAtConfirmedDir("/foo", fSys, nil, nil)
6770
if err != nil {
6871
t.Fatalf("Unexpected error - a directory should work.")
6972
}
70-
_, err = newFileLoaderAt("/foo/project", fSys, nil, nil)
73+
_, err = newLoaderAtConfirmedDir("/foo/project", fSys, nil, nil)
7174
if err != nil {
7275
t.Fatalf("Unexpected error - a directory should work.")
7376
}
74-
_, err = newFileLoaderAt("/foo/project/fileA.yaml", fSys, nil, nil)
77+
_, err = newLoaderAtConfirmedDir("/foo/project/fileA.yaml", fSys, nil, nil)
7578
if err == nil {
7679
t.Fatalf("Expected error - a file should not work.")
7780
}
@@ -321,3 +324,97 @@ func TestRestrictedLoadingInRealLoader(t *testing.T) {
321324
t.Fatalf("unexpected err: %v", err)
322325
}
323326
}
327+
328+
func splitOnNthSlash(v string, n int) (string, string) {
329+
left := ""
330+
for i := 0; i < n; i++ {
331+
k := strings.Index(v, "/")
332+
if k < 0 {
333+
break
334+
}
335+
left = left + v[:k+1]
336+
v = v[k+1:]
337+
}
338+
return left[:len(left)-1], v
339+
}
340+
341+
func TestSplit(t *testing.T) {
342+
path := "a/b/c/d/e/f/g"
343+
if left, right := splitOnNthSlash(path, 2); left != "a/b" || right != "c/d/e/f/g" {
344+
t.Fatalf("got left='%s', right='%s'", left, right)
345+
}
346+
if left, right := splitOnNthSlash(path, 3); left != "a/b/c" || right != "d/e/f/g" {
347+
t.Fatalf("got left='%s', right='%s'", left, right)
348+
}
349+
if left, right := splitOnNthSlash(path, 6); left != "a/b/c/d/e/f" || right != "g" {
350+
t.Fatalf("got left='%s', right='%s'", left, right)
351+
}
352+
}
353+
354+
// makeFakeGitCloner returns a cloner that ignores the
355+
// URL argument and returns a path in a fake file system
356+
// that should already hold the 'repo' contents.
357+
func makeFakeGitCloner(t *testing.T, fSys fs.FileSystem, coRoot string) git.Cloner {
358+
if !fSys.IsDir(coRoot) {
359+
t.Fatalf("expecting a directory at '%s'", coRoot)
360+
}
361+
return func(url string) (
362+
checkoutDir string, pathInCoDir string, err error) {
363+
_, path := splitOnNthSlash(url, 3)
364+
if !fSys.IsDir(coRoot + "/" + path) {
365+
t.Fatalf("expecting a directory at '%s'/'%s'",
366+
coRoot, path)
367+
}
368+
return coRoot, path, nil
369+
}
370+
}
371+
372+
func TestNewLoaderAtGitClone(t *testing.T) {
373+
rootUrl := "github.com/someOrg/someRepo"
374+
pathInRepo := "foo/base"
375+
url := rootUrl + "/" + pathInRepo
376+
if !git.IsRepoUrl(url) {
377+
t.Fatalf("'%s' should be accepted as a repo url", url)
378+
}
379+
380+
coRoot := "/tmp"
381+
fSys := fs.MakeFakeFS()
382+
fSys.MkdirAll(coRoot)
383+
fSys.MkdirAll(coRoot + "/" + pathInRepo)
384+
fSys.WriteFile(
385+
coRoot+"/"+pathInRepo+"/"+constants.KustomizationFileNames[0],
386+
[]byte(`
387+
whatever
388+
`))
389+
l, err := newLoaderAtGitClone(
390+
url, fSys, nil,
391+
makeFakeGitCloner(t, fSys, coRoot))
392+
if err != nil {
393+
t.Fatalf("unexpected err: %v\n", err)
394+
}
395+
if coRoot+"/"+pathInRepo != l.Root() {
396+
t.Fatalf("expected root '%s', got '%s'\n",
397+
coRoot+"/"+pathInRepo, l.Root())
398+
}
399+
if _, err = l.New(url); err == nil {
400+
t.Fatalf("expected cycle error 1")
401+
}
402+
if _, err = l.New(rootUrl + "/" + "foo"); err == nil {
403+
t.Fatalf("expected cycle error 2")
404+
}
405+
406+
pathInRepo = "foo/overlay"
407+
fSys.MkdirAll(coRoot + "/" + pathInRepo)
408+
url = rootUrl + "/" + pathInRepo
409+
if !git.IsRepoUrl(url) {
410+
t.Fatalf("'%s' should be accepted as a repo url", url)
411+
}
412+
l2, err := l.New(url)
413+
if err != nil {
414+
t.Fatalf("unexpected error: %v", err)
415+
}
416+
if coRoot+"/"+pathInRepo != l2.Root() {
417+
t.Fatalf("expected root '%s', got '%s'\n",
418+
coRoot+"/"+pathInRepo, l2.Root())
419+
}
420+
}

0 commit comments

Comments
 (0)