diff --git a/etcdctl/command/format.go b/etcdctl/command/format.go index f5fb872ae..cfcdb8902 100644 --- a/etcdctl/command/format.go +++ b/etcdctl/command/format.go @@ -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) + } +} diff --git a/etcdctl/command/get_command.go b/etcdctl/command/get_command.go index beb391eed..63923d2f6 100644 --- a/etcdctl/command/get_command.go +++ b/etcdctl/command/get_command.go @@ -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")) } diff --git a/etcdctl/command/member_commands.go b/etcdctl/command/member_commands.go index 5f73a1dc3..16b74d603 100644 --- a/etcdctl/command/member_commands.go +++ b/etcdctl/command/member_commands.go @@ -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") diff --git a/etcdctl/command/util.go b/etcdctl/command/util.go index d281edf57..85c3aea82 100644 --- a/etcdctl/command/util.go +++ b/etcdctl/command/util.go @@ -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 }