github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/domain/entities/filters.go (about)

     1  package entities
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  )
     7  
     8  // Identifier interface allows filters to access ID() of object
     9  type Identifier interface {
    10  	Id() string
    11  }
    12  
    13  // Named interface allows filters to access Name() of object
    14  type Named interface {
    15  	Name() string
    16  }
    17  
    18  // Named interface allows filters to access Name() of object
    19  type Names interface {
    20  	Names() []string
    21  }
    22  
    23  // IDOrName interface allows filters to access ID() or Name() of object
    24  type IDOrNamed interface {
    25  	Identifier
    26  	Named
    27  }
    28  
    29  // IDOrName interface allows filters to access ID() or Names() of object
    30  type IDOrNames interface {
    31  	Identifier
    32  	Names
    33  }
    34  
    35  type ImageFilter func(Image) bool
    36  type VolumeFilter func(Volume) bool
    37  type ContainerFilter func(Container) bool
    38  
    39  func CompileImageFilters(filters url.Values) ImageFilter {
    40  	var fns []interface{}
    41  
    42  	for name, targets := range filters {
    43  		switch name {
    44  		case "id":
    45  			fns = append(fns, FilterIDFn(targets))
    46  		case "name":
    47  			fns = append(fns, FilterNamesFn(targets))
    48  		case "idOrName":
    49  			fns = append(fns, FilterIDOrNameFn(targets))
    50  		}
    51  	}
    52  
    53  	return func(image Image) bool {
    54  		for _, fn := range fns {
    55  			if !fn.(ImageFilter)(image) {
    56  				return false
    57  			}
    58  		}
    59  		return true
    60  	}
    61  }
    62  
    63  func CompileContainerFilters(filters url.Values) ContainerFilter {
    64  	var fns []interface{}
    65  
    66  	for name, targets := range filters {
    67  		switch name {
    68  		case "id":
    69  			fns = append(fns, FilterIDFn(targets))
    70  		case "name":
    71  			fns = append(fns, FilterNameFn(targets))
    72  		case "idOrName":
    73  			fns = append(fns, FilterIDOrNameFn(targets))
    74  		}
    75  	}
    76  
    77  	return func(ctnr Container) bool {
    78  		for _, fn := range fns {
    79  			if !fn.(ContainerFilter)(ctnr) {
    80  				return false
    81  			}
    82  		}
    83  		return true
    84  	}
    85  }
    86  
    87  func CompileVolumeFilters(filters url.Values) VolumeFilter {
    88  	var fns []interface{}
    89  
    90  	for name, targets := range filters {
    91  		if name == "id" {
    92  			fns = append(fns, FilterIDFn(targets))
    93  		}
    94  	}
    95  
    96  	return func(volume Volume) bool {
    97  		for _, fn := range fns {
    98  			if !fn.(VolumeFilter)(volume) {
    99  				return false
   100  			}
   101  		}
   102  		return true
   103  	}
   104  }
   105  
   106  func FilterIDFn(id []string) func(Identifier) bool {
   107  	return func(obj Identifier) bool {
   108  		for _, v := range id {
   109  			if strings.Contains(obj.Id(), v) {
   110  				return true
   111  			}
   112  		}
   113  		return false
   114  	}
   115  }
   116  
   117  func FilterNameFn(name []string) func(Named) bool {
   118  	return func(obj Named) bool {
   119  		for _, v := range name {
   120  			if strings.Contains(obj.Name(), v) {
   121  				return true
   122  			}
   123  		}
   124  		return false
   125  	}
   126  }
   127  
   128  func FilterNamesFn(name []string) func(Names) bool {
   129  	return func(obj Names) bool {
   130  		for _, v := range name {
   131  			for _, n := range obj.Names() {
   132  				if strings.Contains(n, v) {
   133  					return true
   134  				}
   135  			}
   136  		}
   137  		return false
   138  	}
   139  }
   140  
   141  func FilterIDOrNameFn(id []string) func(IDOrNamed) bool {
   142  	return func(obj IDOrNamed) bool {
   143  		for _, v := range id {
   144  			if strings.Contains(obj.Id(), v) || strings.Contains(obj.Name(), v) {
   145  				return true
   146  			}
   147  		}
   148  		return false
   149  	}
   150  }