etcdserver: stop using addScheme

This standardizes the flags to use a list of URLs everywhere. The next
step is to enforce the scheme based on TLS settings and support compat
flags.
This commit is contained in:
Brandon Philips
2014-09-30 15:49:13 -07:00
parent aa5b6cdc9e
commit c2f96631d3
8 changed files with 148 additions and 52 deletions

46
pkg/flags/urls.go Normal file
View File

@@ -0,0 +1,46 @@
package flags
import (
"errors"
"fmt"
"net/url"
"strings"
)
// URLs implements the flag.Value interface to allow users to define multiple
// URLs on the command-line
type URLs []url.URL
// Set parses a command line set of URLs formatted like:
// http://127.0.0.1:7001,http://10.1.1.2:80
func (us *URLs) Set(s string) error {
strs := strings.Split(s, ",")
all := make([]url.URL, len(strs))
if len(all) == 0 {
return errors.New("no valid URLs given")
}
for _, in := range strs {
in = strings.TrimSpace(in)
u, err := url.Parse(in)
if err != nil {
return err
}
if u.Scheme != "http" && u.Scheme != "https" {
return fmt.Errorf("URL scheme must be http or https: %s", s)
}
if u.Path != "" {
return fmt.Errorf("URL must not contain a path: %s", s)
}
all = append(all, *u)
}
*us = all
return nil
}
func (us *URLs) String() string {
all := make([]string, len(*us))
for i, u := range *us {
all[i] = u.String()
}
return strings.Join(all, ",")
}

45
pkg/flags/urls_test.go Normal file
View File

@@ -0,0 +1,45 @@
package flags
import (
"testing"
)
func TestValidateURLsBad(t *testing.T) {
tests := []string{
// bad IP specification
":4001",
"127.0:8080",
"123:456",
// bad port specification
"127.0.0.1:foo",
"127.0.0.1:",
// unix sockets not supported
"unix://",
"unix://tmp/etcd.sock",
// bad strings
"somewhere",
"234#$",
"file://foo/bar",
"http://hello/asdf",
}
for i, in := range tests {
u := URLs{}
if err := u.Set(in); err == nil {
t.Errorf(`#%d: unexpected nil error for in=%q`, i, in)
}
}
}
func TestValidateURLsGood(t *testing.T) {
tests := []string{
"https://1.2.3.4:8080",
"http://10.1.1.1:80",
"http://10.1.1.1",
}
for i, in := range tests {
u := URLs{}
if err := u.Set(in); err != nil {
t.Errorf("#%d: err=%v, want nil for in=%q", i, err, in)
}
}
}