Skip to content

Commit

Permalink
[logdog/common/types] make StreamAddr implement flag.Value.
Browse files Browse the repository at this point in the history
R=dnj@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2895233004
  • Loading branch information
riannucci authored and Commit bot committed May 23, 2017
1 parent 60ceda8 commit 4363168
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions logdog/common/types/streamaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package types

import (
"flag"
"net/url"
"strings"

Expand All @@ -26,6 +27,18 @@ type StreamAddr struct {
Path StreamPath
}

var _ flag.Value = (*StreamAddr)(nil)

// Set implements flag.Value
func (s *StreamAddr) Set(v string) error {
a, err := ParseURL(v)
if err != nil {
return err
}
*s = *a
return nil
}

// String returns a string representation of this address.
func (s *StreamAddr) String() string { return s.URL().String() }

Expand Down
22 changes: 22 additions & 0 deletions logdog/common/types/streamaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package types

import (
"flag"
"fmt"
"net/url"
"testing"
Expand Down Expand Up @@ -57,4 +58,25 @@ func TestStreamAddr(t *testing.T) {
})
}
})

Convey(`StreamAddr is a flag.Value`, t, func() {
fs := flag.NewFlagSet("testing", flag.ContinueOnError)
a := &StreamAddr{}

fs.Var(a, "addr", "its totally an address of a thing")

Convey(`good`, func() {
So(fs.Parse([]string{"-addr", "logdog://host/project/a/+/b"}), ShouldBeNil)
So(a, ShouldResemble, &StreamAddr{
"host",
"project",
"a/+/b",
})
})

Convey(`bad`, func() {
So(fs.Parse([]string{"-addr", "://host/project/a/+/b"}), ShouldErrLike,
"failed to parse URL")
})
})
}

0 comments on commit 4363168

Please sign in to comment.