From 9d9c3a7180685fe9c2f2fd596aa99d5e2904d34f Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 4 Jun 2015 09:38:10 -0700 Subject: [PATCH] etcdctl: make setdir/mkdir use etcd/client --- etcdctl/command/mkdir_command.go | 19 +++++++++++++------ etcdctl/command/set_dir_command.go | 17 ++--------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/etcdctl/command/mkdir_command.go b/etcdctl/command/mkdir_command.go index d86b0eebb..503c7dbd9 100644 --- a/etcdctl/command/mkdir_command.go +++ b/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) + } } diff --git a/etcdctl/command/set_dir_command.go b/etcdctl/command/set_dir_command.go index 8d847851b..9aa93736c 100644 --- a/etcdctl/command/set_dir_command.go +++ b/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)) -}