summaryrefslogtreecommitdiff
path: root/pkg/domain/entities/filters.go
blob: c7e22724482cd3bc4c579f681aa07c5c4e9c0f00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package entities

import (
	"net/url"
	"strings"
)

// Identifier interface allows filters to access ID() of object
type Identifier interface {
	Id() string
}

// Named interface allows filters to access Name() of object
type Named interface {
	Name() string
}

// Named interface allows filters to access Name() of object
type Names interface {
	Names() []string
}

// IdOrName interface allows filters to access ID() or Name() of object
type IdOrNamed interface {
	Identifier
	Named
}

// IdOrName interface allows filters to access ID() or Names() of object
type IdOrNames interface {
	Identifier
	Names
}

type ImageFilter func(Image) bool
type VolumeFilter func(Volume) bool
type ContainerFilter func(Container) bool

func CompileImageFilters(filters url.Values) ImageFilter {
	var fns []interface{}

	for name, targets := range filters {
		switch name {
		case "id":
			fns = append(fns, FilterIdFn(targets))
		case "name":
			fns = append(fns, FilterNamesFn(targets))
		case "idOrName":
			fns = append(fns, FilterIdOrNameFn(targets))
		}
	}

	return func(image Image) bool {
		for _, fn := range fns {
			if !fn.(ImageFilter)(image) {
				return false
			}
		}
		return true
	}
}

func CompileContainerFilters(filters url.Values) ContainerFilter {
	var fns []interface{}

	for name, targets := range filters {
		switch name {
		case "id":
			fns = append(fns, FilterIdFn(targets))
		case "name":
			fns = append(fns, FilterNameFn(targets))
		case "idOrName":
			fns = append(fns, FilterIdOrNameFn(targets))
		}
	}

	return func(ctnr Container) bool {
		for _, fn := range fns {
			if !fn.(ContainerFilter)(ctnr) {
				return false
			}
		}
		return true
	}
}

func CompileVolumeFilters(filters url.Values) VolumeFilter {
	var fns []interface{}

	for name, targets := range filters {
		if name == "id" {
			fns = append(fns, FilterIdFn(targets))
		}
	}

	return func(volume Volume) bool {
		for _, fn := range fns {
			if !fn.(VolumeFilter)(volume) {
				return false
			}
		}
		return true
	}
}

func FilterIdFn(id []string) func(Identifier) bool {
	return func(obj Identifier) bool {
		for _, v := range id {
			if strings.Contains(obj.Id(), v) {
				return true
			}
		}
		return false
	}
}

func FilterNameFn(name []string) func(Named) bool {
	return func(obj Named) bool {
		for _, v := range name {
			if strings.Contains(obj.Name(), v) {
				return true
			}
		}
		return false
	}
}

func FilterNamesFn(name []string) func(Names) bool {
	return func(obj Names) bool {
		for _, v := range name {
			for _, n := range obj.Names() {
				if strings.Contains(n, v) {
					return true
				}
			}
		}
		return false
	}
}

func FilterIdOrNameFn(id []string) func(IdOrNamed) bool {
	return func(obj IdOrNamed) bool {
		for _, v := range id {
			if strings.Contains(obj.Id(), v) || strings.Contains(obj.Name(), v) {
				return true
			}
		}
		return false
	}
}