go.etcd.io/etcd@v3.3.27+incompatible/etcdctl/ctlv2/command/rm_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 20 "github.com/coreos/etcd/client" 21 "github.com/urfave/cli" 22 ) 23 24 // NewRemoveCommand returns the CLI command for "rm". 25 func NewRemoveCommand() cli.Command { 26 return cli.Command{ 27 Name: "rm", 28 Usage: "remove a key or a directory", 29 ArgsUsage: "<key>", 30 Flags: []cli.Flag{ 31 cli.BoolFlag{Name: "dir", Usage: "removes the key if it is an empty directory or a key-value pair"}, 32 cli.BoolFlag{Name: "recursive, r", Usage: "removes the key and all child keys(if it is a directory)"}, 33 cli.StringFlag{Name: "with-value", Value: "", Usage: "previous value"}, 34 cli.IntFlag{Name: "with-index", Value: 0, Usage: "previous index"}, 35 }, 36 Action: func(c *cli.Context) error { 37 rmCommandFunc(c, mustNewKeyAPI(c)) 38 return nil 39 }, 40 } 41 } 42 43 // rmCommandFunc executes the "rm" command. 44 func rmCommandFunc(c *cli.Context, ki client.KeysAPI) { 45 if len(c.Args()) == 0 { 46 handleError(c, ExitBadArgs, errors.New("key required")) 47 } 48 key := c.Args()[0] 49 recursive := c.Bool("recursive") 50 dir := c.Bool("dir") 51 prevValue := c.String("with-value") 52 prevIndex := c.Int("with-index") 53 54 ctx, cancel := contextWithTotalTimeout(c) 55 resp, err := ki.Delete(ctx, key, &client.DeleteOptions{PrevIndex: uint64(prevIndex), PrevValue: prevValue, Dir: dir, Recursive: recursive}) 56 cancel() 57 if err != nil { 58 handleError(c, ExitServerError, err) 59 } 60 if !resp.Node.Dir || c.GlobalString("output") != "simple" { 61 printResponseKey(resp, c.GlobalString("output")) 62 } 63 }