Skip to content

Commit

Permalink
Combine loaderImpl and fileLoader.
Browse files Browse the repository at this point in the history
  • Loading branch information
monopole committed Jul 26, 2018
1 parent 4569a09 commit 9e5374e
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 85 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/build.go
Expand Up @@ -70,7 +70,7 @@ func (o *buildOptions) Validate(args []string) error {

// RunBuild runs build command.
func (o *buildOptions) RunBuild(out io.Writer, fSys fs.FileSystem) error {
l := loader.NewLoader(loader.NewFileLoader(fSys))
l := loader.NewFileLoader(fSys)

absPath, err := filepath.Abs(o.kustomizationPath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/configmap.go
Expand Up @@ -65,7 +65,7 @@ func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
err = addConfigMap(
kustomization, flagsAndArgs,
configmapandsecret.NewConfigMapFactory(
fSys, loader.NewLoader(loader.NewFileLoader(fSys))))
fSys, loader.NewFileLoader(fSys)))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/diff.go
Expand Up @@ -68,7 +68,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 {

l := loader.NewLoader(loader.NewFileLoader(fSys))
l := loader.NewFileLoader(fSys)

absPath, err := filepath.Abs(o.kustomizationPath)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/configmapandsecret/configmapfactory_test.go
Expand Up @@ -136,8 +136,7 @@ func TestConstructConfigMap(t *testing.T) {

// TODO: all tests should use a FakeFs
fSys := fs.MakeRealFS()
f := NewConfigMapFactory(fSys,
loader.NewLoader(loader.NewFileLoader(fSys)))
f := NewConfigMapFactory(fSys, loader.NewFileLoader(fSys))
for _, tc := range testCases {
cm, err := f.MakeConfigMap(&tc.input)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/loadertest/fakeloader.go
Expand Up @@ -34,7 +34,7 @@ type FakeLoader struct {
func NewFakeLoader(initialDir string) FakeLoader {
// Create fake filesystem and inject it into initial Loader.
fakefs := fs.MakeFakeFS()
rootLoader := loader.NewLoader(loader.NewFileLoader(fakefs))
rootLoader := loader.NewFileLoader(fakefs)
ldr, _ := rootLoader.New(initialDir)
return FakeLoader{fs: fakefs, delegate: ldr}
}
Expand Down
64 changes: 50 additions & 14 deletions pkg/loader/fileloader.go
Expand Up @@ -26,32 +26,59 @@ import (

const currentDir = "."

// FileLoader loads files from a file system.
type FileLoader struct {
fs fs.FileSystem
// fileLoader loads files from a file system.
type fileLoader struct {
root string
fSys fs.FileSystem
}

// NewFileLoader returns a new FileLoader.
func NewFileLoader(fs fs.FileSystem) *FileLoader {
return &FileLoader{fs: fs}
// NewFileLoader returns a new fileLoader.
func NewFileLoader(fSys fs.FileSystem) *fileLoader {
return newFileLoaderAtRoot("", fSys)
}

// newFileLoaderAtRoot returns a new fileLoader with given root.
func newFileLoaderAtRoot(root string, fs fs.FileSystem) *fileLoader {
return &fileLoader{root: root, fSys: fs}
}

// Root returns the root location for this Loader.
func (l *fileLoader) Root() string {
return l.root
}

// Returns a new Loader rooted at newRoot. "newRoot" MUST be
// a directory (not a file). The directory can have a trailing
// slash or not.
// Example: "/home/seans/project" or "/home/seans/project/"
// NOT "/home/seans/project/file.yaml".
func (l *fileLoader) New(newRoot string) (Loader, error) {
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
}

// IsAbsPath return true if the location calculated with the root
// and location params a full file path.
func (l *FileLoader) IsAbsPath(root string, location string) bool {
fullFilePath, err := l.FullLocation(root, location)
func (l *fileLoader) IsAbsPath(root string, location string) bool {
fullFilePath, err := l.fullLocation(root, location)
if err != nil {
return false
}
return filepath.IsAbs(fullFilePath)
}

// FullLocation returns some notion of a full path.
// fullLocation returns some notion of a full path.
// If location is a full file path, then ignore root. If location is relative, then
// join the root path with the location path. Either root or location can be empty,
// but not both. Special case for ".": Expands to current working directory.
// Example: "/home/seans/project", "subdir/bar" -> "/home/seans/project/subdir/bar".
func (l *FileLoader) FullLocation(root string, location string) (string, error) {
func (l *fileLoader) fullLocation(root string, location string) (string, error) {
// First, validate the parameters
if len(root) == 0 && len(location) == 0 {
return "", fmt.Errorf("unable to calculate full location: root and location empty")
Expand All @@ -74,12 +101,21 @@ func (l *FileLoader) FullLocation(root string, location string) (string, error)

// Load returns the bytes from reading a file at fullFilePath.
// Implements the Loader interface.
func (l *FileLoader) Load(p string) ([]byte, error) {
return l.fs.ReadFile(p)
func (l *fileLoader) Load(location string) ([]byte, error) {
fullLocation, err := l.fullLocation(l.root, location)
if err != nil {
fmt.Printf("Trouble in fulllocation: %v\n", err)
return nil, err
}
return l.fSys.ReadFile(fullLocation)
}

// GlobLoad returns the map from path to bytes from reading a glob path.
// Implements the Loader interface.
func (l *FileLoader) GlobLoad(p string) (map[string][]byte, error) {
return l.fs.ReadFiles(p)
func (l *fileLoader) GlobLoad(location string) (map[string][]byte, error) {
fullLocation, err := l.fullLocation(l.root, location)
if err != nil {
return nil, err
}
return l.fSys.ReadFiles(fullLocation)
}
8 changes: 2 additions & 6 deletions pkg/loader/loader_test.go → pkg/loader/fileloader_test.go
Expand Up @@ -24,18 +24,14 @@ import (
"github.com/kubernetes-sigs/kustomize/pkg/fs"
)

func initializeRootLoader(fakefs fs.FileSystem) Loader {
return NewLoader(NewFileLoader(fakefs))
}

func TestLoader_Root(t *testing.T) {

// Initialize the fake file system and the root loader.
fakefs := fs.MakeFakeFS()
fakefs.WriteFile("/home/seans/project/file.yaml", []byte("Unused"))
fakefs.WriteFile("/home/seans/project/subdir/file.yaml", []byte("Unused"))
fakefs.WriteFile("/home/seans/project2/file.yaml", []byte("Unused"))
rootLoader := initializeRootLoader(fakefs)
rootLoader := NewFileLoader(fakefs)

_, err := rootLoader.New("")
if err == nil {
Expand Down Expand Up @@ -89,7 +85,7 @@ func TestLoader_Load(t *testing.T) {
fakefs.WriteFile("/home/seans/project/file.yaml", []byte("This is a yaml file"))
fakefs.WriteFile("/home/seans/project/subdir/file.yaml", []byte("Subdirectory file content"))
fakefs.WriteFile("/home/seans/project2/file.yaml", []byte("This is another yaml file"))
rootLoader := initializeRootLoader(fakefs)
rootLoader := NewFileLoader(fakefs)

loader, err := rootLoader.New("/home/seans/project")
if err != nil {
Expand Down
59 changes: 0 additions & 59 deletions pkg/loader/loader.go
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
// Package loader has a data loading interface and various implementations.
package loader

import "fmt"

// Loader interface exposes methods to read bytes.
type Loader interface {
// Root returns the root location for this Loader.
Expand All @@ -30,60 +28,3 @@ type Loader interface {
// GlobLoad returns the bytes read from a glob path or an error.
GlobLoad(location string) (map[string][]byte, error)
}

// Private implementation of Loader interface.
type loaderImpl struct {
root string
fLoader *FileLoader
}

const emptyRoot = ""

// NewLoader initializes the first loader with the supported fLoader.
func NewLoader(fl *FileLoader) Loader {
return &loaderImpl{root: emptyRoot, fLoader: fl}
}

// Root returns the root location for this Loader.
func (l *loaderImpl) Root() string {
return l.root
}

// Returns a new Loader rooted at newRoot. "newRoot" MUST be
// a directory (not a file). The directory can have a trailing
// slash or not.
// Example: "/home/seans/project" or "/home/seans/project/"
// NOT "/home/seans/project/file.yaml".
func (l *loaderImpl) New(newRoot string) (Loader, error) {
if !l.fLoader.IsAbsPath(l.root, newRoot) {
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
}
root, err := l.fLoader.FullLocation(l.root, newRoot)
if err != nil {
return nil, err
}
return &loaderImpl{root: root, fLoader: l.fLoader}, nil
}

// Load returns all the bytes read from location or an error.
// "location" can be an absolute path, or if relative, full location is
// calculated from the Root().
func (l *loaderImpl) Load(location string) ([]byte, error) {
fullLocation, err := l.fLoader.FullLocation(l.root, location)
if err != nil {
fmt.Printf("Trouble in fulllocation: %v\n", err)
return nil, err
}
return l.fLoader.Load(fullLocation)
}

// GlobLoad returns a map from path to bytes read from the location or an error.
// "location" can be an absolute path, or if relative, full location is
// calculated from the Root().
func (l *loaderImpl) GlobLoad(location string) (map[string][]byte, error) {
fullLocation, err := l.fLoader.FullLocation(l.root, location)
if err != nil {
return nil, err
}
return l.fLoader.GlobLoad(fullLocation)
}

0 comments on commit 9e5374e

Please sign in to comment.