forked from cpuguy83/docker-volumes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volume.go
72 lines (58 loc) · 1.06 KB
/
volume.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
package main
import "github.com/cpuguy83/dockerclient"
type Volume struct {
docker.Volume
ID string
Containers []string
Names []string
}
type volStore struct {
s map[string]*Volume
}
func (v *volStore) Add(volume *Volume) {
v.s[volume.ID] = volume
}
func (v *volStore) Get(id string) *Volume {
return v.s[id]
}
func (v *volStore) CanRemove(volume *Volume) bool {
if len(volume.Containers) != 0 {
return false
}
return true
}
func (v *volStore) Find(id string) *Volume {
var vol *Volume
if vol = v.Get(id); vol != nil {
return vol
}
if vol = v.FindByName(id); vol != nil {
return vol
}
if vol = v.FindByTruncatedID(id); vol != nil {
return vol
}
return nil
}
func (v *volStore) FindByName(name string) *Volume {
for _, vol := range v.s {
for _, n := range vol.Names {
if n == name {
return vol
}
}
}
return nil
}
func (v *volStore) FindByTruncatedID(id string) *Volume {
for _, vol := range v.s {
volId := vol.ID
if len(volId) > 12 {
volId = volId[:12]
}
if id == volId {
return vol
}
}
return nil
}