/
list.go
117 lines (98 loc) · 2.97 KB
/
list.go
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
package namespace
import (
"fmt"
kubeModels "github.com/containerum/kube-client/pkg/model"
)
type NamespaceList []Namespace
func NamespaceListFromKube(kubeList kubeModels.NamespacesList) NamespaceList {
var list NamespaceList = make([]Namespace, 0, len(kubeList.Namespaces))
for _, namespace := range kubeList.Namespaces {
list = append(list, NamespaceFromKube(namespace).Copy())
}
return list
}
func (list NamespaceList) New() NamespaceList {
return make(NamespaceList, 0, len(list))
}
func (list NamespaceList) Copy() NamespaceList {
var cp = list.New()
for _, namespace := range list {
cp = append(cp, namespace.Copy())
}
return cp
}
func (list NamespaceList) ToKube() kubeModels.NamespacesList {
var namespaces = make([]kubeModels.Namespace, 0, len(list))
for _, namespace := range list {
namespaces = append(namespaces, namespace.ToKube())
}
return kubeModels.NamespacesList{
Namespaces: namespaces,
}
}
func (list NamespaceList) Len() int {
return len(list)
}
func (list NamespaceList) GetDefault(i int, defaultNs Namespace) (Namespace, bool) {
if i >= 0 && i < list.Len() {
return list.Get(i), true
}
return defaultNs, false
}
func (list NamespaceList) Get(i int) Namespace {
return list[i].Copy()
}
func (list NamespaceList) Head() (Namespace, bool) {
return list.GetDefault(0, Namespace{})
}
func (list NamespaceList) Labels() []string {
var labels = make([]string, 0, list.Len())
for _, namespace := range list {
labels = append(labels, namespace.Label)
}
return labels
}
func (list NamespaceList) IDs() []string {
var IDs = make([]string, 0, list.Len())
for _, namespace := range list {
IDs = append(IDs, namespace.ID)
}
return IDs
}
func (list NamespaceList) LabelsAndIDs() []string {
var lines = make([]string, 0, list.Len())
for _, namespace := range list {
lines = append(lines, fmt.Sprintf("%s/%s", namespace.Label, namespace.ID))
}
return lines
}
func (list NamespaceList) OwnersAndLabels() []string {
var ownersAndLabels = make([]string, 0, list.Len())
for _, namespace := range list {
ownersAndLabels = append(ownersAndLabels, fmt.Sprintf("%s/%s", namespace.OwnerLogin, namespace.Label))
}
return ownersAndLabels
}
func (list NamespaceList) Filter(pred func(Namespace) bool) NamespaceList {
var filtered = list.New()
for _, namespace := range list {
if pred(namespace.Copy()) {
filtered = append(filtered, namespace.Copy())
}
}
return filtered
}
// get Namespace by string $LABEL or $OWNER_LOGIN/$LABEL
func (list NamespaceList) GetByUserFriendlyID(id string) (Namespace, bool) {
return list.Filter(func(namespace Namespace) bool {
return namespace.Label == id || namespace.OwnerAndLabel() == id
}).Head()
}
func (list NamespaceList) GetByLabelAndOwner(owner, label string) (Namespace, bool) {
return list.Filter(func(namespace Namespace) bool {
return namespace.OwnerLogin == owner && namespace.Label == label
}).Head()
}
func (list NamespaceList) Append(ns Namespace) NamespaceList {
return append(list.Copy(), ns.Copy())
}