go.etcd.io/etcd@v3.3.27+incompatible/etcdctl/ctlv2/command/update_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 "errors" 19 "os" 20 "time" 21 22 "github.com/coreos/etcd/client" 23 "github.com/urfave/cli" 24 ) 25 26 // NewUpdateCommand returns the CLI command for "update". 27 func NewUpdateCommand() cli.Command { 28 return cli.Command{ 29 Name: "update", 30 Usage: "update an existing key with a given value", 31 ArgsUsage: "<key> <value>", 32 Flags: []cli.Flag{ 33 cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live in seconds"}, 34 }, 35 Action: func(c *cli.Context) error { 36 updateCommandFunc(c, mustNewKeyAPI(c)) 37 return nil 38 }, 39 } 40 } 41 42 // updateCommandFunc executes the "update" command. 43 func updateCommandFunc(c *cli.Context, ki client.KeysAPI) { 44 if len(c.Args()) == 0 { 45 handleError(c, ExitBadArgs, errors.New("key required")) 46 } 47 key := c.Args()[0] 48 value, err := argOrStdin(c.Args(), os.Stdin, 1) 49 if err != nil { 50 handleError(c, ExitBadArgs, errors.New("value required")) 51 } 52 53 ttl := c.Int("ttl") 54 55 ctx, cancel := contextWithTotalTimeout(c) 56 resp, err := ki.Set(ctx, key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevExist: client.PrevExist}) 57 cancel() 58 if err != nil { 59 handleError(c, ExitServerError, err) 60 } 61 62 printResponseKey(resp, c.GlobalString("output")) 63 }