-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
840 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ",") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.