-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddr.go
More file actions
41 lines (32 loc) · 710 Bytes
/
Copy pathaddr.go
File metadata and controls
41 lines (32 loc) · 710 Bytes
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
package main
import (
"fmt"
"net"
"strconv"
"strings"
)
func parseAddress(arg string) (string, error) {
const defaultAddress = ":7777"
arg = strings.TrimSpace(arg)
if arg == "" {
return defaultAddress, nil
}
if port, err := strconv.Atoi(arg); err == nil {
if port < 1 || port > 65535 {
return "", fmt.Errorf("invalid port: %q", arg)
}
return ":" + arg, nil
}
_, port, err := net.SplitHostPort(arg)
if err != nil {
return "", fmt.Errorf("invalid address %q %w", arg, err)
}
n, err := strconv.Atoi(port)
if err != nil {
return "", fmt.Errorf("invalid port %q %w", arg, err)
}
if n < 1 || n > 65535 {
return "", fmt.Errorf("invalid port %q", port)
}
return arg, nil
}