/
containers.go
106 lines (91 loc) · 2.71 KB
/
containers.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
package engine
import (
"fmt"
"strings"
"github.com/0xrawsec/golang-utils/datastructs"
)
var (
//ErrContainerAlreadyExists should be returned in case an container name is already used in Containers
ErrContainerAlreadyExists = fmt.Errorf("Already existing container")
)
// ContainerDB structure used to store several containers
type ContainerDB map[string]*datastructs.SyncedSet
// NewContainers initializes a new Containers structure
func NewContainers() *ContainerDB {
c := make(ContainerDB)
return &c
}
// AddNewContainer adds an empty container to the DB
func (c *ContainerDB) AddNewContainer(name string) error {
container := datastructs.NewSyncedSet()
return c.AddContainer(name, container)
}
// AddContainer adds a new container to c
func (c *ContainerDB) AddContainer(name string, container *datastructs.SyncedSet) error {
if _, ok := (*c)[name]; !ok {
(*c)[name] = container
return nil
}
return ErrContainerAlreadyExists
}
// AddToContainer adds a new value into a container and creates a
// new container if it does not exist yet
func (c *ContainerDB) AddToContainer(name string, values ...interface{}) {
if !c.Has(name) {
c.AddNewContainer(name)
}
container, _ := c.Get(name)
container.Add(values...)
}
// AddStringToContainer adds new strings (converted to lower case)
// into a container and creates a new container if it does not exist yet.
func (c *ContainerDB) AddStringToContainer(name string, values ...string) {
if !c.Has(name) {
c.AddNewContainer(name)
}
container, _ := c.Get(name)
for _, s := range values {
container.Add(strings.ToLower(s))
}
}
// Len gives the size of a Container
func (c *ContainerDB) Len(name string) int {
if c.Has(name) {
return (*c)[name].Len()
}
return 0
}
// Has checks if a named container is in the DB
func (c *ContainerDB) Has(name string) bool {
_, ok := (*c)[name]
return ok
}
//Get get a container by its name
func (c *ContainerDB) Get(name string) (*datastructs.SyncedSet, bool) {
cont, ok := (*c)[name]
return cont, ok
}
// Contains checks if named container contains value
func (c *ContainerDB) Contains(name string, value string) bool {
if cont, ok := (*c)[name]; ok {
return cont.Contains(value)
}
return false
}
// ContainsString checks if named container contains value ignoring value case
func (c *ContainerDB) ContainsString(name string, value string) bool {
if cont, ok := (*c)[name]; ok {
return cont.Contains(strings.ToLower(value))
}
return false
}
func (c *ContainerDB) String() string {
out := make([]string, 0)
for name := range *c {
out = append(out, fmt.Sprintf("Container: %s", name))
for _, v := range (*c)[name].Slice() {
out = append(out, fmt.Sprintf("\tType:%T Value:%[1]v", v))
}
}
return strings.Join(out, "\n")
}