@@ -322,3 +322,51 @@ func ProfileFromObject(
322322 result .ExportTarget = exportTarget
323323 return result , nil
324324}
325+
326+ // ForeachFilter iterates over the filters of the profile and applies the
327+ // given function to each filter. If unpackNestedProfiles is true, it will
328+ // unpack the nested profiles and apply the function to their filters as well.
329+ func (p * Profile ) ForeachFilter (
330+ context RunContext ,
331+ fn func (FilterRunner ) error ,
332+ unpackNestedProfiles bool ,
333+ ) error {
334+ for i := range p .Filters {
335+ filter := p .Filters [i ]
336+ profileFilter , ok := filter .(* ProfileFilter )
337+ if unpackNestedProfiles && ok {
338+ subProfile , ok := context .Config .Profiles [profileFilter .Profile ]
339+ if ! ok {
340+ return burrito .WrappedErrorf (
341+ "Failed to find profile of the profile-filter.\n " +
342+ "Parent profile: %s\n " +
343+ "Profile filter index: %d\n " +
344+ "Referenced profile: %s\n " ,
345+ context .Profile , i , profileFilter .Profile ,
346+ )
347+ }
348+ err := subProfile .ForeachFilter (context , fn , unpackNestedProfiles )
349+ if err != nil {
350+ return burrito .WrappedErrorf (
351+ "Failed to iterate over filters of the profile-filter.\n " +
352+ "Parent profile: %s\n " +
353+ "Profile filter index: %d\n " +
354+ "Referenced profile: %s\n " ,
355+ context .Profile , i , profileFilter .Profile ,
356+ )
357+ }
358+ } else {
359+ err := fn (filter )
360+ if err != nil {
361+ return burrito .WrapErrorf (
362+ err ,
363+ "Failed to iterate apply function to the filter.\n " +
364+ "Profile: %s\n " +
365+ "Filter index: %d\n " ,
366+ context .Profile , i ,
367+ )
368+ }
369+ }
370+ }
371+ return nil
372+ }
0 commit comments