get_command.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2015 The etcd Authors
  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/clientv3"
  19. "github.com/spf13/cobra"
  20. )
  21. var (
  22. getConsistency string
  23. getLimit int64
  24. getSortOrder string
  25. getSortTarget string
  26. getPrefix bool
  27. getFromKey bool
  28. getRev int64
  29. getKeysOnly bool
  30. )
  31. // NewGetCommand returns the cobra command for "get".
  32. func NewGetCommand() *cobra.Command {
  33. cmd := &cobra.Command{
  34. Use: "get [options] <key> [range_end]",
  35. Short: "Get gets the key or a range of keys.",
  36. Run: getCommandFunc,
  37. }
  38. cmd.Flags().StringVar(&getConsistency, "consistency", "l", "Linearizable(l) or Serializable(s)")
  39. cmd.Flags().StringVar(&getSortOrder, "order", "", "order of results; ASCEND or DESCEND")
  40. cmd.Flags().StringVar(&getSortTarget, "sort-by", "", "sort target; CREATE, KEY, MODIFY, VALUE, or VERSION")
  41. cmd.Flags().Int64Var(&getLimit, "limit", 0, "maximum number of results")
  42. cmd.Flags().BoolVar(&getPrefix, "prefix", false, "get keys with matching prefix")
  43. cmd.Flags().BoolVar(&getFromKey, "from-key", false, "get keys that are greater than or equal to the given key")
  44. cmd.Flags().Int64Var(&getRev, "rev", 0, "specify the kv revision")
  45. cmd.Flags().BoolVar(&getKeysOnly, "keys-only", false, "get only the keys")
  46. return cmd
  47. }
  48. // getCommandFunc executes the "get" command.
  49. func getCommandFunc(cmd *cobra.Command, args []string) {
  50. key, opts := getGetOp(cmd, args)
  51. ctx, cancel := commandCtx(cmd)
  52. resp, err := mustClientFromCmd(cmd).Get(ctx, key, opts...)
  53. cancel()
  54. if err != nil {
  55. ExitWithError(ExitError, err)
  56. }
  57. display.Get(*resp)
  58. }
  59. func getGetOp(cmd *cobra.Command, args []string) (string, []clientv3.OpOption) {
  60. if len(args) == 0 {
  61. ExitWithError(ExitBadArgs, fmt.Errorf("range command needs arguments."))
  62. }
  63. if getPrefix && getFromKey {
  64. ExitWithError(ExitBadArgs, fmt.Errorf("`--prefix` and `--from-key` cannot be set at the same time, choose one."))
  65. }
  66. opts := []clientv3.OpOption{}
  67. switch getConsistency {
  68. case "s":
  69. opts = append(opts, clientv3.WithSerializable())
  70. case "l":
  71. default:
  72. ExitWithError(ExitBadFeature, fmt.Errorf("unknown consistency flag %q", getConsistency))
  73. }
  74. key := args[0]
  75. if len(args) > 1 {
  76. if getPrefix || getFromKey {
  77. ExitWithError(ExitBadArgs, fmt.Errorf("too many arguments, only accept one arguement when `--prefix` or `--from-key` is set."))
  78. }
  79. opts = append(opts, clientv3.WithRange(args[1]))
  80. }
  81. opts = append(opts, clientv3.WithLimit(getLimit))
  82. if getRev > 0 {
  83. opts = append(opts, clientv3.WithRev(getRev))
  84. }
  85. sortByOrder := clientv3.SortNone
  86. sortOrder := strings.ToUpper(getSortOrder)
  87. switch {
  88. case sortOrder == "ASCEND":
  89. sortByOrder = clientv3.SortAscend
  90. case sortOrder == "DESCEND":
  91. sortByOrder = clientv3.SortDescend
  92. case sortOrder == "":
  93. // nothing
  94. default:
  95. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort order %v", getSortOrder))
  96. }
  97. sortByTarget := clientv3.SortByKey
  98. sortTarget := strings.ToUpper(getSortTarget)
  99. switch {
  100. case sortTarget == "CREATE":
  101. sortByTarget = clientv3.SortByCreateRevision
  102. case sortTarget == "KEY":
  103. sortByTarget = clientv3.SortByKey
  104. case sortTarget == "MODIFY":
  105. sortByTarget = clientv3.SortByModRevision
  106. case sortTarget == "VALUE":
  107. sortByTarget = clientv3.SortByValue
  108. case sortTarget == "VERSION":
  109. sortByTarget = clientv3.SortByVersion
  110. case sortTarget == "":
  111. // nothing
  112. default:
  113. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort target %v", getSortTarget))
  114. }
  115. opts = append(opts, clientv3.WithSort(sortByTarget, sortByOrder))
  116. if getPrefix {
  117. if len(key) == 0 {
  118. key = "\x00"
  119. opts = append(opts, clientv3.WithFromKey())
  120. } else {
  121. opts = append(opts, clientv3.WithPrefix())
  122. }
  123. }
  124. if getFromKey {
  125. if len(key) == 0 {
  126. key = "\x00"
  127. }
  128. opts = append(opts, clientv3.WithFromKey())
  129. }
  130. if getKeysOnly {
  131. opts = append(opts, clientv3.WithKeysOnly())
  132. }
  133. return key, opts
  134. }