Sfoglia il codice sorgente

etcdctl: ls use etcd/client

Xiang Li 10 anni fa
parent
commit
b20c06348d
2 ha cambiato i file con 23 aggiunte e 34 eliminazioni
  1. 0 13
      etcdctl/command/handle.go
  2. 23 21
      etcdctl/command/ls_command.go

+ 0 - 13
etcdctl/command/handle.go

@@ -120,19 +120,6 @@ func handlePrint(c *cli.Context, fn handlerFunc, pFn printFunc) {
 	}
 	}
 }
 }
 
 
-// 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(ExitServerError, err)
-	}
-
-	if resp != nil && pFn != nil {
-		pFn(c, resp, c.GlobalString("output"))
-	}
-}
-
 // handleDir handles a request that wants to do operations on a single dir.
 // handleDir handles a request that wants to do operations on a single dir.
 // Dir cannot be printed out, so we set NIL print function here.
 // Dir cannot be printed out, so we set NIL print function here.
 func handleDir(c *cli.Context, fn handlerFunc) {
 func handleDir(c *cli.Context, fn handlerFunc) {

+ 23 - 21
etcdctl/command/ls_command.go

@@ -15,10 +15,12 @@
 package command
 package command
 
 
 import (
 import (
+	"errors"
 	"fmt"
 	"fmt"
 
 
 	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
 	"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"
 )
 )
 
 
 func NewLsCommand() cli.Command {
 func NewLsCommand() cli.Command {
@@ -31,19 +33,33 @@ func NewLsCommand() cli.Command {
 			cli.BoolFlag{Name: "p", Usage: "append slash (/) to directories"},
 			cli.BoolFlag{Name: "p", Usage: "append slash (/) to directories"},
 		},
 		},
 		Action: func(c *cli.Context) {
 		Action: func(c *cli.Context) {
-			handleLs(c, lsCommandFunc)
+			lsCommandFunc(c, mustNewKeyAPI(c))
 		},
 		},
 	}
 	}
 }
 }
 
 
-// handleLs handles a request that intends to do ls-like operations.
-func handleLs(c *cli.Context, fn handlerFunc) {
-	handleContextualPrint(c, fn, printLs)
+// lsCommandFunc executes the "ls" command.
+func lsCommandFunc(c *cli.Context, ki client.KeysAPI) {
+	if len(c.Args()) == 0 {
+		handleError(ExitBadArgs, errors.New("key required"))
+	}
+
+	key := c.Args()[0]
+	sort := c.Bool("sort")
+	recursive := c.Bool("recursive")
+
+	// TODO: handle transport timeout
+	resp, err := ki.Get(context.TODO(), key, &client.GetOptions{Sort: sort, Recursive: recursive})
+	if err != nil {
+		handleError(ExitServerError, err)
+	}
+
+	printLs(c, resp)
 }
 }
 
 
 // printLs writes a response out in a manner similar to the `ls` command in unix.
 // printLs writes a response out in a manner similar to the `ls` command in unix.
 // Non-empty directories list their contents and files list their name.
 // Non-empty directories list their contents and files list their name.
-func printLs(c *cli.Context, resp *etcd.Response, format string) {
+func printLs(c *cli.Context, resp *client.Response) {
 	if !resp.Node.Dir {
 	if !resp.Node.Dir {
 		fmt.Println(resp.Node.Key)
 		fmt.Println(resp.Node.Key)
 	}
 	}
@@ -52,22 +68,8 @@ func printLs(c *cli.Context, resp *etcd.Response, format string) {
 	}
 	}
 }
 }
 
 
-// lsCommandFunc executes the "ls" command.
-func lsCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
-	key := "/"
-	if len(c.Args()) != 0 {
-		key = c.Args()[0]
-	}
-	recursive := c.Bool("recursive")
-	sort := c.Bool("sort")
-
-	// Retrieve the value from the server.
-	return client.Get(key, sort, recursive)
-}
-
 // rPrint recursively prints out the nodes in the node structure.
 // rPrint recursively prints out the nodes in the node structure.
-func rPrint(c *cli.Context, n *etcd.Node) {
-
+func rPrint(c *cli.Context, n *client.Node) {
 	if n.Dir && c.Bool("p") {
 	if n.Dir && c.Bool("p") {
 		fmt.Println(fmt.Sprintf("%v/", n.Key))
 		fmt.Println(fmt.Sprintf("%v/", n.Key))
 	} else {
 	} else {