-
Notifications
You must be signed in to change notification settings - Fork 7
/
docker.go
89 lines (71 loc) · 1.88 KB
/
docker.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
package domain
import (
"fmt"
"github.com/flowci/flow-agent-x/util"
"strings"
)
type (
// DockerVolume volume will mount to step docker
DockerVolume struct {
Name string // volume name
Dest string // dest path
Script string // script file name to execute
Image string // image contain volume
Init string // init script /ws/{init} in image that will copy required data to /target
}
)
func (v *DockerVolume) HasImage() bool {
return v.Image != ""
}
func (v *DockerVolume) InitScriptInImage() string {
return fmt.Sprintf("/ws/%s", v.Init)
}
func (v *DockerVolume) DefaultTargetInImage() string {
return "/target"
}
func (v *DockerVolume) ScriptPath() string {
return fmt.Sprintf("%s/%s", v.Dest, v.Script)
}
func (v *DockerVolume) ToBindStr() string {
return fmt.Sprintf("%s:%s", v.Name, v.Dest)
}
// NewFromString parse string name=xxx,dest=xxx,script=xxx;name=xxx,dest=xxx,script=xxx;...
func NewVolumesFromString(val string) []*DockerVolume {
var volumes []*DockerVolume
if util.IsEmptyString(val) {
return volumes
}
tokens := strings.Split(val, ";")
if len(tokens) == 0 {
return volumes
}
getValue := func(val string) string {
pair := strings.Split(val, "=")
if len(pair) != 2 {
panic(fmt.Errorf("'%s' is invalid volume string, must be key=value pair", val))
}
return pair[1]
}
for _, token := range tokens {
if util.IsEmptyString(token) {
continue
}
fields := strings.Split(token, ",")
if len(fields) != 5 {
panic(fmt.Errorf("'%s' is invalid volume string, fields must contain name,dest,script,image,init", token))
}
name := fields[0]
dest := fields[1]
script := fields[2]
image := fields[3]
initScript := fields[4]
volumes = append(volumes, &DockerVolume{
Name: getValue(name),
Dest: getValue(dest),
Script: getValue(script),
Image: getValue(image),
Init: getValue(initScript),
})
}
return volumes
}