etcdctl: make set command use etcd/client

This commit is contained in:
Xiang Li 2015-06-04 09:01:44 -07:00 committed by Yicheng Qin
parent e9478ba630
commit db4b18aee3

View File

@ -17,9 +17,11 @@ package command
import ( import (
"errors" "errors"
"os" "os"
"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"
) )
// NewSetCommand returns the CLI command for "set". // NewSetCommand returns the CLI command for "set".
@ -33,28 +35,31 @@ func NewSetCommand() cli.Command {
cli.IntFlag{Name: "swap-with-index", Value: 0, Usage: "previous index"}, cli.IntFlag{Name: "swap-with-index", Value: 0, Usage: "previous index"},
}, },
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
handleKey(c, setCommandFunc) setCommandFunc(c, mustNewKeyAPI(c))
}, },
} }
} }
// setCommandFunc executes the "set" command. // setCommandFunc executes the "set" command.
func setCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) { func setCommandFunc(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]
value, err := argOrStdin(c.Args(), os.Stdin, 1) value, err := argOrStdin(c.Args(), os.Stdin, 1)
if err != nil { if err != nil {
return nil, errors.New("value required") handleError(ExitBadArgs, errors.New("value required"))
} }
ttl := c.Int("ttl") ttl := c.Int("ttl")
prevValue := c.String("swap-with-value") prevValue := c.String("swap-with-value")
prevIndex := c.Int("swap-with-index") prevIndex := c.Int("swap-with-index")
if prevValue == "" && prevIndex == 0 { // TODO: handle transport timeout
return client.Set(key, value, uint64(ttl)) resp, err := ki.Set(context.TODO(), key, value, &client.SetOptions{TTL: time.Second * time.Duration(ttl), PrevIndex: uint64(prevIndex), PrevValue: prevValue})
if err != nil {
handleError(ExitServerError, err)
} }
return client.CompareAndSwap(key, value, uint64(ttl), prevValue, uint64(prevIndex))
printResponseKey(resp, c.GlobalString("output"))
} }