go.etcd.io/etcd@v3.3.27+incompatible/etcdctl/ctlv2/command/rmdir_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  // NewRemoveDirCommand returns the CLI command for "rmdir".
    25  func NewRemoveDirCommand() cli.Command {
    26  	return cli.Command{
    27  		Name:      "rmdir",
    28  		Usage:     "removes the key if it is an empty directory or a key-value pair",
    29  		ArgsUsage: "<key>",
    30  		Action: func(c *cli.Context) error {
    31  			rmdirCommandFunc(c, mustNewKeyAPI(c))
    32  			return nil
    33  		},
    34  	}
    35  }
    36  
    37  // rmdirCommandFunc executes the "rmdir" command.
    38  func rmdirCommandFunc(c *cli.Context, ki client.KeysAPI) {
    39  	if len(c.Args()) == 0 {
    40  		handleError(c, ExitBadArgs, errors.New("key required"))
    41  	}
    42  	key := c.Args()[0]
    43  
    44  	ctx, cancel := contextWithTotalTimeout(c)
    45  	resp, err := ki.Delete(ctx, key, &client.DeleteOptions{Dir: true})
    46  	cancel()
    47  	if err != nil {
    48  		handleError(c, ExitServerError, err)
    49  	}
    50  
    51  	if !resp.Node.Dir || c.GlobalString("output") != "simple" {
    52  		printResponseKey(resp, c.GlobalString("output"))
    53  	}
    54  }