etcdctl: make rm use etcd/client
This commit is contained in:
@ -212,6 +212,9 @@ type DeleteOptions struct {
|
||||
// or explicitly set to false, only a single Node will be
|
||||
// deleted.
|
||||
Recursive bool
|
||||
|
||||
// Dir specifies whether or not this Node should be removed as a directory.
|
||||
Dir bool
|
||||
}
|
||||
|
||||
type Watcher interface {
|
||||
@ -349,6 +352,7 @@ func (k *httpKeysAPI) Delete(ctx context.Context, key string, opts *DeleteOption
|
||||
if opts != nil {
|
||||
act.PrevValue = opts.PrevValue
|
||||
act.PrevIndex = opts.PrevIndex
|
||||
act.Dir = opts.Dir
|
||||
act.Recursive = opts.Recursive
|
||||
}
|
||||
|
||||
@ -520,6 +524,7 @@ type deleteAction struct {
|
||||
Key string
|
||||
PrevValue string
|
||||
PrevIndex uint64
|
||||
Dir bool
|
||||
Recursive bool
|
||||
}
|
||||
|
||||
@ -533,6 +538,9 @@ func (a *deleteAction) HTTPRequest(ep url.URL) *http.Request {
|
||||
if a.PrevIndex != 0 {
|
||||
params.Set("prevIndex", strconv.FormatUint(a.PrevIndex, 10))
|
||||
}
|
||||
if a.Dir {
|
||||
params.Set("dir", "true")
|
||||
}
|
||||
if a.Recursive {
|
||||
params.Set("recursive", "true")
|
||||
}
|
||||
|
@ -79,7 +79,11 @@ func printResponseKey(resp *client.Response, format string) {
|
||||
// Format the result.
|
||||
switch format {
|
||||
case "simple":
|
||||
fmt.Println(resp.Node.Value)
|
||||
if resp.Action != "delete" {
|
||||
fmt.Println(resp.Node.Value)
|
||||
} else {
|
||||
fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
|
||||
}
|
||||
case "extended":
|
||||
// Extended prints in a rfc2822 style format
|
||||
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("Index:", resp.Index)
|
||||
fmt.Println("")
|
||||
fmt.Println(resp.Node.Value)
|
||||
if resp.Action != "delete" {
|
||||
fmt.Println("")
|
||||
fmt.Println(resp.Node.Value)
|
||||
}
|
||||
case "json":
|
||||
b, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
|
@ -18,14 +18,14 @@ 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/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 {
|
||||
return cli.Command{
|
||||
Name: "rm",
|
||||
Usage: "remove a key",
|
||||
Usage: "remove a key or a directory",
|
||||
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: "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"},
|
||||
},
|
||||
Action: func(c *cli.Context) {
|
||||
handleAll(c, removeCommandFunc)
|
||||
rmCommandFunc(c, mustNewKeyAPI(c))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// removeCommandFunc executes the "rm" command.
|
||||
func removeCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
|
||||
// rmCommandFunc executes the "rm" command.
|
||||
func rmCommandFunc(c *cli.Context, ki client.KeysAPI) {
|
||||
if len(c.Args()) == 0 {
|
||||
return nil, errors.New("key required")
|
||||
handleError(ExitBadArgs, errors.New("key required"))
|
||||
}
|
||||
key := c.Args()[0]
|
||||
recursive := c.Bool("recursive")
|
||||
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")
|
||||
prevIndex := uint64(c.Int("with-index"))
|
||||
prevIndex := c.Int("with-index")
|
||||
|
||||
if prevValue != "" || prevIndex != 0 {
|
||||
return client.CompareAndDelete(key, prevValue, prevIndex)
|
||||
// TODO: handle transport timeout
|
||||
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 {
|
||||
return client.Delete(key, recursive)
|
||||
if !resp.Node.Dir {
|
||||
printResponseKey(resp, c.GlobalString("output"))
|
||||
}
|
||||
|
||||
return client.DeleteDir(key)
|
||||
}
|
||||
|
Reference in New Issue
Block a user