etcdctl: centralize getEndpoints logic
parent
4b555dba99
commit
f4ea274555
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue