Skip to content

Commit

Permalink
combine fileLoader.New and NewLoader into one function: NewLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Liujingfang1 committed Aug 15, 2018
1 parent f018370 commit 7485292
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (o *buildOptions) Validate(args []string) error {

// RunBuild runs build command.
func (o *buildOptions) RunBuild(out io.Writer, fSys fs.FileSystem) error {
rootLoader, err := loader.NewLoader(o.kustomizationPath, fSys)
rootLoader, err := loader.NewLoader(o.kustomizationPath, "", fSys)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (o *diffOptions) Validate(args []string) error {
// RunDiff gets the differences between Application.MakeCustomizedResMap() and Application.MakeUncustomizedResMap().
func (o *diffOptions) RunDiff(out, errOut io.Writer, fSys fs.FileSystem) error {

rootLoader, err := loader.NewLoader(o.kustomizationPath, fSys)
rootLoader, err := loader.NewLoader(o.kustomizationPath, "", fSys)
if err != nil {
return err
}
Expand Down
12 changes: 1 addition & 11 deletions pkg/loader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,7 @@ func (l *fileLoader) Root() string {
// Example: "/home/seans/project" or "/home/seans/project/"
// NOT "/home/seans/project/file.yaml".
func (l *fileLoader) New(newRoot string) (Loader, error) {
if isRepoUrl(newRoot) {
return newGithubLoader(newRoot, l.fSys)
}
if !l.IsAbsPath(l.root, newRoot) {
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
}
root, err := l.fullLocation(l.root, newRoot)
if err != nil {
return nil, err
}
return newFileLoaderAtRoot(root, l.fSys), nil
return NewLoader(newRoot, l.root, l.fSys)
}

// IsAbsPath return true if the location calculated with the root
Expand Down
2 changes: 1 addition & 1 deletion pkg/loader/fileloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestLoader_Root(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected in New(): %v\n", err)
}
if "/home/seans/project/" != loader.Root() {
if "/home/seans/project" != loader.Root() {
t.Fatalf("Incorrect Loader Root: %s\n", loader.Root())
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/loader/githubloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
type githubLoader struct {
repo string
checkoutDir string
fSys fs.FileSystem
loader *fileLoader
}

Expand Down Expand Up @@ -70,7 +69,6 @@ func newGithubLoader(repoUrl string, fs fs.FileSystem) (*githubLoader, error) {
return &githubLoader{
repo: repoUrl,
checkoutDir: target,
fSys: fs,
loader: l,
}, nil
}
Expand Down
33 changes: 24 additions & 9 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package loader

import (
"fmt"
"path/filepath"

"github.com/kubernetes-sigs/kustomize/pkg/fs"
"path/filepath"
)

// Loader interface exposes methods to read bytes.
Expand All @@ -38,23 +38,38 @@ type Loader interface {

// NewLoader returns a Loader given a target
// The target can be a local disk directory or a github Url
func NewLoader(target string, fSys fs.FileSystem) (Loader, error) {
func NewLoader(target, r string, fSys fs.FileSystem) (Loader, error) {
if !isValidLoaderPath(target, r) {
return nil, fmt.Errorf("Not valid path: root='%s', loc='%s'\n", r, target)
}

if isRepoUrl(target) {
return newGithubLoader(target, fSys)
}

l := NewFileLoader(fSys)
absPath, err := filepath.Abs(target)
if err != nil {
return nil, err
l := newFileLoaderAtRoot(r, fSys)
if isRootLoaderPath(r) {
absPath, err := filepath.Abs(target)
if err != nil {
return nil, err
}
target = absPath
}

if !l.IsAbsPath(l.root, absPath) {
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, absPath)
if !l.IsAbsPath(l.root, target) {
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, target)
}
root, err := l.fullLocation(l.root, absPath)
root, err := l.fullLocation(l.root, target)
if err != nil {
return nil, err
}
return newFileLoaderAtRoot(root, l.fSys), nil
}

func isValidLoaderPath(target, root string) bool {
return target != "" || root != ""
}

func isRootLoaderPath(root string) bool {
return root == ""
}

0 comments on commit 7485292

Please sign in to comment.