From db4b18aee3dd644614b15c0baca6c8b09ca1c491 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 4 Jun 2015 09:01:44 -0700 Subject: [PATCH] etcdctl: make set command use etcd/client --- etcdctl/command/set_command.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/etcdctl/command/set_command.go b/etcdctl/command/set_command.go index 118379624..9800c5e80 100644 --- a/etcdctl/command/set_command.go +++ b/etcdctl/command/set_command.go @@ -17,9 +17,11 @@ package command import ( "errors" "os" + "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" ) // 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"}, }, Action: func(c *cli.Context) { - handleKey(c, setCommandFunc) + setCommandFunc(c, mustNewKeyAPI(c)) }, } } // 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 { - return nil, errors.New("key required") + handleError(ExitBadArgs, errors.New("key required")) } key := c.Args()[0] value, err := argOrStdin(c.Args(), os.Stdin, 1) if err != nil { - return nil, errors.New("value required") + handleError(ExitBadArgs, errors.New("value required")) } ttl := c.Int("ttl") prevValue := c.String("swap-with-value") prevIndex := c.Int("swap-with-index") - if prevValue == "" && prevIndex == 0 { - return client.Set(key, value, uint64(ttl)) + // TODO: handle transport timeout + 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")) }