Skip to content

Commit

Permalink
Add TCPAddr, UDPAddr, UnixAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed May 20, 2018
1 parent d7a0fc0 commit 832701c
Show file tree
Hide file tree
Showing 7 changed files with 840 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,19 @@ Here's a compact overview:
| [Regexps](https://godoc.org/github.com/sgreben/flagvar#Regexps) | [a-z]+ | []*regexp.Regexp |
| [Strings](https://godoc.org/github.com/sgreben/flagvar#Strings) | "xyxy" | []string |
| [StringSet](https://godoc.org/github.com/sgreben/flagvar#StringSet) | "xyxy" | []string |
| [TCPAddr](https://godoc.org/github.com/sgreben/flagvar#TCPAddr) | 127.0.0.1:10 | net.TCPAddr |
| [TCPAddrs](https://godoc.org/github.com/sgreben/flagvar#TCPAddrs) | 127.0.0.1:10 | []net.TCPAddr |
| [TCPAddrsCSV](https://godoc.org/github.com/sgreben/flagvar#TCPAddrsCSV) | 127.0.0.1:10,:123 | []net.TCPAddr |
| [Template](https://godoc.org/github.com/sgreben/flagvar#Template) | "{{.Size}}" | *template.Template |
| [Templates](https://godoc.org/github.com/sgreben/flagvar#Templates) | "{{.Size}}" | []*template.Template |
| [Time](https://godoc.org/github.com/sgreben/flagvar#Time) | "10:30 AM" | time.Time |
| [Times](https://godoc.org/github.com/sgreben/flagvar#Times) | "10:30 AM" | []time.Time |
| [UDPAddr](https://godoc.org/github.com/sgreben/flagvar#UDPAddr) | 127.0.0.1:10 | net.UDPAddr |
| [UDPAddrs](https://godoc.org/github.com/sgreben/flagvar#UDPAddrs) | 127.0.0.1:10 | []net.UDPAddr |
| [UDPAddrsCSV](https://godoc.org/github.com/sgreben/flagvar#UDPAddrsCSV) | 127.0.0.1:10,:123 | []net.UDPAddr |
| [UnixAddr](https://godoc.org/github.com/sgreben/flagvar#UnixAddr) | /example.sock | net.UnixAddr |
| [UnixAddrs](https://godoc.org/github.com/sgreben/flagvar#UnixAddrs) | /example.sock | []net.UnixAddr |
| [UnixAddrsCSV](https://godoc.org/github.com/sgreben/flagvar#UnixAddrsCSV) | /example.sock,/other.sock | []net.UnixAddr |
| [URL](https://godoc.org/github.com/sgreben/flagvar#URL) | https://github.com | *url.URL |
| [URLs](https://godoc.org/github.com/sgreben/flagvar#URLs) | https://github.com | []*url.URL |
| [Wrap](https://godoc.org/github.com/sgreben/flagvar#Wrap) | | |
Expand Down
107 changes: 107 additions & 0 deletions tcp_addr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package flagvar

import (
"strings"

"net"
)

// TCPAddr is a `flag.Value` for TCP addresses.
// The `Network` field is used if set, otherwise "tcp".
type TCPAddr struct {
Network string

Value *net.TCPAddr
Text string
}

// Set is flag.Value.Set
func (fv *TCPAddr) Set(v string) error {
network := "tcp"
if fv.Network != "" {
network = fv.Network
}
tcpAddr, err := net.ResolveTCPAddr(network, v)
if err != nil {
return err
}
fv.Text = v
fv.Value = tcpAddr
return nil
}

func (fv *TCPAddr) String() string {
return fv.Text
}

// TCPAddrs is a `flag.Value` for TCPAddr addresses.
// The `Network` field is used if set, otherwise "tcp".
type TCPAddrs struct {
Network string

Values []*net.TCPAddr
Texts []string
}

// Set is flag.Value.Set
func (fv *TCPAddrs) Set(v string) error {
network := "tcp"
if fv.Network != "" {
network = fv.Network
}
tcpAddr, err := net.ResolveTCPAddr(network, v)
if err != nil {
return err
}
fv.Texts = append(fv.Texts, v)
fv.Values = append(fv.Values, tcpAddr)
return nil
}

func (fv *TCPAddrs) String() string {
return strings.Join(fv.Texts, ",")
}

// TCPAddrsCSV is a `flag.Value` for TCPAddr addresses.
// The `Network` field is used if set, otherwise "tcp".
// If `Accumulate` is set, the values of all instances of the flag are accumulated.
// The `Separator` field is used instead of the comma when set.
type TCPAddrsCSV struct {
Network string
Separator string
Accumulate bool

Values []*net.TCPAddr
Texts []string
}

// Set is flag.Value.Set
func (fv *TCPAddrsCSV) Set(v string) error {
network := "tcp"
if fv.Network != "" {
network = fv.Network
}
separator := fv.Separator
if separator == "" {
separator = ","
}
if !fv.Accumulate {
fv.Values = fv.Values[:0]
fv.Texts = fv.Texts[:0]
}
parts := strings.Split(v, separator)
for _, part := range parts {
part = strings.TrimSpace(part)
tcpAddr, err := net.ResolveTCPAddr(network, part)
if err != nil {
return err
}
fv.Texts = append(fv.Texts, part)
fv.Values = append(fv.Values, tcpAddr)
}
return nil
}

func (fv *TCPAddrsCSV) String() string {
return strings.Join(fv.Texts, ",")
}
170 changes: 170 additions & 0 deletions tcp_addr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package flagvar_test

import (
"flag"
"net"
"reflect"
"testing"

"github.com/sgreben/flagvar"
)

func TestTCPAddr(t *testing.T) {
fv := flagvar.TCPAddr{}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addr", "")

err := fs.Parse([]string{"-tcp-addr", "127.0.0.1:123"})
if err != nil {
t.Fail()
}
addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:123")
if !reflect.DeepEqual(fv.Value, addr) {
t.Fail()
}
}

func TestTCPAddrNetwork(t *testing.T) {
fv := flagvar.TCPAddr{Network: "tcp4"}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addr", "")

err := fs.Parse([]string{"-tcp-addr", "127.0.0.1:123"})
if err != nil {
t.Fail()
}
addr, _ := net.ResolveTCPAddr("tcp4", "127.0.0.1:123")
if !reflect.DeepEqual(fv.Value, addr) {
t.Fail()
}
}

func TestTCPAddrFail(t *testing.T) {
fv := flagvar.TCPAddr{}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addr", "")

err := fs.Parse([]string{"-tcp-addr", "999.999.999.999:1"})
if err == nil {
t.Fail()
}
}

func TestTCPAddrs(t *testing.T) {
fv := flagvar.TCPAddrs{}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addr", "")

err := fs.Parse([]string{"-tcp-addr", "127.0.0.1:123", "-tcp-addr", ":80"})
if err != nil {
t.Fail()
}
addr1, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:123")
addr2, _ := net.ResolveTCPAddr("tcp", ":80")
if !reflect.DeepEqual(fv.Values, []*net.TCPAddr{addr1, addr2}) {
t.Fail()
}
}

func TestTCPAddrsNetwork(t *testing.T) {
fv := flagvar.TCPAddrs{Network: "tcp4"}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addr", "")

err := fs.Parse([]string{"-tcp-addr", "127.0.0.1:123", "-tcp-addr", ":80"})
if err != nil {
t.Fail()
}
addr1, _ := net.ResolveTCPAddr("tcp4", "127.0.0.1:123")
addr2, _ := net.ResolveTCPAddr("tcp4", ":80")
if !reflect.DeepEqual(fv.Values, []*net.TCPAddr{addr1, addr2}) {
t.Fail()
}
}

func TestTCPAddrsFail(t *testing.T) {
fv := flagvar.TCPAddrs{}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addr", "")

err := fs.Parse([]string{"-tcp-addr", "[a-"})
if err == nil {
t.Fail()
}
}

func TestTCPAddrsCSV(t *testing.T) {
fv := flagvar.TCPAddrsCSV{}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addrs-csv", "")

err := fs.Parse([]string{"-tcp-addrs-csv", "127.0.0.1:123,10.10.1.2:13"})
if err != nil {
t.Fail()
}
addr1, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:123")
addr2, _ := net.ResolveTCPAddr("tcp", "10.10.1.2:13")
if !reflect.DeepEqual(fv.Values, []*net.TCPAddr{addr1, addr2}) {
t.Fail()
}
}

func TestTCPAddrsCSVNetwork(t *testing.T) {
fv := flagvar.TCPAddrsCSV{Network: "tcp4"}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addrs-csv", "")

err := fs.Parse([]string{"-tcp-addrs-csv", "127.0.0.1:123,10.10.1.2:13"})
if err != nil {
t.Fail()
}
addr1, _ := net.ResolveTCPAddr("tcp4", "127.0.0.1:123")
addr2, _ := net.ResolveTCPAddr("tcp4", "10.10.1.2:13")
if !reflect.DeepEqual(fv.Values, []*net.TCPAddr{addr1, addr2}) {
t.Fail()
}
}

func TestTCPAddrsCSVSeparator(t *testing.T) {
fv := flagvar.TCPAddrsCSV{Separator: ";"}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addrs-csv", "")

err := fs.Parse([]string{"-tcp-addrs-csv", "127.0.0.1:123;:32"})
if err != nil {
t.Fail()
}
addr1, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:123")
addr2, _ := net.ResolveTCPAddr("tcp", ":32")
if !reflect.DeepEqual(fv.Values, []*net.TCPAddr{addr1, addr2}) {
t.Fail()
}
}

func TestTCPAddrsCSVAccumulate(t *testing.T) {
fv := flagvar.TCPAddrsCSV{Accumulate: true}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addrs-csv", "")

err := fs.Parse([]string{"-tcp-addrs-csv", "127.0.0.1:123,10.10.1.2:432", "-tcp-addrs-csv", "10.10.1.2:432"})
if err != nil {
t.Fail()
}
addr1, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:123")
addr2, _ := net.ResolveTCPAddr("tcp", "10.10.1.2:432")
addr3, _ := net.ResolveTCPAddr("tcp", "10.10.1.2:432")
if !reflect.DeepEqual(fv.Values, []*net.TCPAddr{addr1, addr2, addr3}) {
t.Fail()
}
}

func TestTCPAddrsCSVFail(t *testing.T) {
fv := flagvar.TCPAddrsCSV{}
var fs flag.FlagSet
fs.Var(&fv, "tcp-addrs-csv", "")

err := fs.Parse([]string{"-tcp-addrs-csv", "999.999.999.1:1"})
if err == nil {
t.Fail()
}
}
Loading

0 comments on commit 832701c

Please sign in to comment.