github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/volume/service/by.go (about)

     1  package service // import "github.com/demonoid81/moby/volume/service"
     2  
     3  import (
     4  	"github.com/demonoid81/moby/api/types/filters"
     5  	"github.com/demonoid81/moby/volume"
     6  )
     7  
     8  // By is an interface which is used to implement filtering on volumes.
     9  type By interface {
    10  	isBy()
    11  }
    12  
    13  // ByDriver is `By` that filters based on the driver names that are passed in
    14  func ByDriver(drivers ...string) By {
    15  	return byDriver(drivers)
    16  }
    17  
    18  type byDriver []string
    19  
    20  func (byDriver) isBy() {}
    21  
    22  // ByReferenced is a `By` that filters based on if the volume has references
    23  type ByReferenced bool
    24  
    25  func (ByReferenced) isBy() {}
    26  
    27  // And creates a `By` combining all the passed in bys using AND logic.
    28  func And(bys ...By) By {
    29  	and := make(andCombinator, 0, len(bys))
    30  	for _, by := range bys {
    31  		and = append(and, by)
    32  	}
    33  	return and
    34  }
    35  
    36  type andCombinator []By
    37  
    38  func (andCombinator) isBy() {}
    39  
    40  // Or creates a `By` combining all the passed in bys using OR logic.
    41  func Or(bys ...By) By {
    42  	or := make(orCombinator, 0, len(bys))
    43  	for _, by := range bys {
    44  		or = append(or, by)
    45  	}
    46  	return or
    47  }
    48  
    49  type orCombinator []By
    50  
    51  func (orCombinator) isBy() {}
    52  
    53  // CustomFilter is a `By` that is used by callers to provide custom filtering
    54  // logic.
    55  type CustomFilter filterFunc
    56  
    57  func (CustomFilter) isBy() {}
    58  
    59  // FromList returns a By which sets the initial list of volumes to use
    60  func FromList(ls *[]volume.Volume, by By) By {
    61  	return &fromList{by: by, ls: ls}
    62  }
    63  
    64  type fromList struct {
    65  	by By
    66  	ls *[]volume.Volume
    67  }
    68  
    69  func (fromList) isBy() {}
    70  
    71  func byLabelFilter(filter filters.Args) By {
    72  	return CustomFilter(func(v volume.Volume) bool {
    73  		dv, ok := v.(volume.DetailedVolume)
    74  		if !ok {
    75  			return false
    76  		}
    77  
    78  		labels := dv.Labels()
    79  		if !filter.MatchKVList("label", labels) {
    80  			return false
    81  		}
    82  		if filter.Contains("label!") {
    83  			if filter.MatchKVList("label!", labels) {
    84  				return false
    85  			}
    86  		}
    87  		return true
    88  	})
    89  }