delete_range_command.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2015 CoreOS, Inc.
  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/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. )
  22. // NewDeleteRangeCommand returns the CLI command for "deleteRange".
  23. func NewDeleteRangeCommand() cli.Command {
  24. return cli.Command{
  25. Name: "delete-range",
  26. Action: func(c *cli.Context) {
  27. deleteRangeCommandFunc(c)
  28. },
  29. }
  30. }
  31. // deleteRangeCommandFunc executes the "delegeRange" command.
  32. func deleteRangeCommandFunc(c *cli.Context) {
  33. if len(c.Args()) == 0 {
  34. panic("bad arg")
  35. }
  36. var rangeEnd []byte
  37. key := []byte(c.Args()[0])
  38. if len(c.Args()) > 1 {
  39. rangeEnd = []byte(c.Args()[1])
  40. }
  41. conn, err := grpc.Dial("127.0.0.1:12379")
  42. if err != nil {
  43. panic(err)
  44. }
  45. etcd := pb.NewEtcdClient(conn)
  46. req := &pb.DeleteRangeRequest{Key: key, RangeEnd: rangeEnd}
  47. etcd.DeleteRange(context.Background(), req)
  48. if rangeEnd != nil {
  49. fmt.Printf("range [%s, %s) is deleted\n", string(key), string(rangeEnd))
  50. } else {
  51. fmt.Printf("key %s is deleted\n", string(key))
  52. }
  53. }