// matchesFilters applies the relevant filter groups to a filterable func matchesFilters(filterable filters.Filterable) bool { // recover from potential panics caused by malformed filters defer func() { if r := recover(); r != nil { log.Error("Recovered in matchesFilters with panic: ", r) } }() var groups map[string]filters.FilterGroup switch filterable.(type) { case *reaperaws.Instance: groups = config.Instances.FilterGroups case *reaperaws.AutoScalingGroup: groups = config.AutoScalingGroups.FilterGroups case *reaperaws.Cloudformation: groups = config.Cloudformations.FilterGroups case *reaperaws.SecurityGroup: groups = config.SecurityGroups.FilterGroups case *reaperaws.Volume: groups = config.Volumes.FilterGroups default: log.Warning("You probably screwed up and need to make sure matchesFilters works!") return false } matched := false // if there are no filters groups defined default to not match if len(groups) == 0 { return false } shouldFilter := false for _, group := range groups { if len(group) > 0 { // there is a filter shouldFilter = true } } // no filters, default to not match if !shouldFilter { return false } for name, group := range groups { didMatch := filters.ApplyFilters(filterable, group) if didMatch { matched = true filterable.AddFilterGroup(name, group) } } // convenient if isWhitelisted(filterable) { matched = false } return matched }