mirror of https://github.com/docker/docs.git
driverutil: refactor splitPortProto to parse port as str
Signed-off-by: André Carvalho <andre.carvalho@corp.globo.com>
This commit is contained in:
parent
e2e2395a3d
commit
a171962328
|
|
@ -1,23 +1,16 @@
|
|||
package driverutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
import "strings"
|
||||
|
||||
// SplitPortProto splits a string in the format port/protocol, defaulting
|
||||
// protocol to "tcp" if not provided.
|
||||
func SplitPortProto(raw string) (port int, protocol string, err error) {
|
||||
func SplitPortProto(raw string) (port string, protocol string, err error) {
|
||||
parts := strings.Split(raw, "/")
|
||||
if len(parts) == 1 {
|
||||
protocol = "tcp"
|
||||
} else {
|
||||
protocol = parts[1]
|
||||
}
|
||||
port, err = strconv.Atoi(parts[0])
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("invalid port number %s: %s", parts[0], err)
|
||||
}
|
||||
port = parts[0]
|
||||
return port, protocol, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,14 +9,13 @@ import (
|
|||
func TestSplitPortProtocol(t *testing.T) {
|
||||
tests := []struct {
|
||||
raw string
|
||||
expectedPort int
|
||||
expectedPort string
|
||||
expectedProto string
|
||||
expectedErr bool
|
||||
}{
|
||||
{"8080/tcp", 8080, "tcp", false},
|
||||
{"90/udp", 90, "udp", false},
|
||||
{"80", 80, "tcp", false},
|
||||
{"abc", 0, "", true},
|
||||
{"8080/tcp", "8080", "tcp", false},
|
||||
{"90/udp", "90", "udp", false},
|
||||
{"80", "80", "tcp", false},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
|
|
|
|||
Loading…
Reference in New Issue