del_command.go 2.6 KB

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