github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/columns/filter.go (about)

     1  // Copyright 2022 The Inspektor Gadget authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package columns
    16  
    17  import "strings"
    18  
    19  type ColumnFilter func(matcher ColumnMatcher) bool
    20  
    21  // Or combines several ColumnFilter and matches if one filter matches
    22  func Or(filters ...ColumnFilter) ColumnFilter {
    23  	return func(matcher ColumnMatcher) bool {
    24  		for _, f := range filters {
    25  			if f(matcher) {
    26  				return true
    27  			}
    28  		}
    29  		return false
    30  	}
    31  }
    32  
    33  // And combines several ColumnFilter and matches if all filters match
    34  func And(filters ...ColumnFilter) ColumnFilter {
    35  	return func(matcher ColumnMatcher) bool {
    36  		for _, f := range filters {
    37  			if !f(matcher) {
    38  				return false
    39  			}
    40  		}
    41  		return true
    42  	}
    43  }
    44  
    45  // WithEmbedded checks whether a column matches the embedded criteria
    46  func WithEmbedded(embedded bool) ColumnFilter {
    47  	return func(matcher ColumnMatcher) bool {
    48  		if matcher.IsEmbedded() == embedded {
    49  			return true
    50  		}
    51  		return false
    52  	}
    53  }
    54  
    55  // WithTags makes sure that all returned columns contain all the given tags
    56  func WithTags(tags []string) ColumnFilter {
    57  	tags = ToLowerStrings(tags)
    58  	return func(matcher ColumnMatcher) bool {
    59  		for _, tag := range tags {
    60  			if !matcher.HasTag(tag) {
    61  				return false
    62  			}
    63  		}
    64  		return true
    65  	}
    66  }
    67  
    68  // WithAnyTag makes sure that all returned columns contain at least one of the given tags
    69  func WithAnyTag(tags []string) ColumnFilter {
    70  	tags = ToLowerStrings(tags)
    71  	return func(matcher ColumnMatcher) bool {
    72  		for _, tag := range tags {
    73  			if matcher.HasTag(tag) {
    74  				return true
    75  			}
    76  		}
    77  		return false
    78  	}
    79  }
    80  
    81  // WithoutTags makes sure that all returned columns contain none of the given tags
    82  func WithoutTags(tags []string) ColumnFilter {
    83  	tags = ToLowerStrings(tags)
    84  	return func(matcher ColumnMatcher) bool {
    85  		for _, tag := range tags {
    86  			if matcher.HasTag(tag) {
    87  				return false
    88  			}
    89  		}
    90  		return true
    91  	}
    92  }
    93  
    94  // WithTag makes sure that all returned columns contain all the given tag
    95  func WithTag(tag string) ColumnFilter {
    96  	tag = strings.ToLower(tag)
    97  	return func(matcher ColumnMatcher) bool {
    98  		return matcher.HasTag(tag)
    99  	}
   100  }
   101  
   102  // WithoutTag makes sure that all returned columns don't contain the given tag
   103  func WithoutTag(tag string) ColumnFilter {
   104  	tag = strings.ToLower(tag)
   105  	return func(matcher ColumnMatcher) bool {
   106  		return !matcher.HasTag(tag)
   107  	}
   108  }
   109  
   110  // WithoutExceptTag makes sure that all returned columns don't contain the given
   111  // tag, except when it also includes the exception given
   112  func WithoutExceptTag(tag, exception string) ColumnFilter {
   113  	tag = strings.ToLower(tag)
   114  	return func(matcher ColumnMatcher) bool {
   115  		return !matcher.HasTag(tag) || matcher.HasTag(exception)
   116  	}
   117  }
   118  
   119  // WithNoTags makes sure that all returned columns contain no tags
   120  func WithNoTags() ColumnFilter {
   121  	return func(matcher ColumnMatcher) bool {
   122  		return matcher.HasNoTags()
   123  	}
   124  }