combine fileLoader.New and NewLoader into one function: NewLoader

This commit is contained in:
Jingfang Liu
2018-08-15 11:01:48 -07:00
parent f018370628
commit 72b1a4bc5c
6 changed files with 27 additions and 24 deletions

View File

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

View File

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

View File

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

View File

@@ -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())
}

View File

@@ -31,7 +31,6 @@ import (
type githubLoader struct {
repo string
checkoutDir string
fSys fs.FileSystem
loader *fileLoader
}
@@ -70,7 +69,6 @@ func newGithubLoader(repoUrl string, fs fs.FileSystem) (*githubLoader, error) {
return &githubLoader{
repo: repoUrl,
checkoutDir: target,
fSys: fs,
loader: l,
}, nil
}

View File

@@ -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 == ""
}