mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
etcdctl: make set command use etcd/client
This commit is contained in:
parent
e9478ba630
commit
db4b18aee3
@ -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"))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user