diff --git a/etcdctl/command/handle.go b/etcdctl/command/handle.go index 54f2aaf59..d5565f787 100644 --- a/etcdctl/command/handle.go +++ b/etcdctl/command/handle.go @@ -20,7 +20,6 @@ import ( "encoding/json" "errors" "fmt" - "net/url" "os" "strings" @@ -40,72 +39,29 @@ func dumpCURL(client *etcd.Client) { } } -// createHttpPath attaches http scheme to the given address if needed -func createHttpPath(addr string) (string, error) { - u, err := url.Parse(addr) - if err != nil { - return "", err - } - - if u.Scheme == "" { - u.Scheme = "http" - } - return u.String(), nil -} - -func getPeersFlagValue(c *cli.Context) []string { - peerstr := c.GlobalString("peers") - - // Use an environment variable if nothing was supplied on the - // command line - if peerstr == "" { - peerstr = os.Getenv("ETCDCTL_PEERS") - } - - // If we still don't have peers, use a default - if peerstr == "" { - peerstr = "127.0.0.1:4001" - } - - return strings.Split(peerstr, ",") -} - // rawhandle wraps the command function handlers and sets up the // environment but performs no output formatting. func rawhandle(c *cli.Context, fn handlerFunc) (*etcd.Response, error) { - sync := !c.GlobalBool("no-sync") - - peers := getPeersFlagValue(c) - - // If no sync, create http path for each peer address - if !sync { - revisedPeers := make([]string, 0) - for _, peer := range peers { - if revisedPeer, err := createHttpPath(peer); err != nil { - fmt.Fprintf(os.Stderr, "Unsupported url %v: %v\n", peer, err) - } else { - revisedPeers = append(revisedPeers, revisedPeer) - } - } - peers = revisedPeers + endpoints, err := getEndpoints(c) + if err != nil { + return nil, err } - client := etcd.NewClient(peers) + client := etcd.NewClient(endpoints) if c.GlobalBool("debug") { go dumpCURL(client) } // Sync cluster. - if sync { + if !c.GlobalBool("no-sync") { if ok := client.SyncCluster(); !ok { - handleError(FailedToConnectToHost, errors.New("Cannot sync with the cluster using peers "+strings.Join(peers, ", "))) + handleError(FailedToConnectToHost, errors.New("cannot sync with the cluster using endpoints "+strings.Join(endpoints, ", "))) } } if c.GlobalBool("debug") { - fmt.Fprintf(os.Stderr, "Cluster-Peers: %s\n", - strings.Join(client.GetCluster(), " ")) + fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(client.GetCluster(), ", ")) } // Execute handler function. diff --git a/etcdctl/command/member_commands.go b/etcdctl/command/member_commands.go index f79549a4f..41ff125df 100644 --- a/etcdctl/command/member_commands.go +++ b/etcdctl/command/member_commands.go @@ -52,14 +52,13 @@ func NewMemberCommand() cli.Command { } func mustNewMembersAPI(c *cli.Context) client.MembersAPI { - peers := getPeersFlagValue(c) - for i, p := range peers { - if !strings.HasPrefix(p, "http") && !strings.HasPrefix(p, "https") { - peers[i] = fmt.Sprintf("http://%s", p) - } + eps, err := getEndpoints(c) + if err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + os.Exit(1) } - hc, err := client.NewHTTPClient(&http.Transport{}, peers) + hc, err := client.NewHTTPClient(&http.Transport{}, eps) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) @@ -75,6 +74,10 @@ func mustNewMembersAPI(c *cli.Context) client.MembersAPI { } } + if c.GlobalBool("debug") { + fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", ")) + } + return client.NewMembersAPI(hc) } diff --git a/etcdctl/command/util.go b/etcdctl/command/util.go index 7dd29227c..76970d8c9 100644 --- a/etcdctl/command/util.go +++ b/etcdctl/command/util.go @@ -20,7 +20,11 @@ import ( "errors" "io" "io/ioutil" + "net/url" + "os" "strings" + + "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli" ) var ( @@ -49,3 +53,45 @@ func argOrStdin(args []string, stdin io.Reader, i int) (string, error) { } return string(bytes), nil } + +func maybeAddScheme(maybeAddr string) (string, error) { + u, err := url.Parse(maybeAddr) + if err != nil { + return "", err + } + + if u.Scheme == "" { + u.Scheme = "http" + } + + return u.String(), nil +} + +func getPeersFlagValue(c *cli.Context) []string { + peerstr := c.GlobalString("peers") + + // Use an environment variable if nothing was supplied on the + // command line + if peerstr == "" { + peerstr = os.Getenv("ETCDCTL_PEERS") + } + + // If we still don't have peers, use a default + if peerstr == "" { + peerstr = "127.0.0.1:4001" + } + + return strings.Split(peerstr, ",") +} + +func getEndpoints(c *cli.Context) ([]string, error) { + eps := getPeersFlagValue(c) + var err error + for i, ep := range eps { + eps[i], err = maybeAddScheme(ep) + if err != nil { + return nil, err + } + } + return eps, nil +}