소스 검색

etcdctl: make get command use etcd/client

Xiang Li 10 년 전
부모
커밋
e9478ba630
4개의 변경된 파일78개의 추가작업 그리고 42개의 파일을 삭제
  1. 32 0
      etcdctl/command/format.go
  2. 18 21
      etcdctl/command/get_command.go
  3. 0 21
      etcdctl/command/member_commands.go
  4. 28 0
      etcdctl/command/util.go

+ 32 - 0
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)
+	}
+}

+ 18 - 21
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)
-}
-
-// 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) {
+func getCommandFunc(c *cli.Context, ki client.KeysAPI) {
 	if len(c.Args()) == 0 {
-		return nil, errors.New("key required")
+		handleError(ExitBadArgs, errors.New("key required"))
 	}
+
 	key := c.Args()[0]
 	sorted := c.Bool("sort")
 
-	// Retrieve the value from the server.
-	return client.Get(key, sorted, false)
+	// TODO: handle transport timeout
+	resp, err := ki.Get(context.TODO(), key, &client.GetOptions{Sort: sorted})
+	if err != nil {
+		handleError(ExitServerError, err)
+	}
+
+	if resp.Node.Dir {
+		fmt.Fprintln(os.Stderr, fmt.Sprintf("%s: is a directory", resp.Node.Key))
+		os.Exit(1)
+	}
+
+	printResponseKey(resp, c.GlobalString("output"))
 }

+ 0 - 21
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")

+ 28 - 0
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
 }