get_command.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. getHex 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(&getHex, "hex", false, "print out key and value as hex encode string for text format")
  39. // TODO: add fromkey.
  40. // TODO: add prefix.
  41. // TODO: add consistency.
  42. return cmd
  43. }
  44. // getCommandFunc executes the "get" command.
  45. func getCommandFunc(cmd *cobra.Command, args []string) {
  46. if len(args) == 0 {
  47. ExitWithError(ExitBadArgs, fmt.Errorf("range command needs arguments."))
  48. }
  49. opts := []clientv3.OpOption{}
  50. key := args[0]
  51. if len(args) > 1 {
  52. opts = append(opts, clientv3.WithRange(args[1]))
  53. }
  54. opts = append(opts, clientv3.WithLimit(getLimit))
  55. sortByOrder := clientv3.SortNone
  56. sortOrder := strings.ToUpper(getSortOrder)
  57. switch {
  58. case sortOrder == "ASCEND":
  59. sortByOrder = clientv3.SortAscend
  60. case sortOrder == "DESCEND":
  61. sortByOrder = clientv3.SortDescend
  62. case sortOrder == "":
  63. // nothing
  64. default:
  65. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort order %v", getSortOrder))
  66. }
  67. sortByTarget := clientv3.SortByKey
  68. sortTarget := strings.ToUpper(getSortTarget)
  69. switch {
  70. case sortTarget == "CREATE":
  71. sortByTarget = clientv3.SortByCreatedRev
  72. case sortTarget == "KEY":
  73. sortByTarget = clientv3.SortByKey
  74. case sortTarget == "MODIFY":
  75. sortByTarget = clientv3.SortByModifiedRev
  76. case sortTarget == "VALUE":
  77. sortByTarget = clientv3.SortByValue
  78. case sortTarget == "VERSION":
  79. sortByTarget = clientv3.SortByVersion
  80. case sortTarget == "":
  81. // nothing
  82. default:
  83. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort target %v", getSortTarget))
  84. }
  85. opts = append(opts, clientv3.WithSort(sortByTarget, sortByOrder))
  86. c := mustClientFromCmd(cmd)
  87. kvapi := clientv3.NewKV(c)
  88. resp, err := kvapi.Get(context.TODO(), key, opts...)
  89. if err != nil {
  90. ExitWithError(ExitError, err)
  91. }
  92. for _, kv := range resp.Kvs {
  93. printKV(getHex, kv)
  94. }
  95. }