@@ -26,32 +26,59 @@ import (
26
26
27
27
const currentDir = "."
28
28
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
32
33
}
33
34
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
37
64
}
38
65
39
66
// IsAbsPath return true if the location calculated with the root
40
67
// 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 )
43
70
if err != nil {
44
71
return false
45
72
}
46
73
return filepath .IsAbs (fullFilePath )
47
74
}
48
75
49
- // FullLocation returns some notion of a full path.
76
+ // fullLocation returns some notion of a full path.
50
77
// If location is a full file path, then ignore root. If location is relative, then
51
78
// join the root path with the location path. Either root or location can be empty,
52
79
// but not both. Special case for ".": Expands to current working directory.
53
80
// 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 ) {
55
82
// First, validate the parameters
56
83
if len (root ) == 0 && len (location ) == 0 {
57
84
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)
74
101
75
102
// Load returns the bytes from reading a file at fullFilePath.
76
103
// 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 )
79
111
}
80
112
81
113
// GlobLoad returns the map from path to bytes from reading a glob path.
82
114
// 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 )
85
121
}
0 commit comments