put_command.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 The etcd Authors
  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. "os"
  18. "strconv"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/spf13/cobra"
  21. )
  22. var (
  23. leaseStr string
  24. putPrevKV bool
  25. putIgnoreVal bool
  26. putIgnoreLease bool
  27. )
  28. // NewPutCommand returns the cobra command for "put".
  29. func NewPutCommand() *cobra.Command {
  30. cmd := &cobra.Command{
  31. Use: "put [options] <key> <value> (<value> can also be given from stdin)",
  32. Short: "Puts the given key into the store",
  33. Long: `
  34. Puts the given key into the store.
  35. When <value> begins with '-', <value> is interpreted as a flag.
  36. Insert '--' for workaround:
  37. $ put <key> -- <value>
  38. $ put -- <key> <value>
  39. If <value> isn't given as a command line argument and '--ignore-value' is not specified,
  40. this command tries to read the value from standard input.
  41. If <lease> isn't given as a command line argument and '--ignore-lease' is not specified,
  42. this command tries to read the value from standard input.
  43. For example,
  44. $ cat file | put <key>
  45. will store the content of the file to <key>.
  46. `,
  47. Run: putCommandFunc,
  48. }
  49. cmd.Flags().StringVar(&leaseStr, "lease", "0", "lease ID (in hexadecimal) to attach to the key")
  50. cmd.Flags().BoolVar(&putPrevKV, "prev-kv", false, "return the previous key-value pair before modification")
  51. cmd.Flags().BoolVar(&putIgnoreVal, "ignore-value", false, "updates the key using its current value")
  52. cmd.Flags().BoolVar(&putIgnoreLease, "ignore-lease", false, "updates the key using its current lease")
  53. return cmd
  54. }
  55. // putCommandFunc executes the "put" command.
  56. func putCommandFunc(cmd *cobra.Command, args []string) {
  57. key, value, opts := getPutOp(cmd, args)
  58. ctx, cancel := commandCtx(cmd)
  59. resp, err := mustClientFromCmd(cmd).Put(ctx, key, value, opts...)
  60. cancel()
  61. if err != nil {
  62. ExitWithError(ExitError, err)
  63. }
  64. display.Put(*resp)
  65. }
  66. func getPutOp(cmd *cobra.Command, args []string) (string, string, []clientv3.OpOption) {
  67. if len(args) == 0 {
  68. ExitWithError(ExitBadArgs, fmt.Errorf("put command needs 1 argument and input from stdin or 2 arguments."))
  69. }
  70. key := args[0]
  71. if putIgnoreVal && len(args) > 1 {
  72. ExitWithError(ExitBadArgs, fmt.Errorf("put command needs only 1 argument when 'ignore-value' is set."))
  73. }
  74. var value string
  75. var err error
  76. if !putIgnoreVal {
  77. value, err = argOrStdin(args, os.Stdin, 1)
  78. if err != nil {
  79. ExitWithError(ExitBadArgs, fmt.Errorf("put command needs 1 argument and input from stdin or 2 arguments."))
  80. }
  81. }
  82. id, err := strconv.ParseInt(leaseStr, 16, 64)
  83. if err != nil {
  84. ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID (%v), expecting ID in Hex", err))
  85. }
  86. opts := []clientv3.OpOption{}
  87. if id != 0 {
  88. opts = append(opts, clientv3.WithLease(clientv3.LeaseID(id)))
  89. }
  90. if putPrevKV {
  91. opts = append(opts, clientv3.WithPrevKV())
  92. }
  93. if putIgnoreVal {
  94. opts = append(opts, clientv3.WithIgnoreValue())
  95. }
  96. if putIgnoreLease {
  97. opts = append(opts, clientv3.WithIgnoreLease())
  98. }
  99. return key, value, opts
  100. }