get_command.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/Godeps/_workspace/src/github.com/spf13/cobra"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  20. "github.com/coreos/etcd/clientv3"
  21. )
  22. var (
  23. getLimit int64
  24. getSortOrder string
  25. getSortTarget string
  26. getPrefix bool
  27. )
  28. // NewGetCommand returns the cobra command for "get".
  29. func NewGetCommand() *cobra.Command {
  30. cmd := &cobra.Command{
  31. Use: "get [options] <key> [range_end]",
  32. Short: "Get gets the key or a range of keys.",
  33. Run: getCommandFunc,
  34. }
  35. cmd.Flags().StringVar(&getSortOrder, "order", "", "order of results; ASCEND or DESCEND")
  36. cmd.Flags().StringVar(&getSortTarget, "sort-by", "", "sort target; CREATE, KEY, MODIFY, VALUE, or VERSION")
  37. cmd.Flags().Int64Var(&getLimit, "limit", 0, "maximum number of results")
  38. cmd.Flags().BoolVar(&getPrefix, "prefix", false, "enable keys with matching prefix")
  39. // TODO: add fromkey.
  40. // TODO: add consistency.
  41. return cmd
  42. }
  43. // getCommandFunc executes the "get" command.
  44. func getCommandFunc(cmd *cobra.Command, args []string) {
  45. key, opts := getGetOp(cmd, args)
  46. resp, err := mustClientFromCmd(cmd).Get(context.TODO(), key, opts...)
  47. if err != nil {
  48. ExitWithError(ExitError, err)
  49. }
  50. display.Get(*resp)
  51. }
  52. func getGetOp(cmd *cobra.Command, args []string) (string, []clientv3.OpOption) {
  53. if len(args) == 0 {
  54. ExitWithError(ExitBadArgs, fmt.Errorf("range command needs arguments."))
  55. }
  56. opts := []clientv3.OpOption{}
  57. key := args[0]
  58. if len(args) > 1 {
  59. if getPrefix {
  60. ExitWithError(ExitBadArgs, fmt.Errorf("too many arguments for range with prefix, only accept one."))
  61. }
  62. opts = append(opts, clientv3.WithRange(args[1]))
  63. }
  64. opts = append(opts, clientv3.WithLimit(getLimit))
  65. sortByOrder := clientv3.SortNone
  66. sortOrder := strings.ToUpper(getSortOrder)
  67. switch {
  68. case sortOrder == "ASCEND":
  69. sortByOrder = clientv3.SortAscend
  70. case sortOrder == "DESCEND":
  71. sortByOrder = clientv3.SortDescend
  72. case sortOrder == "":
  73. // nothing
  74. default:
  75. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort order %v", getSortOrder))
  76. }
  77. sortByTarget := clientv3.SortByKey
  78. sortTarget := strings.ToUpper(getSortTarget)
  79. switch {
  80. case sortTarget == "CREATE":
  81. sortByTarget = clientv3.SortByCreatedRev
  82. case sortTarget == "KEY":
  83. sortByTarget = clientv3.SortByKey
  84. case sortTarget == "MODIFY":
  85. sortByTarget = clientv3.SortByModifiedRev
  86. case sortTarget == "VALUE":
  87. sortByTarget = clientv3.SortByValue
  88. case sortTarget == "VERSION":
  89. sortByTarget = clientv3.SortByVersion
  90. case sortTarget == "":
  91. // nothing
  92. default:
  93. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort target %v", getSortTarget))
  94. }
  95. opts = append(opts, clientv3.WithSort(sortByTarget, sortByOrder))
  96. if getPrefix {
  97. opts = append(opts, clientv3.WithPrefix())
  98. }
  99. return key, opts
  100. }