github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/storage/filter.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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 storage
    18  
    19  import rspb "k8s.io/helm/pkg/proto/hapi/release"
    20  
    21  // FilterFunc returns true if the release object satisfies
    22  // the predicate of the underlying 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  // Any returns a FilterFunc that filters a list of releases
    34  // determined by the predicate 'f0 || f1 || ... || fn'.
    35  func Any(filters ...FilterFunc) FilterFunc {
    36  	return func(rls *rspb.Release) bool {
    37  		for _, filter := range filters {
    38  			if filter(rls) {
    39  				return true
    40  			}
    41  		}
    42  		return false
    43  	}
    44  }
    45  
    46  // All returns a FilterFunc that filters a list of releases
    47  // determined by the predicate 'f0 && f1 && ... && fn'.
    48  func All(filters ...FilterFunc) FilterFunc {
    49  	return func(rls *rspb.Release) bool {
    50  		for _, filter := range filters {
    51  			if !filter(rls) {
    52  				return false
    53  			}
    54  		}
    55  		return true
    56  	}
    57  }
    58  
    59  // StatusFilter filters a set of releases by status code.
    60  func StatusFilter(status rspb.Status_Code) FilterFunc {
    61  	return FilterFunc(func(rls *rspb.Release) bool {
    62  		if rls == nil {
    63  			return true
    64  		}
    65  		return rls.GetInfo().GetStatus().Code == status
    66  	})
    67  }