get_command.go 4.1 KB

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