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