etcdctl: make get command use etcd/client

This commit is contained in:
Xiang Li 2015-06-03 20:26:13 -07:00 committed by Yicheng Qin
parent 147b14cfc0
commit e9478ba630
4 changed files with 78 additions and 42 deletions

View File

@ -20,6 +20,7 @@ import (
"os"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
"github.com/coreos/etcd/client"
)
// printKey writes the etcd response to STDOUT in the given format.
@ -72,3 +73,34 @@ func printKeyOnly(resp *etcd.Response, format string) {
fmt.Fprintln(os.Stderr, "Unsupported output format:", format)
}
}
// printResponseKey only supports to print key correctly.
func printResponseKey(resp *client.Response, format string) {
// Format the result.
switch format {
case "simple":
fmt.Println(resp.Node.Value)
case "extended":
// Extended prints in a rfc2822 style format
fmt.Println("Key:", resp.Node.Key)
fmt.Println("Created-Index:", resp.Node.CreatedIndex)
fmt.Println("Modified-Index:", resp.Node.ModifiedIndex)
if resp.PrevNode != nil {
fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
}
fmt.Println("TTL:", resp.Node.TTL)
fmt.Println("Index:", resp.Index)
fmt.Println("")
fmt.Println(resp.Node.Value)
case "json":
b, err := json.Marshal(resp)
if err != nil {
panic(err)
}
fmt.Println(string(b))
default:
fmt.Fprintln(os.Stderr, "Unsupported output format:", format)
}
}

View File

@ -20,7 +20,8 @@ import (
"os"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/client"
)
// NewGetCommand returns the CLI command for "get".
@ -32,34 +33,30 @@ func NewGetCommand() cli.Command {
cli.BoolFlag{Name: "sort", Usage: "returns result in sorted order"},
},
Action: func(c *cli.Context) {
handleGet(c, getCommandFunc)
getCommandFunc(c, mustNewKeyAPI(c))
},
}
}
// handleGet handles a request that intends to do get-like operations.
func handleGet(c *cli.Context, fn handlerFunc) {
handlePrint(c, fn, printGet)
}
// getCommandFunc executes the "get" command.
func getCommandFunc(c *cli.Context, ki client.KeysAPI) {
if len(c.Args()) == 0 {
handleError(ExitBadArgs, errors.New("key required"))
}
key := c.Args()[0]
sorted := c.Bool("sort")
// TODO: handle transport timeout
resp, err := ki.Get(context.TODO(), key, &client.GetOptions{Sort: sorted})
if err != nil {
handleError(ExitServerError, err)
}
// printGet writes error message when getting the value of a directory.
func printGet(resp *etcd.Response, format string) {
if resp.Node.Dir {
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s: is a directory", resp.Node.Key))
os.Exit(1)
}
printKey(resp, format)
}
// getCommandFunc executes the "get" command.
func getCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
if len(c.Args()) == 0 {
return nil, errors.New("key required")
}
key := c.Args()[0]
sorted := c.Bool("sort")
// Retrieve the value from the server.
return client.Get(key, sorted, false)
printResponseKey(resp, c.GlobalString("output"))
}

View File

@ -48,27 +48,6 @@ func NewMemberCommand() cli.Command {
}
}
func mustNewMembersAPI(c *cli.Context) client.MembersAPI {
hc := mustNewClient(c)
if !c.GlobalBool("no-sync") {
ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
err := hc.Sync(ctx)
cancel()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
if c.GlobalBool("debug") {
fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
}
return client.NewMembersAPI(hc)
}
func actionMemberList(c *cli.Context) {
if len(c.Args()) != 0 {
fmt.Fprintln(os.Stderr, "No arguments accepted")

View File

@ -25,6 +25,7 @@ import (
"strings"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/client"
"github.com/coreos/etcd/pkg/transport"
)
@ -115,6 +116,14 @@ func getTransport(c *cli.Context) (*http.Transport, error) {
return transport.NewTransport(tls)
}
func mustNewKeyAPI(c *cli.Context) client.KeysAPI {
return client.NewKeysAPI(mustNewClient(c))
}
func mustNewMembersAPI(c *cli.Context) client.MembersAPI {
return client.NewMembersAPI(mustNewClient(c))
}
func mustNewClient(c *cli.Context) client.Client {
eps, err := getEndpoints(c)
if err != nil {
@ -149,5 +158,24 @@ func mustNewClient(c *cli.Context) client.Client {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if !c.GlobalBool("no-sync") {
ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
err := hc.Sync(ctx)
cancel()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
if c.GlobalBool("debug") {
fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
}
if c.GlobalBool("debug") {
client.EnablecURLDebug()
}
return hc
}