get_command.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "golang.org/x/net/context"
  21. )
  22. var (
  23. getConsistency string
  24. getLimit int64
  25. getSortOrder string
  26. getSortTarget string
  27. getPrefix bool
  28. getFromKey bool
  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. return cmd
  44. }
  45. // getCommandFunc executes the "get" command.
  46. func getCommandFunc(cmd *cobra.Command, args []string) {
  47. key, opts := getGetOp(cmd, args)
  48. resp, err := mustClientFromCmd(cmd).Get(context.TODO(), key, opts...)
  49. if err != nil {
  50. ExitWithError(ExitError, err)
  51. }
  52. display.Get(*resp)
  53. }
  54. func getGetOp(cmd *cobra.Command, args []string) (string, []clientv3.OpOption) {
  55. if len(args) == 0 {
  56. ExitWithError(ExitBadArgs, fmt.Errorf("range command needs arguments."))
  57. }
  58. if getPrefix && getFromKey {
  59. ExitWithError(ExitBadArgs, fmt.Errorf("`--prefix` and `--from-key` cannot be set at the same time, choose one."))
  60. }
  61. opts := []clientv3.OpOption{}
  62. switch getConsistency {
  63. case "s":
  64. opts = append(opts, clientv3.WithSerializable())
  65. case "l":
  66. default:
  67. ExitWithError(ExitBadFeature, fmt.Errorf("unknown consistency flag %q", getConsistency))
  68. }
  69. key := args[0]
  70. if len(args) > 1 {
  71. if getPrefix || getFromKey {
  72. ExitWithError(ExitBadArgs, fmt.Errorf("too many arguments, only accept one arguement when `--prefix` or `--from-key` is set."))
  73. }
  74. opts = append(opts, clientv3.WithRange(args[1]))
  75. }
  76. opts = append(opts, clientv3.WithLimit(getLimit))
  77. sortByOrder := clientv3.SortNone
  78. sortOrder := strings.ToUpper(getSortOrder)
  79. switch {
  80. case sortOrder == "ASCEND":
  81. sortByOrder = clientv3.SortAscend
  82. case sortOrder == "DESCEND":
  83. sortByOrder = clientv3.SortDescend
  84. case sortOrder == "":
  85. // nothing
  86. default:
  87. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort order %v", getSortOrder))
  88. }
  89. sortByTarget := clientv3.SortByKey
  90. sortTarget := strings.ToUpper(getSortTarget)
  91. switch {
  92. case sortTarget == "CREATE":
  93. sortByTarget = clientv3.SortByCreateRevision
  94. case sortTarget == "KEY":
  95. sortByTarget = clientv3.SortByKey
  96. case sortTarget == "MODIFY":
  97. sortByTarget = clientv3.SortByModRevision
  98. case sortTarget == "VALUE":
  99. sortByTarget = clientv3.SortByValue
  100. case sortTarget == "VERSION":
  101. sortByTarget = clientv3.SortByVersion
  102. case sortTarget == "":
  103. // nothing
  104. default:
  105. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort target %v", getSortTarget))
  106. }
  107. opts = append(opts, clientv3.WithSort(sortByTarget, sortByOrder))
  108. if getPrefix {
  109. opts = append(opts, clientv3.WithPrefix())
  110. }
  111. if getFromKey {
  112. opts = append(opts, clientv3.WithFromKey())
  113. }
  114. return key, opts
  115. }