-
Notifications
You must be signed in to change notification settings - Fork 1
/
addr.go
58 lines (46 loc) · 872 Bytes
/
addr.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package glx
import (
"fmt"
"net"
"strconv"
"strings"
"github.com/spf13/pflag"
)
type Port uint16
type Addr struct {
IP net.IP
Host string
Port Port
}
var _ pflag.Value = (*Addr)(nil)
func (a *Addr) String() string {
var h string
if a.IP != nil {
h = a.IP.String()
} else if a.Host != "" {
h = a.Host
}
return fmt.Sprintf("%s:%d", h, a.Port)
}
func (a *Addr) Set(in string) error {
chunks := strings.Split(in, ":")
if c := strings.Join(chunks[:len(chunks)-1], ":"); c != "" {
if ip := net.ParseIP(c); ip != nil {
a.IP = ip
} else {
a.Host = c
}
}
if c := chunks[len(chunks)-1]; c != "" {
port, err := strconv.ParseUint(c, 10, 16)
if err != nil {
return err
}
a.Port = Port(port)
}
if a.Port == 0 {
return fmt.Errorf("invalid address format: %s", in)
}
return nil
}
func (Addr) Type() string { return "Addr" }