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