// Just like handlePrint but also passed the context of the command func handleContextualPrint(c *cli.Context, fn handlerFunc, pFn contextualPrintFunc) { resp, err := rawhandle(c, fn) if err != nil { handleError(ErrorFromEtcd, err) } if resp != nil && pFn != nil { pFn(c, resp, c.GlobalString("output")) } }
// handlePrint wraps the command function handlers to parse global flags // into a client and to properly format the response objects. func handlePrint(c *cli.Context, fn handlerFunc, pFn printFunc) { resp, err := rawhandle(c, fn) // Print error and exit, if necessary. if err != nil { handleError(ErrorFromEtcd, err) } if resp != nil && pFn != nil { pFn(resp, c.GlobalString("output")) } }
// 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") 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" } peers := strings.Split(peerstr, ",") // 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 } client := etcd.NewClient(peers) if c.GlobalBool("debug") { go dumpCURL(client) } // Sync cluster. if sync { if ok := client.SyncCluster(); !ok { handleError(FailedToConnectToHost, errors.New("Cannot sync with the cluster using peers "+strings.Join(peers, ", "))) } } if c.GlobalBool("debug") { fmt.Fprintf(os.Stderr, "Cluster-Peers: %s\n", strings.Join(client.GetCluster(), " ")) } // Execute handler function. return fn(c, client) }