forked from AliyunContainerService/pouch
/
ports.go
56 lines (48 loc) · 1.39 KB
/
ports.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
package opts
import (
"fmt"
"strconv"
"strings"
"github.com/docker/go-connections/nat"
)
// ParseExposedPorts parse ports.
// FIXME(ziren): add more explanation
func ParseExposedPorts(portList, expose []string) (map[string]interface{}, error) {
// translate ports
tmpPorts, _, err := nat.ParsePortSpecs(portList)
if err != nil {
return nil, err
}
ports := map[string]interface{}{}
for n, p := range tmpPorts {
ports[string(n)] = p
}
for _, e := range expose {
if strings.Contains(e, ":") {
return nil, fmt.Errorf("invalid port format for --expose: %s", e)
}
//support two formats for expose, original format <portnum>/[<proto>] or <startport-endport>/[<proto>]
proto, port := nat.SplitProtoPort(e)
//parse the start and end port and create a sequence of ports to expose
//if expose a port, the start and end port are the same
start, end, err := nat.ParsePortRange(port)
if err != nil {
return nil, fmt.Errorf("invalid range format for --expose: %s, error: %s", e, err)
}
for i := start; i <= end; i++ {
p, err := nat.NewPort(proto, strconv.FormatUint(i, 10))
if err != nil {
return nil, err
}
if _, exists := ports[string(p)]; !exists {
ports[string(p)] = struct{}{}
}
}
}
return ports, nil
}
// ValidateExposedPorts verify the correction of exposed ports.
func ValidateExposedPorts(ports map[string]interface{}) error {
// TODO
return nil
}