@@ -79,17 +79,21 @@ func (c *DiskCatalog) GetTemplatePath(target string) ([]string, error) {
79
79
}
80
80
81
81
// try to handle deprecated template paths
82
- absPath := BackwardsCompatiblePaths (c .templatesDirectory , target )
83
- if absPath != target && strings .TrimPrefix (absPath , c .templatesDirectory + string (filepath .Separator )) != target {
84
- if config .DefaultConfig .LogAllEvents {
85
- gologger .DefaultLogger .Print ().Msgf ("[%v] requested Template path %s is deprecated, please update to %s\n " , aurora .Yellow ("WRN" ).String (), target , absPath )
82
+ absPath := target
83
+ if c .templatesFS == nil {
84
+ absPath = BackwardsCompatiblePaths (c .templatesDirectory , target )
85
+ if absPath != target && strings .TrimPrefix (absPath , c .templatesDirectory + string (filepath .Separator )) != target {
86
+ if config .DefaultConfig .LogAllEvents {
87
+ gologger .DefaultLogger .Print ().Msgf ("[%v] requested Template path %s is deprecated, please update to %s\n " , aurora .Yellow ("WRN" ).String (), target , absPath )
88
+ }
89
+ deprecatedPathsCounter ++
86
90
}
87
- deprecatedPathsCounter ++
88
- }
89
91
90
- absPath , err := c .convertPathToAbsolute (absPath )
91
- if err != nil {
92
- return nil , errors .Wrapf (err , "could not find template file" )
92
+ var err error
93
+ absPath , err = c .convertPathToAbsolute (absPath )
94
+ if err != nil {
95
+ return nil , errors .Wrapf (err , "could not find template file" )
96
+ }
93
97
}
94
98
95
99
// Template input is either a file or a directory
@@ -143,24 +147,60 @@ func (c *DiskCatalog) findGlobPathMatches(absPath string, processed map[string]s
143
147
if c .templatesDirectory == "" {
144
148
templateDir = "./"
145
149
}
146
- matches , _ := fs .Glob (os .DirFS (filepath .Join (templateDir , "http" )), inputGlob )
147
- if len (matches ) != 0 {
150
+
151
+ if c .templatesFS == nil {
152
+ matches , _ := fs .Glob (os .DirFS (filepath .Join (templateDir , "http" )), inputGlob )
153
+ if len (matches ) != 0 {
154
+ return matches
155
+ }
156
+
157
+ // condition to support network cve related globs
158
+ matches , _ = fs .Glob (os .DirFS (filepath .Join (templateDir , "network" )), inputGlob )
159
+ return matches
160
+ } else {
161
+ sub , err := fs .Sub (c .templatesFS , filepath .Join (templateDir , "http" ))
162
+ if err != nil {
163
+ return nil
164
+ }
165
+ matches , _ := fs .Glob (sub , inputGlob )
166
+ if len (matches ) != 0 {
167
+ return matches
168
+ }
169
+
170
+ // condition to support network cve related globs
171
+ sub , err = fs .Sub (c .templatesFS , filepath .Join (templateDir , "network" ))
172
+ if err != nil {
173
+ return nil
174
+ }
175
+ matches , _ = fs .Glob (sub , inputGlob )
148
176
return matches
149
177
}
150
- // condition to support network cve related globs
151
- matches , _ = fs .Glob (os .DirFS (filepath .Join (templateDir , "network" )), inputGlob )
152
- return matches
153
178
}
154
179
155
180
var matched []string
156
- matches , err := fs .Glob (c .templatesFS , relPath )
157
- if len (matches ) != 0 {
158
- matched = append (matched , matches ... )
181
+ var matches []string
182
+ if c .templatesFS == nil {
183
+ var err error
184
+ matches , err = filepath .Glob (relPath )
185
+ if len (matches ) != 0 {
186
+ matched = append (matched , matches ... )
187
+ } else {
188
+ matched = append (matched , OldPathsResolver (relPath )... )
189
+ }
190
+ if err != nil && len (matched ) == 0 {
191
+ return nil , errors .Errorf ("wildcard found, but unable to glob: %s\n " , err )
192
+ }
159
193
} else {
160
- matched = append (matched , OldPathsResolver (relPath )... )
161
- }
162
- if err != nil && len (matched ) == 0 {
163
- return nil , errors .Errorf ("wildcard found, but unable to glob: %s\n " , err )
194
+ var err error
195
+ matches , err = fs .Glob (c .templatesFS , relPath )
196
+ if len (matches ) != 0 {
197
+ matched = append (matched , matches ... )
198
+ } else {
199
+ matched = append (matched , OldPathsResolver (relPath )... )
200
+ }
201
+ if err != nil && len (matched ) == 0 {
202
+ return nil , errors .Errorf ("wildcard found, but unable to glob: %s\n " , err )
203
+ }
164
204
}
165
205
results := make ([]string , 0 , len (matches ))
166
206
for _ , match := range matches {
@@ -175,11 +215,23 @@ func (c *DiskCatalog) findGlobPathMatches(absPath string, processed map[string]s
175
215
// findFileMatches finds if a path is an absolute file. If the path
176
216
// is a file, it returns true otherwise false with no errors.
177
217
func (c * DiskCatalog ) findFileMatches (absPath string , processed map [string ]struct {}) (match string , matched bool , err error ) {
178
- info , err := os .Stat (absPath )
218
+ if c .templatesFS != nil {
219
+ absPath = strings .TrimPrefix (absPath , "/" )
220
+ }
221
+ var info fs.File
222
+ if c .templatesFS == nil {
223
+ info , err = os .Open (absPath )
224
+ } else {
225
+ info , err = c .templatesFS .Open (absPath )
226
+ }
227
+ if err != nil {
228
+ return "" , false , err
229
+ }
230
+ stat , err := info .Stat ()
179
231
if err != nil {
180
232
return "" , false , err
181
233
}
182
- if ! info .Mode ().IsRegular () {
234
+ if ! stat .Mode ().IsRegular () {
183
235
return "" , false , nil
184
236
}
185
237
if _ , ok := processed [absPath ]; ! ok {
@@ -192,22 +244,43 @@ func (c *DiskCatalog) findFileMatches(absPath string, processed map[string]struc
192
244
// findDirectoryMatches finds matches for templates from a directory
193
245
func (c * DiskCatalog ) findDirectoryMatches (absPath string , processed map [string ]struct {}) ([]string , error ) {
194
246
var results []string
195
- err := filepath .WalkDir (
196
- absPath ,
197
- func (path string , d fs.DirEntry , err error ) error {
198
- // continue on errors
199
- if err != nil {
247
+ var err error
248
+ if c .templatesFS == nil {
249
+ err = filepath .WalkDir (
250
+ absPath ,
251
+ func (path string , d fs.DirEntry , err error ) error {
252
+ // continue on errors
253
+ if err != nil {
254
+ return nil
255
+ }
256
+ if ! d .IsDir () && config .GetTemplateFormatFromExt (path ) != config .Unknown {
257
+ if _ , ok := processed [path ]; ! ok {
258
+ results = append (results , path )
259
+ processed [path ] = struct {}{}
260
+ }
261
+ }
200
262
return nil
201
- }
202
- if ! d .IsDir () && config .GetTemplateFormatFromExt (path ) != config .Unknown {
203
- if _ , ok := processed [path ]; ! ok {
204
- results = append (results , path )
205
- processed [path ] = struct {}{}
263
+ },
264
+ )
265
+ } else {
266
+ err = fs .WalkDir (
267
+ c .templatesFS ,
268
+ absPath ,
269
+ func (path string , d fs.DirEntry , err error ) error {
270
+ // continue on errors
271
+ if err != nil {
272
+ return nil
206
273
}
207
- }
208
- return nil
209
- },
210
- )
274
+ if ! d .IsDir () && config .GetTemplateFormatFromExt (path ) != config .Unknown {
275
+ if _ , ok := processed [path ]; ! ok {
276
+ results = append (results , path )
277
+ processed [path ] = struct {}{}
278
+ }
279
+ }
280
+ return nil
281
+ },
282
+ )
283
+ }
211
284
return results , err
212
285
}
213
286
0 commit comments