etcdctl: make rm use etcd/client

This commit is contained in:
Xiang Li
2015-06-04 16:44:29 -07:00
committed by Yicheng Qin
parent 9d9c3a7180
commit e3fcc450cf
3 changed files with 31 additions and 20 deletions

View File

@ -212,6 +212,9 @@ type DeleteOptions struct {
// or explicitly set to false, only a single Node will be // or explicitly set to false, only a single Node will be
// deleted. // deleted.
Recursive bool Recursive bool
// Dir specifies whether or not this Node should be removed as a directory.
Dir bool
} }
type Watcher interface { type Watcher interface {
@ -349,6 +352,7 @@ func (k *httpKeysAPI) Delete(ctx context.Context, key string, opts *DeleteOption
if opts != nil { if opts != nil {
act.PrevValue = opts.PrevValue act.PrevValue = opts.PrevValue
act.PrevIndex = opts.PrevIndex act.PrevIndex = opts.PrevIndex
act.Dir = opts.Dir
act.Recursive = opts.Recursive act.Recursive = opts.Recursive
} }
@ -520,6 +524,7 @@ type deleteAction struct {
Key string Key string
PrevValue string PrevValue string
PrevIndex uint64 PrevIndex uint64
Dir bool
Recursive bool Recursive bool
} }
@ -533,6 +538,9 @@ func (a *deleteAction) HTTPRequest(ep url.URL) *http.Request {
if a.PrevIndex != 0 { if a.PrevIndex != 0 {
params.Set("prevIndex", strconv.FormatUint(a.PrevIndex, 10)) params.Set("prevIndex", strconv.FormatUint(a.PrevIndex, 10))
} }
if a.Dir {
params.Set("dir", "true")
}
if a.Recursive { if a.Recursive {
params.Set("recursive", "true") params.Set("recursive", "true")
} }

View File

@ -79,7 +79,11 @@ func printResponseKey(resp *client.Response, format string) {
// Format the result. // Format the result.
switch format { switch format {
case "simple": case "simple":
if resp.Action != "delete" {
fmt.Println(resp.Node.Value) fmt.Println(resp.Node.Value)
} else {
fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
}
case "extended": case "extended":
// Extended prints in a rfc2822 style format // Extended prints in a rfc2822 style format
fmt.Println("Key:", resp.Node.Key) fmt.Println("Key:", resp.Node.Key)
@ -92,8 +96,10 @@ func printResponseKey(resp *client.Response, format string) {
fmt.Println("TTL:", resp.Node.TTL) fmt.Println("TTL:", resp.Node.TTL)
fmt.Println("Index:", resp.Index) fmt.Println("Index:", resp.Index)
if resp.Action != "delete" {
fmt.Println("") fmt.Println("")
fmt.Println(resp.Node.Value) fmt.Println(resp.Node.Value)
}
case "json": case "json":
b, err := json.Marshal(resp) b, err := json.Marshal(resp)
if err != nil { if err != nil {

View File

@ -18,14 +18,14 @@ import (
"errors" "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/Godeps/_workspace/src/golang.org/x/net/context" // NewRemoveCommand returns the CLI command for "rm".
"github.com/coreos/etcd/client"
) )
// NewRemoveCommand returns the CLI command for "rm".
func NewRemoveCommand() cli.Command { func NewRemoveCommand() cli.Command {
return cli.Command{ return cli.Command{
Name: "rm", Name: "rm",
Usage: "remove a key", Usage: "remove a key or a directory",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{Name: "dir", Usage: "removes the key if it is an empty directory or a key-value pair"}, cli.BoolFlag{Name: "dir", Usage: "removes the key if it is an empty directory or a key-value pair"},
cli.BoolFlag{Name: "recursive", Usage: "removes the key and all child keys(if it is a directory)"}, cli.BoolFlag{Name: "recursive", Usage: "removes the key and all child keys(if it is a directory)"},
@ -33,32 +33,29 @@ func NewRemoveCommand() cli.Command {
cli.IntFlag{Name: "with-index", Value: 0, Usage: "previous index"}, cli.IntFlag{Name: "with-index", Value: 0, Usage: "previous index"},
}, },
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
handleAll(c, removeCommandFunc) rmCommandFunc(c, mustNewKeyAPI(c))
}, },
} }
} }
// removeCommandFunc executes the "rm" command. // rmCommandFunc executes the "rm" command.
func removeCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) { func rmCommandFunc(c *cli.Context, ki client.KeysAPI) {
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]
recursive := c.Bool("recursive") recursive := c.Bool("recursive")
dir := c.Bool("dir") dir := c.Bool("dir")
// TODO: distinguish with flag is not set and empty flag
// the cli pkg need to provide this feature
prevValue := c.String("with-value") prevValue := c.String("with-value")
prevIndex := uint64(c.Int("with-index")) prevIndex := c.Int("with-index")
if prevValue != "" || prevIndex != 0 { // TODO: handle transport timeout
return client.CompareAndDelete(key, prevValue, prevIndex) resp, err := ki.Delete(context.TODO(), key, &client.DeleteOptions{PrevIndex: uint64(prevIndex), PrevValue: prevValue, Dir: dir, Recursive: recursive})
if err != nil {
handleError(ExitServerError, err)
} }
if recursive || !dir { if !resp.Node.Dir {
return client.Delete(key, recursive) printResponseKey(resp, c.GlobalString("output"))
} }
return client.DeleteDir(key)
} }