get_command.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "fmt"
  17. "strings"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  20. "github.com/coreos/etcd/clientv3"
  21. )
  22. var (
  23. getLimit int64
  24. getSortOrder string
  25. getSortTarget string
  26. getPrefix bool
  27. getFromKey bool
  28. )
  29. // NewGetCommand returns the cobra command for "get".
  30. func NewGetCommand() *cobra.Command {
  31. cmd := &cobra.Command{
  32. Use: "get [options] <key> [range_end]",
  33. Short: "Get gets the key or a range of keys.",
  34. Run: getCommandFunc,
  35. }
  36. cmd.Flags().StringVar(&getSortOrder, "order", "", "order of results; ASCEND or DESCEND")
  37. cmd.Flags().StringVar(&getSortTarget, "sort-by", "", "sort target; CREATE, KEY, MODIFY, VALUE, or VERSION")
  38. cmd.Flags().Int64Var(&getLimit, "limit", 0, "maximum number of results")
  39. cmd.Flags().BoolVar(&getPrefix, "prefix", false, "get keys with matching prefix")
  40. cmd.Flags().BoolVar(&getFromKey, "from-key", false, "get keys that are greater than or equal to the given key")
  41. // TODO: add consistency.
  42. return cmd
  43. }
  44. // getCommandFunc executes the "get" command.
  45. func getCommandFunc(cmd *cobra.Command, args []string) {
  46. key, opts := getGetOp(cmd, args)
  47. resp, err := mustClientFromCmd(cmd).Get(context.TODO(), key, opts...)
  48. if err != nil {
  49. ExitWithError(ExitError, err)
  50. }
  51. display.Get(*resp)
  52. }
  53. func getGetOp(cmd *cobra.Command, args []string) (string, []clientv3.OpOption) {
  54. if len(args) == 0 {
  55. ExitWithError(ExitBadArgs, fmt.Errorf("range command needs arguments."))
  56. }
  57. if getPrefix && getFromKey {
  58. ExitWithError(ExitBadArgs, fmt.Errorf("`--prefix` and `--from-key` cannot be set at the same time, choose one."))
  59. }
  60. opts := []clientv3.OpOption{}
  61. key := args[0]
  62. if len(args) > 1 {
  63. if getPrefix || getFromKey {
  64. ExitWithError(ExitBadArgs, fmt.Errorf("too many arguments, only accept one arguement when `--prefix` or `--from-key` is set."))
  65. }
  66. opts = append(opts, clientv3.WithRange(args[1]))
  67. }
  68. opts = append(opts, clientv3.WithLimit(getLimit))
  69. sortByOrder := clientv3.SortNone
  70. sortOrder := strings.ToUpper(getSortOrder)
  71. switch {
  72. case sortOrder == "ASCEND":
  73. sortByOrder = clientv3.SortAscend
  74. case sortOrder == "DESCEND":
  75. sortByOrder = clientv3.SortDescend
  76. case sortOrder == "":
  77. // nothing
  78. default:
  79. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort order %v", getSortOrder))
  80. }
  81. sortByTarget := clientv3.SortByKey
  82. sortTarget := strings.ToUpper(getSortTarget)
  83. switch {
  84. case sortTarget == "CREATE":
  85. sortByTarget = clientv3.SortByCreateRevision
  86. case sortTarget == "KEY":
  87. sortByTarget = clientv3.SortByKey
  88. case sortTarget == "MODIFY":
  89. sortByTarget = clientv3.SortByModRevision
  90. case sortTarget == "VALUE":
  91. sortByTarget = clientv3.SortByValue
  92. case sortTarget == "VERSION":
  93. sortByTarget = clientv3.SortByVersion
  94. case sortTarget == "":
  95. // nothing
  96. default:
  97. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort target %v", getSortTarget))
  98. }
  99. opts = append(opts, clientv3.WithSort(sortByTarget, sortByOrder))
  100. if getPrefix {
  101. opts = append(opts, clientv3.WithPrefix())
  102. }
  103. if getFromKey {
  104. opts = append(opts, clientv3.WithFromKey())
  105. }
  106. return key, opts
  107. }