func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
	var (
		exposedPorts = make(map[Port]struct{}, len(ports))
		bindings     = make(map[Port][]PortBinding)
	)

	for _, rawPort := range ports {
		proto := "tcp"

		if i := strings.LastIndex(rawPort, "/"); i != -1 {
			proto = rawPort[i+1:]
			rawPort = rawPort[:i]
		}
		if !strings.Contains(rawPort, ":") {
			rawPort = fmt.Sprintf("::%s", rawPort)
		} else if len(strings.Split(rawPort, ":")) == 2 {
			rawPort = fmt.Sprintf(":%s", rawPort)
		}

		parts, err := parsers.PartParser(portSpecTemplate, rawPort)
		if err != nil {
			return nil, nil, err
		}

		var (
			containerPort = parts["containerPort"]
			rawIP         = parts["ip"]
			hostPort      = parts["hostPort"]
		)

		if rawIP != "" && net.ParseIP(rawIP) == nil {
			return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIP)
		}
		if containerPort == "" {
			return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
		}

		startPort, endPort, err := parsers.ParsePortRange(containerPort)
		if err != nil {
			return nil, nil, fmt.Errorf("Invalid containerPort: %s", containerPort)
		}

		var startHostPort, endHostPort uint64 = 0, 0
		if len(hostPort) > 0 {
			startHostPort, endHostPort, err = parsers.ParsePortRange(hostPort)
			if err != nil {
				return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort)
			}
		}

		if hostPort != "" && (endPort-startPort) != (endHostPort-startHostPort) {
			// Allow host port range iff containerPort is not a range.
			// In this case, use the host port range as the dynamic
			// host port range to allocate into.
			if endPort != startPort {
				return nil, nil, fmt.Errorf("Invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
			}
		}

		if !validateProto(strings.ToLower(proto)) {
			return nil, nil, fmt.Errorf("Invalid proto: %s", proto)
		}

		for i := uint64(0); i <= (endPort - startPort); i++ {
			containerPort = strconv.FormatUint(startPort+i, 10)
			if len(hostPort) > 0 {
				hostPort = strconv.FormatUint(startHostPort+i, 10)
			}
			// Set hostPort to a range only if there is a single container port
			// and a dynamic host port.
			if startPort == endPort && startHostPort != endHostPort {
				hostPort = fmt.Sprintf("%s-%s", hostPort, strconv.FormatUint(endHostPort, 10))
			}
			port, err := NewPort(strings.ToLower(proto), containerPort)
			if err != nil {
				return nil, nil, err
			}
			if _, exists := exposedPorts[port]; !exists {
				exposedPorts[port] = struct{}{}
			}

			binding := PortBinding{
				HostIP:   rawIP,
				HostPort: hostPort,
			}
			bslice, exists := bindings[port]
			if !exists {
				bslice = []PortBinding{}
			}
			bindings[port] = append(bslice, binding)
		}
	}
	return exposedPorts, bindings, nil
}
// ParsePortSpecs receives port specs in the format of ip:public:private/proto and parses
// these in to the internal types
ParsePortSpecs receives port specs in the format of ip:public:private/proto and parses
these in to the internal types
// ParsePortSpecs receives port specs in the format of ip : public : private / proto and parses // these in to the internal types
func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
	var (
		exposedPorts = make(map[Port]struct{}, len(ports))
		bindings     = make(map[Port][]PortBinding)
	)
	for _, rawPort := range ports {
		proto := "tcp"
		if i := strings.LastIndex(rawPort, "/"); i != -1 {
			proto = rawPort[i+1:]
			rawPort = rawPort[:i]
		}
		if !strings.Contains(rawPort, ":") {
			rawPort = fmt.Sprintf("::%s", rawPort)
		} else if len(strings.Split(rawPort, ":")) == 2 {
			rawPort = fmt.Sprintf(":%s", rawPort)
		}
		parts, err := parsers.PartParser(portSpecTemplate, rawPort)
		if err != nil {
			return nil, nil, err
		}
		var (
			containerPort = parts["containerPort"]
			rawIP         = parts["ip"]
			hostPort      = parts["hostPort"]
		)
		if rawIP != "" && net.ParseIP(rawIP) == nil {
			return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIP)
		}
		if containerPort == "" {
			return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
		}
		startPort, endPort, err := parsers.ParsePortRange(containerPort)
		if err != nil {
			return nil, nil, fmt.Errorf("Invalid containerPort: %s", containerPort)
		}
		var startHostPort, endHostPort uint64 = 0, 0
		if len(hostPort) > 0 {
			startHostPort, endHostPort, err = parsers.ParsePortRange(hostPort)
			if err != nil {
				return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort)
			}
		}
		if hostPort != "" && (endPort-startPort) != (endHostPort-startHostPort) {
			if endPort != startPort {
				return nil, nil, fmt.Errorf("Invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
			}
		}
		if !validateProto(strings.ToLower(proto)) {
			return nil, nil, fmt.Errorf("Invalid proto: %s", proto)
		}
		for i := uint64(0); i <= (endPort - startPort); i++ {
			containerPort = strconv.FormatUint(startPort+i, 10)
			if len(hostPort) > 0 {
				hostPort = strconv.FormatUint(startHostPort+i, 10)
			}
			if startPort == endPort && startHostPort != endHostPort {
				hostPort = fmt.Sprintf("%s-%s", hostPort, strconv.FormatUint(endHostPort, 10))
			}
			port, err := NewPort(strings.ToLower(proto), containerPort)
			if err != nil {
				return nil, nil, err
			}
			if _, exists := exposedPorts[port]; !exists {
				exposedPorts[port] = struct{}{}
			}
			binding := PortBinding{
				HostIP:   rawIP,
				HostPort: hostPort,
			}
			bslice, exists := bindings[port]
			if !exists {
				bslice = []PortBinding{}
			}
			bindings[port] = append(bslice, binding)
		}
	}
	return exposedPorts, bindings, nil
}
func ParsePortSpecs ( ports [ ] string ) ( map [ Port ] struct { } , map [ Port ] [ ] PortBinding , error ) { var ( exposedPorts = make ( map [ Port ] struct { } , len ( ports ) ) bindings = make ( map [ Port ] [ ] PortBinding ) ) for _ , rawPort := range ports { proto := " " if i := strings . LastIndex ( rawPort , " " ) ; i != - 1 { proto = rawPort [ i + 1 : ] rawPort = rawPort [ : i ] } if ! strings . Contains ( rawPort , " " ) { rawPort = fmt . Sprintf ( " " , rawPort ) } else if len ( strings . Split ( rawPort , " " ) ) == 2 { rawPort = fmt . Sprintf ( " " , rawPort ) } parts , err := parsers . PartParser ( portSpecTemplate , rawPort ) if err != nil { return nil , nil , err } var ( containerPort = parts [ " " ] rawIP = parts [ " " ] hostPort = parts [ " " ] ) if rawIP != " " && net . ParseIP ( rawIP ) == nil { return nil , nil , fmt . Errorf ( " " , rawIP ) } if containerPort == " " { return nil , nil , fmt . Errorf ( " " , rawPort ) } startPort , endPort , err := parsers . ParsePortRange ( containerPort ) if err != nil { return nil , nil , fmt . Errorf ( " " , containerPort ) } var startHostPort , endHostPort uint64 = 0 , 0 if len ( hostPort ) > 0 { startHostPort , endHostPort , err = parsers . ParsePortRange ( hostPort ) if err != nil { return nil , nil , fmt . Errorf ( " " , hostPort ) } } if hostPort != " " && ( endPort - startPort ) != ( endHostPort - startHostPort ) { if endPort != startPort { return nil , nil , fmt . Errorf ( " " , containerPort , hostPort ) } } if ! validateProto ( strings . ToLower ( proto ) ) { return nil , nil , fmt . Errorf ( " " , proto ) } for i := uint64 ( 0 ) ; i <= ( endPort - startPort ) ; i ++ { containerPort = strconv . FormatUint ( startPort + i , 10 ) if len ( hostPort ) > 0 { hostPort = strconv . FormatUint ( startHostPort + i , 10 ) } if startPort == endPort && startHostPort != endHostPort { hostPort = fmt . Sprintf ( " " , hostPort , strconv . FormatUint ( endHostPort , 10 ) ) } port , err := NewPort ( strings . ToLower ( proto ) , containerPort ) if err != nil { return nil , nil , err } if _ , exists := exposedPorts [ port ] ; ! exists { exposedPorts [ port ] = struct { } { } } binding := PortBinding { HostIP : rawIP , HostPort : hostPort , } bslice , exists := bindings [ port ] if ! exists { bslice = [ ] PortBinding { } } bindings [ port ] = append ( bslice , binding ) } } return exposedPorts , bindings , nil }
ParsePortSpecs receives port specs in the format of ip:public:private/proto and parses
these in to the internal types
ParsePortSpecs receives port specs in the format of ip : public : private / proto and parses these in to the internal types
// Allow host port range iff containerPort is not a range. // In this case, use the host port range as the dynamic // host port range to allocate into. // Set hostPort to a range only if there is a single container port // and a dynamic host port.