Skip to content

Commit 9e5374e

Browse files
committed
Combine loaderImpl and fileLoader.
1 parent 4569a09 commit 9e5374e

File tree

8 files changed

+57
-85
lines changed

8 files changed

+57
-85
lines changed

pkg/commands/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (o *buildOptions) Validate(args []string) error {
7070

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

7575
absPath, err := filepath.Abs(o.kustomizationPath)
7676
if err != nil {

pkg/commands/configmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
6565
err = addConfigMap(
6666
kustomization, flagsAndArgs,
6767
configmapandsecret.NewConfigMapFactory(
68-
fSys, loader.NewLoader(loader.NewFileLoader(fSys))))
68+
fSys, loader.NewFileLoader(fSys)))
6969
if err != nil {
7070
return err
7171
}

pkg/commands/diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (o *diffOptions) Validate(args []string) error {
6868
// RunDiff gets the differences between Application.MakeCustomizedResMap() and Application.MakeUncustomizedResMap().
6969
func (o *diffOptions) RunDiff(out, errOut io.Writer, fSys fs.FileSystem) error {
7070

71-
l := loader.NewLoader(loader.NewFileLoader(fSys))
71+
l := loader.NewFileLoader(fSys)
7272

7373
absPath, err := filepath.Abs(o.kustomizationPath)
7474
if err != nil {

pkg/configmapandsecret/configmapfactory_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ func TestConstructConfigMap(t *testing.T) {
136136

137137
// TODO: all tests should use a FakeFs
138138
fSys := fs.MakeRealFS()
139-
f := NewConfigMapFactory(fSys,
140-
loader.NewLoader(loader.NewFileLoader(fSys)))
139+
f := NewConfigMapFactory(fSys, loader.NewFileLoader(fSys))
141140
for _, tc := range testCases {
142141
cm, err := f.MakeConfigMap(&tc.input)
143142
if err != nil {

pkg/internal/loadertest/fakeloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type FakeLoader struct {
3434
func NewFakeLoader(initialDir string) FakeLoader {
3535
// Create fake filesystem and inject it into initial Loader.
3636
fakefs := fs.MakeFakeFS()
37-
rootLoader := loader.NewLoader(loader.NewFileLoader(fakefs))
37+
rootLoader := loader.NewFileLoader(fakefs)
3838
ldr, _ := rootLoader.New(initialDir)
3939
return FakeLoader{fs: fakefs, delegate: ldr}
4040
}

pkg/loader/fileloader.go

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,59 @@ import (
2626

2727
const currentDir = "."
2828

29-
// FileLoader loads files from a file system.
30-
type FileLoader struct {
31-
fs fs.FileSystem
29+
// fileLoader loads files from a file system.
30+
type fileLoader struct {
31+
root string
32+
fSys fs.FileSystem
3233
}
3334

34-
// NewFileLoader returns a new FileLoader.
35-
func NewFileLoader(fs fs.FileSystem) *FileLoader {
36-
return &FileLoader{fs: fs}
35+
// NewFileLoader returns a new fileLoader.
36+
func NewFileLoader(fSys fs.FileSystem) *fileLoader {
37+
return newFileLoaderAtRoot("", fSys)
38+
}
39+
40+
// newFileLoaderAtRoot returns a new fileLoader with given root.
41+
func newFileLoaderAtRoot(root string, fs fs.FileSystem) *fileLoader {
42+
return &fileLoader{root: root, fSys: fs}
43+
}
44+
45+
// Root returns the root location for this Loader.
46+
func (l *fileLoader) Root() string {
47+
return l.root
48+
}
49+
50+
// Returns a new Loader rooted at newRoot. "newRoot" MUST be
51+
// a directory (not a file). The directory can have a trailing
52+
// slash or not.
53+
// Example: "/home/seans/project" or "/home/seans/project/"
54+
// NOT "/home/seans/project/file.yaml".
55+
func (l *fileLoader) New(newRoot string) (Loader, error) {
56+
if !l.IsAbsPath(l.root, newRoot) {
57+
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
58+
}
59+
root, err := l.fullLocation(l.root, newRoot)
60+
if err != nil {
61+
return nil, err
62+
}
63+
return newFileLoaderAtRoot(root, l.fSys), nil
3764
}
3865

3966
// IsAbsPath return true if the location calculated with the root
4067
// and location params a full file path.
41-
func (l *FileLoader) IsAbsPath(root string, location string) bool {
42-
fullFilePath, err := l.FullLocation(root, location)
68+
func (l *fileLoader) IsAbsPath(root string, location string) bool {
69+
fullFilePath, err := l.fullLocation(root, location)
4370
if err != nil {
4471
return false
4572
}
4673
return filepath.IsAbs(fullFilePath)
4774
}
4875

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

75102
// Load returns the bytes from reading a file at fullFilePath.
76103
// Implements the Loader interface.
77-
func (l *FileLoader) Load(p string) ([]byte, error) {
78-
return l.fs.ReadFile(p)
104+
func (l *fileLoader) Load(location string) ([]byte, error) {
105+
fullLocation, err := l.fullLocation(l.root, location)
106+
if err != nil {
107+
fmt.Printf("Trouble in fulllocation: %v\n", err)
108+
return nil, err
109+
}
110+
return l.fSys.ReadFile(fullLocation)
79111
}
80112

81113
// GlobLoad returns the map from path to bytes from reading a glob path.
82114
// Implements the Loader interface.
83-
func (l *FileLoader) GlobLoad(p string) (map[string][]byte, error) {
84-
return l.fs.ReadFiles(p)
115+
func (l *fileLoader) GlobLoad(location string) (map[string][]byte, error) {
116+
fullLocation, err := l.fullLocation(l.root, location)
117+
if err != nil {
118+
return nil, err
119+
}
120+
return l.fSys.ReadFiles(fullLocation)
85121
}

pkg/loader/loader_test.go renamed to pkg/loader/fileloader_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,14 @@ import (
2424
"github.com/kubernetes-sigs/kustomize/pkg/fs"
2525
)
2626

27-
func initializeRootLoader(fakefs fs.FileSystem) Loader {
28-
return NewLoader(NewFileLoader(fakefs))
29-
}
30-
3127
func TestLoader_Root(t *testing.T) {
3228

3329
// Initialize the fake file system and the root loader.
3430
fakefs := fs.MakeFakeFS()
3531
fakefs.WriteFile("/home/seans/project/file.yaml", []byte("Unused"))
3632
fakefs.WriteFile("/home/seans/project/subdir/file.yaml", []byte("Unused"))
3733
fakefs.WriteFile("/home/seans/project2/file.yaml", []byte("Unused"))
38-
rootLoader := initializeRootLoader(fakefs)
34+
rootLoader := NewFileLoader(fakefs)
3935

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

9490
loader, err := rootLoader.New("/home/seans/project")
9591
if err != nil {

pkg/loader/loader.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
// Package loader has a data loading interface and various implementations.
1818
package loader
1919

20-
import "fmt"
21-
2220
// Loader interface exposes methods to read bytes.
2321
type Loader interface {
2422
// Root returns the root location for this Loader.
@@ -30,60 +28,3 @@ type Loader interface {
3028
// GlobLoad returns the bytes read from a glob path or an error.
3129
GlobLoad(location string) (map[string][]byte, error)
3230
}
33-
34-
// Private implementation of Loader interface.
35-
type loaderImpl struct {
36-
root string
37-
fLoader *FileLoader
38-
}
39-
40-
const emptyRoot = ""
41-
42-
// NewLoader initializes the first loader with the supported fLoader.
43-
func NewLoader(fl *FileLoader) Loader {
44-
return &loaderImpl{root: emptyRoot, fLoader: fl}
45-
}
46-
47-
// Root returns the root location for this Loader.
48-
func (l *loaderImpl) Root() string {
49-
return l.root
50-
}
51-
52-
// Returns a new Loader rooted at newRoot. "newRoot" MUST be
53-
// a directory (not a file). The directory can have a trailing
54-
// slash or not.
55-
// Example: "/home/seans/project" or "/home/seans/project/"
56-
// NOT "/home/seans/project/file.yaml".
57-
func (l *loaderImpl) New(newRoot string) (Loader, error) {
58-
if !l.fLoader.IsAbsPath(l.root, newRoot) {
59-
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
60-
}
61-
root, err := l.fLoader.FullLocation(l.root, newRoot)
62-
if err != nil {
63-
return nil, err
64-
}
65-
return &loaderImpl{root: root, fLoader: l.fLoader}, nil
66-
}
67-
68-
// Load returns all the bytes read from location or an error.
69-
// "location" can be an absolute path, or if relative, full location is
70-
// calculated from the Root().
71-
func (l *loaderImpl) Load(location string) ([]byte, error) {
72-
fullLocation, err := l.fLoader.FullLocation(l.root, location)
73-
if err != nil {
74-
fmt.Printf("Trouble in fulllocation: %v\n", err)
75-
return nil, err
76-
}
77-
return l.fLoader.Load(fullLocation)
78-
}
79-
80-
// GlobLoad returns a map from path to bytes read from the location or an error.
81-
// "location" can be an absolute path, or if relative, full location is
82-
// calculated from the Root().
83-
func (l *loaderImpl) GlobLoad(location string) (map[string][]byte, error) {
84-
fullLocation, err := l.fLoader.FullLocation(l.root, location)
85-
if err != nil {
86-
return nil, err
87-
}
88-
return l.fLoader.GlobLoad(fullLocation)
89-
}

0 commit comments

Comments
 (0)