get_command.go 4.7 KB

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