go.etcd.io/etcd@v3.3.27+incompatible/etcdctl/ctlv3/command/del_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  
    20  	"github.com/coreos/etcd/clientv3"
    21  	"github.com/spf13/cobra"
    22  )
    23  
    24  var (
    25  	delPrefix  bool
    26  	delPrevKV  bool
    27  	delFromKey bool
    28  )
    29  
    30  // NewDelCommand returns the cobra command for "del".
    31  func NewDelCommand() *cobra.Command {
    32  	cmd := &cobra.Command{
    33  		Use:   "del [options] <key> [range_end]",
    34  		Short: "Removes the specified key or range of keys [key, range_end)",
    35  		Run:   delCommandFunc,
    36  	}
    37  
    38  	cmd.Flags().BoolVar(&delPrefix, "prefix", false, "delete keys with matching prefix")
    39  	cmd.Flags().BoolVar(&delPrevKV, "prev-kv", false, "return deleted key-value pairs")
    40  	cmd.Flags().BoolVar(&delFromKey, "from-key", false, "delete keys that are greater than or equal to the given key using byte compare")
    41  	return cmd
    42  }
    43  
    44  // delCommandFunc executes the "del" command.
    45  func delCommandFunc(cmd *cobra.Command, args []string) {
    46  	key, opts := getDelOp(cmd, args)
    47  	ctx, cancel := commandCtx(cmd)
    48  	resp, err := mustClientFromCmd(cmd).Delete(ctx, key, opts...)
    49  	cancel()
    50  	if err != nil {
    51  		ExitWithError(ExitError, err)
    52  	}
    53  	display.Del(*resp)
    54  }
    55  
    56  func getDelOp(cmd *cobra.Command, args []string) (string, []clientv3.OpOption) {
    57  	if len(args) == 0 || len(args) > 2 {
    58  		ExitWithError(ExitBadArgs, fmt.Errorf("del command needs one argument as key and an optional argument as range_end."))
    59  	}
    60  
    61  	if delPrefix && delFromKey {
    62  		ExitWithError(ExitBadArgs, fmt.Errorf("`--prefix` and `--from-key` cannot be set at the same time, choose one."))
    63  	}
    64  
    65  	opts := []clientv3.OpOption{}
    66  	key := args[0]
    67  	if len(args) > 1 {
    68  		if delPrefix || delFromKey {
    69  			ExitWithError(ExitBadArgs, fmt.Errorf("too many arguments, only accept one argument when `--prefix` or `--from-key` is set."))
    70  		}
    71  		opts = append(opts, clientv3.WithRange(args[1]))
    72  	}
    73  
    74  	if delPrefix {
    75  		if len(key) == 0 {
    76  			key = "\x00"
    77  			opts = append(opts, clientv3.WithFromKey())
    78  		} else {
    79  			opts = append(opts, clientv3.WithPrefix())
    80  		}
    81  	}
    82  	if delPrevKV {
    83  		opts = append(opts, clientv3.WithPrevKV())
    84  	}
    85  
    86  	if delFromKey {
    87  		if len(key) == 0 {
    88  			key = "\x00"
    89  		}
    90  		opts = append(opts, clientv3.WithFromKey())
    91  	}
    92  
    93  	return key, opts
    94  }