mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
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:
46
pkg/flags/urls.go
Normal file
46
pkg/flags/urls.go
Normal 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
45
pkg/flags/urls_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user