...

Source file src/github.com/linkerd/linkerd2/cli/cmd/range_slice.go

Documentation: github.com/linkerd/linkerd2/cli/cmd

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // validateRangeSlice ensures that provided slice contains valid entries
    10  // representing either port number(s) and/or port range(s).  Invalid entries
    11  // will result in an error being returned.
    12  func validateRangeSlice(rangeSlice []string) error {
    13  	for _, portOrRange := range rangeSlice {
    14  		if strings.Contains(portOrRange, "-") {
    15  			bounds := strings.Split(portOrRange, "-")
    16  			if len(bounds) != 2 {
    17  				return fmt.Errorf("ranges expected as <lower>-<upper>")
    18  			}
    19  			lower, err := strconv.Atoi(bounds[0])
    20  			if err != nil || !isValidPort(lower) {
    21  				return fmt.Errorf("\"%s\" is not a valid lower-bound", bounds[0])
    22  			}
    23  			upper, err := strconv.Atoi(bounds[1])
    24  			if err != nil || !isValidPort(upper) {
    25  				return fmt.Errorf("\"%s\" is not a valid upper-bound", bounds[1])
    26  			}
    27  			if upper < lower {
    28  				return fmt.Errorf("\"%s\": upper-bound must be greater than or equal to lower-bound", portOrRange)
    29  			}
    30  		} else {
    31  			port, err := strconv.Atoi(portOrRange)
    32  			if err != nil || !isValidPort(port) {
    33  				return fmt.Errorf("\"%s\" is not a valid port nor port-range", portOrRange)
    34  			}
    35  		}
    36  	}
    37  	return nil
    38  }
    39  
    40  // isValidPort ensures that the provided value is a valid TCP port number, 0-65535 (inclusive).
    41  func isValidPort(port int) bool {
    42  	return port >= 0 && port <= 65535
    43  }
    44  

View as plain text