etcdctl: make setdir/mkdir use etcd/client

This commit is contained in:
Xiang Li
2015-06-04 09:38:10 -07:00
committed by Yicheng Qin
parent db4b18aee3
commit 9d9c3a7180
2 changed files with 15 additions and 21 deletions

View File

@ -16,9 +16,11 @@ package command
import ( import (
"errors" "errors"
"time"
"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"
) )
// NewMakeDirCommand returns the CLI command for "mkdir". // 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"}, cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
}, },
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
handleDir(c, makeDirCommandFunc) mkdirCommandFunc(c, mustNewKeyAPI(c), client.PrevNoExist)
}, },
} }
} }
// makeDirCommandFunc executes the "mkdir" command. // mkdirCommandFunc executes the "mkdir" command.
func makeDirCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) { func mkdirCommandFunc(c *cli.Context, ki client.KeysAPI, prevExist client.PrevExistType) {
if len(c.Args()) == 0 { if len(c.Args()) == 0 {
return nil, errors.New("key required") handleError(ExitBadArgs, errors.New("key required"))
} }
key := c.Args()[0] key := c.Args()[0]
ttl := c.Int("ttl") 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)
}
} }

View File

@ -15,10 +15,8 @@
package command package command
import ( import (
"errors"
"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/client"
) )
// NewSetDirCommand returns the CLI command for "setDir". // 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"}, cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
}, },
Action: func(c *cli.Context) { 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))
}