@@ -23,15 +23,16 @@ import (
23
23
// FilesystemPackageLoader loads packages by walking file system tree.
24
24
type FilesystemPackageLoader struct {
25
25
* log.Logger
26
- Context types.Variables
27
- pathContexts map [string ]types.Variables
28
- multiErr * multierror.Error
29
- pkgFile * v1alpha2.Pkgfile
30
- Root string
31
- absRootPath string
32
- pkgFilePaths []string
33
- varFilePaths []string
34
- pkgs []* v1alpha2.Pkg
26
+ Context types.Variables
27
+ pathContexts map [string ]types.Variables
28
+ multiErr * multierror.Error
29
+ pkgFile * v1alpha2.Pkgfile
30
+ Root string
31
+ absRootPath string
32
+ pkgFilePaths []string
33
+ varFilePaths []string
34
+ templateFilePaths []string
35
+ pkgs []* v1alpha2.Pkg
35
36
}
36
37
37
38
func (fspl * FilesystemPackageLoader ) walkFunc () filepath.WalkFunc {
@@ -50,11 +51,13 @@ func (fspl *FilesystemPackageLoader) walkFunc() filepath.WalkFunc {
50
51
return nil
51
52
}
52
53
53
- switch info . Name () {
54
- case constants .PkgYaml :
54
+ switch {
55
+ case info . Name () == constants .PkgYaml :
55
56
fspl .pkgFilePaths = append (fspl .pkgFilePaths , path )
56
- case constants .VarsYaml :
57
+ case info . Name () == constants .VarsYaml :
57
58
fspl .varFilePaths = append (fspl .varFilePaths , path )
59
+ case strings .HasSuffix (info .Name (), constants .TemplateExt ):
60
+ fspl .templateFilePaths = append (fspl .templateFilePaths , path )
58
61
}
59
62
60
63
return nil
@@ -115,6 +118,17 @@ func (fspl *FilesystemPackageLoader) Load() (*LoadResult, error) {
115
118
fspl .Printf ("loaded pkg %q from %q" , pkg .Name , path )
116
119
fspl .pkgs = append (fspl .pkgs , pkg )
117
120
}
121
+
122
+ for _ , path := range fspl .templateFilePaths {
123
+ var pkg * v1alpha2.Pkg
124
+
125
+ if pkg , err = fspl .attachTemplate (path ); err != nil {
126
+ fspl .Printf ("error attaching template %q: %s" , path , err )
127
+ fspl .multiErr = multierror .Append (fspl .multiErr , fmt .Errorf ("error attaching template %q: %w" , path , err ))
128
+ } else {
129
+ fspl .Printf ("attached template %q to %q" , path , pkg .Name )
130
+ }
131
+ }
118
132
}
119
133
120
134
return & LoadResult {
@@ -197,6 +211,38 @@ func (fspl *FilesystemPackageLoader) loadPkg(path string) (*v1alpha2.Pkg, error)
197
211
return v1alpha2 .NewPkg (filepath .Dir (basePath ), path , contents , context )
198
212
}
199
213
214
+ func (fspl * FilesystemPackageLoader ) attachTemplate (path string ) (* v1alpha2.Pkg , error ) {
215
+ // find the closest pkgs in relative path
216
+ var (
217
+ closestPkg * v1alpha2.Pkg
218
+ shortestRel string
219
+ )
220
+
221
+ for _ , pkg := range fspl .pkgs {
222
+ rel , err := filepath .Rel (pkg .BaseDir , filepath .Dir (path ))
223
+ if err != nil || strings .HasPrefix (rel , ".." ) {
224
+ continue
225
+ }
226
+
227
+ if shortestRel == "" || len (rel ) < len (shortestRel ) {
228
+ closestPkg = pkg
229
+ shortestRel = rel
230
+ }
231
+ }
232
+
233
+ if closestPkg == nil {
234
+ return nil , fmt .Errorf ("no suitable package found for template %q" , path )
235
+ }
236
+
237
+ content , err := os .ReadFile (path )
238
+ if err != nil {
239
+ return nil , err
240
+ }
241
+
242
+ // attach the template to the closest package
243
+ return closestPkg , closestPkg .AttachTemplatedFile (filepath .Join (shortestRel , filepath .Base (path )), content )
244
+ }
245
+
200
246
func (fspl * FilesystemPackageLoader ) loadPkgfile () error {
201
247
f , err := os .Open (filepath .Join (fspl .Root , constants .Pkgfile ))
202
248
if err != nil {
0 commit comments