/
convert.go
46 lines (38 loc) · 896 Bytes
/
convert.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
package component
import (
"errors"
"fmt"
"github.com/docker/docker/api/types/container"
"strings"
)
func asDeviceMappings(devices []string) ([]container.DeviceMapping, error) {
if devices == nil {
return nil, nil
}
mapped := make([]container.DeviceMapping, len(devices), len(devices))
for idx, device := range devices {
var source, destination, permissions string
parts := strings.Split(device, ":")
switch len(parts) {
case 3:
permissions = parts[2]
fallthrough
case 2:
destination = parts[1]
fallthrough
case 1:
source = parts[0]
default:
return nil, errors.New(fmt.Sprintf("unexpected device mapping: %s", device))
}
if destination == "" {
destination = source
}
mapped[idx] = container.DeviceMapping{
PathOnHost: source,
PathInContainer: destination,
CgroupPermissions: permissions,
}
}
return mapped, nil
}