mk_command.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package command
  14. import (
  15. "errors"
  16. "os"
  17. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  19. )
  20. // NewMakeCommand returns the CLI command for "mk".
  21. func NewMakeCommand() cli.Command {
  22. return cli.Command{
  23. Name: "mk",
  24. Usage: "make a new key with a given value",
  25. Flags: []cli.Flag{
  26. cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
  27. },
  28. Action: func(c *cli.Context) {
  29. handleKey(c, makeCommandFunc)
  30. },
  31. }
  32. }
  33. // makeCommandFunc executes the "make" command.
  34. func makeCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
  35. if len(c.Args()) == 0 {
  36. return nil, errors.New("Key required")
  37. }
  38. key := c.Args()[0]
  39. value, err := argOrStdin(c.Args(), os.Stdin, 1)
  40. if err != nil {
  41. return nil, errors.New("Value required")
  42. }
  43. ttl := c.Int("ttl")
  44. return client.Create(key, value, uint64(ttl))
  45. }