chore(etcd): introduce sanitizeURL

checkURL was a little weird and allowed two different ways to specify
flags. Introduce sanitizeURL which will make sure the host passed in is
simply hostname:port and then appends a Scheme.
This commit is contained in:
Brandon Philips 2013-08-10 19:22:04 -07:00
parent 6610fc39cc
commit 9111617f32

26
etcd.go
View File

@ -142,18 +142,27 @@ var info *Info
// //
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Check a URL and clean it up if the user forgot the schema // sanitizeURL will cleanup a host string in the format hostname:port and
func checkURL(u string, defaultSchema string) string { // attach a schema.
p, err := url.Parse(u) func sanitizeURL(host string, defaultScheme string) string {
// Blank URLs are fine input, just return it
if len(host) == 0 {
return host
}
p, err := url.Parse(host)
if err != nil { if err != nil {
panic(err) fatal(err)
} }
if len(p.Host) == 0 && len(defaultSchema) != 0 { // Make sure the host is in Host:Port format
return checkURL(fmt.Sprintf("%s://%s", defaultSchema, u), "") _, _, err = net.SplitHostPort(host)
if err != nil {
fatal(err)
} }
p = &url.URL{Host: host, Scheme: defaultScheme}
return p.String() return p.String()
} }
@ -226,8 +235,9 @@ func main() {
fatal("ERROR: server name required. e.g. '-n=server_name'") fatal("ERROR: server name required. e.g. '-n=server_name'")
} }
argInfo.RaftURL = checkURL(argInfo.RaftURL, raftDefaultScheme) argInfo.RaftURL = sanitizeURL(argInfo.RaftURL, raftTlsConfig.Scheme)
argInfo.EtcdURL = checkURL(argInfo.EtcdURL, etcdDefaultScheme) argInfo.EtcdURL = sanitizeURL(argInfo.EtcdURL, etcdTlsConfig.Scheme)
argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http")
// Setup commands. // Setup commands.
registerCommands() registerCommands()