get_command.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "encoding/hex"
  17. "fmt"
  18. "strings"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  21. "github.com/coreos/etcd/clientv3"
  22. )
  23. var (
  24. getLimit int64
  25. getSortOrder string
  26. getSortTarget string
  27. getHex 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(&getSortOrder, "order", "", "order of results; ASCEND or DESCEND")
  37. cmd.Flags().StringVar(&getSortTarget, "sort-by", "", "sort target; CREATE, KEY, MODIFY, VALUE, or VERSION")
  38. cmd.Flags().Int64Var(&getLimit, "limit", 0, "maximum number of results")
  39. cmd.Flags().BoolVar(&getHex, "hex", false, "print out key and value as hex encode string for text format")
  40. // TODO: add fromkey.
  41. // TODO: add prefix.
  42. // TODO: add consistency.
  43. return cmd
  44. }
  45. // getCommandFunc executes the "get" command.
  46. func getCommandFunc(cmd *cobra.Command, args []string) {
  47. if len(args) == 0 {
  48. ExitWithError(ExitBadArgs, fmt.Errorf("range command needs arguments."))
  49. }
  50. opts := []clientv3.OpOption{}
  51. key := args[0]
  52. if len(args) > 1 {
  53. opts = append(opts, clientv3.WithRange(args[1]))
  54. }
  55. opts = append(opts, clientv3.WithLimit(getLimit))
  56. sortByOrder := clientv3.SortNone
  57. sortOrder := strings.ToUpper(getSortOrder)
  58. switch {
  59. case sortOrder == "ASCEND":
  60. sortByOrder = clientv3.SortAscend
  61. case sortOrder == "DESCEND":
  62. sortByOrder = clientv3.SortDescend
  63. case sortOrder == "":
  64. // nothing
  65. default:
  66. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort order %v", getSortOrder))
  67. }
  68. sortByTarget := clientv3.SortByKey
  69. sortTarget := strings.ToUpper(getSortTarget)
  70. switch {
  71. case sortTarget == "CREATE":
  72. sortByTarget = clientv3.SortByCreatedRev
  73. case sortTarget == "KEY":
  74. sortByTarget = clientv3.SortByKey
  75. case sortTarget == "MODIFY":
  76. sortByTarget = clientv3.SortByModifiedRev
  77. case sortTarget == "VALUE":
  78. sortByTarget = clientv3.SortByValue
  79. case sortTarget == "VERSION":
  80. sortByTarget = clientv3.SortByVersion
  81. case sortTarget == "":
  82. // nothing
  83. default:
  84. ExitWithError(ExitBadFeature, fmt.Errorf("bad sort target %v", getSortTarget))
  85. }
  86. opts = append(opts, clientv3.WithSort(sortByTarget, sortByOrder))
  87. c := mustClientFromCmd(cmd)
  88. kvapi := clientv3.NewKV(c)
  89. resp, err := kvapi.Get(context.TODO(), key, opts...)
  90. if err != nil {
  91. ExitWithError(ExitError, err)
  92. }
  93. for _, kv := range resp.Kvs {
  94. k, v := string(kv.Key), string(kv.Value)
  95. if getHex {
  96. k = addHexPrefix(hex.EncodeToString(kv.Key))
  97. v = addHexPrefix(hex.EncodeToString(kv.Value))
  98. }
  99. fmt.Printf("%s\r\n%s\r\n", k, v)
  100. }
  101. }
  102. func addHexPrefix(s string) string {
  103. ns := make([]byte, len(s)*2)
  104. for i := 0; i < len(s); i += 2 {
  105. ns[i*2] = '\\'
  106. ns[i*2+1] = 'x'
  107. ns[i*2+2] = s[i]
  108. ns[i*2+3] = s[i+1]
  109. }
  110. return string(ns)
  111. }