get_command.go 3.9 KB

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