gitlab.azmi.pl/azmi-open-source/helm@v3.0.0-beta.3+incompatible/pkg/releaseutil/filter.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package releaseutil // import "helm.sh/helm/pkg/releaseutil"
    18  
    19  import rspb "helm.sh/helm/pkg/release"
    20  
    21  // FilterFunc returns true if the release object satisfies
    22  // the predicate of the underlying filter func.
    23  type FilterFunc func(*rspb.Release) bool
    24  
    25  // Check applies the FilterFunc to the release object.
    26  func (fn FilterFunc) Check(rls *rspb.Release) bool {
    27  	if rls == nil {
    28  		return false
    29  	}
    30  	return fn(rls)
    31  }
    32  
    33  // Filter applies the filter(s) to the list of provided releases
    34  // returning the list that satisfies the filtering predicate.
    35  func (fn FilterFunc) Filter(rels []*rspb.Release) (rets []*rspb.Release) {
    36  	for _, rel := range rels {
    37  		if fn.Check(rel) {
    38  			rets = append(rets, rel)
    39  		}
    40  	}
    41  	return
    42  }
    43  
    44  // Any returns a FilterFunc that filters a list of releases
    45  // determined by the predicate 'f0 || f1 || ... || fn'.
    46  func Any(filters ...FilterFunc) FilterFunc {
    47  	return func(rls *rspb.Release) bool {
    48  		for _, filter := range filters {
    49  			if filter(rls) {
    50  				return true
    51  			}
    52  		}
    53  		return false
    54  	}
    55  }
    56  
    57  // All returns a FilterFunc that filters a list of releases
    58  // determined by the predicate 'f0 && f1 && ... && fn'.
    59  func All(filters ...FilterFunc) FilterFunc {
    60  	return func(rls *rspb.Release) bool {
    61  		for _, filter := range filters {
    62  			if !filter(rls) {
    63  				return false
    64  			}
    65  		}
    66  		return true
    67  	}
    68  }
    69  
    70  // StatusFilter filters a set of releases by status code.
    71  func StatusFilter(status rspb.Status) FilterFunc {
    72  	return FilterFunc(func(rls *rspb.Release) bool {
    73  		if rls == nil {
    74  			return true
    75  		}
    76  		return rls.Info.Status == status
    77  	})
    78  }