...

Source file src/github.com/docker/go-connections/nat/parse.go

Documentation: github.com/docker/go-connections/nat

     1  package nat
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // ParsePortRange parses and validates the specified string as a port-range (8000-9000)
    10  func ParsePortRange(ports string) (uint64, uint64, error) {
    11  	if ports == "" {
    12  		return 0, 0, fmt.Errorf("empty string specified for ports")
    13  	}
    14  	if !strings.Contains(ports, "-") {
    15  		start, err := strconv.ParseUint(ports, 10, 16)
    16  		end := start
    17  		return start, end, err
    18  	}
    19  
    20  	parts := strings.Split(ports, "-")
    21  	start, err := strconv.ParseUint(parts[0], 10, 16)
    22  	if err != nil {
    23  		return 0, 0, err
    24  	}
    25  	end, err := strconv.ParseUint(parts[1], 10, 16)
    26  	if err != nil {
    27  		return 0, 0, err
    28  	}
    29  	if end < start {
    30  		return 0, 0, fmt.Errorf("invalid range specified for port: %s", ports)
    31  	}
    32  	return start, end, nil
    33  }
    34  

View as plain text