get_command.go 3.1 KB

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