update_command.go 972 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package command
  2. import (
  3. "errors"
  4. "os"
  5. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  6. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  7. )
  8. // NewUpdateCommand returns the CLI command for "update".
  9. func NewUpdateCommand() cli.Command {
  10. return cli.Command{
  11. Name: "update",
  12. Usage: "update an existing key with a given value",
  13. Flags: []cli.Flag{
  14. cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
  15. },
  16. Action: func(c *cli.Context) {
  17. handleKey(c, updateCommandFunc)
  18. },
  19. }
  20. }
  21. // updateCommandFunc executes the "update" command.
  22. func updateCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
  23. if len(c.Args()) == 0 {
  24. return nil, errors.New("Key required")
  25. }
  26. key := c.Args()[0]
  27. value, err := argOrStdin(c.Args(), os.Stdin, 1)
  28. if err != nil {
  29. return nil, errors.New("Value required")
  30. }
  31. ttl := c.Int("ttl")
  32. return client.Update(key, value, uint64(ttl))
  33. }