Prechádzať zdrojové kódy

etcdctl: make setdir/mkdir use etcd/client

Xiang Li 10 rokov pred
rodič
commit
9d9c3a7180

+ 13 - 6
etcdctl/command/mkdir_command.go

@@ -16,9 +16,11 @@ package command
 
 import (
 	"errors"
+	"time"
 
 	"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"
 )
 
 // NewMakeDirCommand returns the CLI command for "mkdir".
@@ -30,18 +32,23 @@ func NewMakeDirCommand() cli.Command {
 			cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
 		},
 		Action: func(c *cli.Context) {
-			handleDir(c, makeDirCommandFunc)
+			mkdirCommandFunc(c, mustNewKeyAPI(c), client.PrevNoExist)
 		},
 	}
 }
 
-// makeDirCommandFunc executes the "mkdir" command.
-func makeDirCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
+// mkdirCommandFunc executes the "mkdir" command.
+func mkdirCommandFunc(c *cli.Context, ki client.KeysAPI, prevExist client.PrevExistType) {
 	if len(c.Args()) == 0 {
-		return nil, errors.New("key required")
+		handleError(ExitBadArgs, errors.New("key required"))
 	}
+
 	key := c.Args()[0]
 	ttl := c.Int("ttl")
 
-	return client.CreateDir(key, uint64(ttl))
+	// TODO: handle transport timeout
+	_, err := ki.Set(context.TODO(), key, "", &client.SetOptions{TTL: time.Second * time.Duration(ttl), Dir: true, PrevExist: prevExist})
+	if err != nil {
+		handleError(ExitServerError, err)
+	}
 }

+ 2 - 15
etcdctl/command/set_dir_command.go

@@ -15,10 +15,8 @@
 package command
 
 import (
-	"errors"
-
 	"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/client"
 )
 
 // NewSetDirCommand returns the CLI command for "setDir".
@@ -30,18 +28,7 @@ func NewSetDirCommand() cli.Command {
 			cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
 		},
 		Action: func(c *cli.Context) {
-			handleDir(c, setDirCommandFunc)
+			mkdirCommandFunc(c, mustNewKeyAPI(c), client.PrevIgnore)
 		},
 	}
 }
-
-// setDirCommandFunc executes the "setDir" command.
-func setDirCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
-	if len(c.Args()) == 0 {
-		return nil, errors.New("Key required")
-	}
-	key := c.Args()[0]
-	ttl := c.Int("ttl")
-
-	return client.SetDir(key, uint64(ttl))
-}