浏览代码

v3etcdctl: initial v3 ctl support

Xiang Li 10 年之前
父节点
当前提交
523567bcc7
共有 4 个文件被更改,包括 209 次插入0 次删除
  1. 61 0
      v3etcdctl/command/delete_range_command.go
  2. 53 0
      v3etcdctl/command/put_command.go
  3. 58 0
      v3etcdctl/command/range_command.go
  4. 37 0
      v3etcdctl/main.go

+ 61 - 0
v3etcdctl/command/delete_range_command.go

@@ -0,0 +1,61 @@
+// Copyright 2015 CoreOS, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package command
+
+import (
+	"fmt"
+
+	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
+	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
+	"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
+	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
+)
+
+// NewDeleteRangeCommand returns the CLI command for "deleteRange".
+func NewDeleteRangeCommand() cli.Command {
+	return cli.Command{
+		Name: "delete-range",
+		Action: func(c *cli.Context) {
+			deleteRangeCommandFunc(c)
+		},
+	}
+}
+
+// deleteRangeCommandFunc executes the "delegeRange" command.
+func deleteRangeCommandFunc(c *cli.Context) {
+	if len(c.Args()) == 0 {
+		panic("bad arg")
+	}
+
+	var rangeEnd []byte
+	key := []byte(c.Args()[0])
+	if len(c.Args()) > 1 {
+		rangeEnd = []byte(c.Args()[1])
+	}
+	conn, err := grpc.Dial("127.0.0.1:12379")
+	if err != nil {
+		panic(err)
+	}
+	etcd := pb.NewEtcdClient(conn)
+	req := &pb.DeleteRangeRequest{Key: key, RangeEnd: rangeEnd}
+
+	etcd.DeleteRange(context.Background(), req)
+
+	if rangeEnd != nil {
+		fmt.Printf("range [%s, %s) is deleted\n", string(key), string(rangeEnd))
+	} else {
+		fmt.Printf("key %s is deleted\n", string(key))
+	}
+}

+ 53 - 0
v3etcdctl/command/put_command.go

@@ -0,0 +1,53 @@
+// Copyright 2015 CoreOS, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package command
+
+import (
+	"fmt"
+
+	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
+	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
+	"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
+	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
+)
+
+// NewPutCommand returns the CLI command for "put".
+func NewPutCommand() cli.Command {
+	return cli.Command{
+		Name: "put",
+		Action: func(c *cli.Context) {
+			putCommandFunc(c)
+		},
+	}
+}
+
+// putCommandFunc executes the "put" command.
+func putCommandFunc(c *cli.Context) {
+	if len(c.Args()) != 2 {
+		panic("bad arg")
+	}
+
+	key := []byte(c.Args()[0])
+	value := []byte(c.Args()[1])
+	conn, err := grpc.Dial("127.0.0.1:12379")
+	if err != nil {
+		panic(err)
+	}
+	etcd := pb.NewEtcdClient(conn)
+	req := &pb.PutRequest{Key: key, Value: value}
+
+	etcd.Put(context.Background(), req)
+	fmt.Printf("%s %s\n", key, value)
+}

+ 58 - 0
v3etcdctl/command/range_command.go

@@ -0,0 +1,58 @@
+// Copyright 2015 CoreOS, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package command
+
+import (
+	"fmt"
+
+	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
+	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
+	"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
+	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
+)
+
+// NewRangeCommand returns the CLI command for "range".
+func NewRangeCommand() cli.Command {
+	return cli.Command{
+		Name: "range",
+		Action: func(c *cli.Context) {
+			rangeCommandFunc(c)
+		},
+	}
+}
+
+// rangeCommandFunc executes the "range" command.
+func rangeCommandFunc(c *cli.Context) {
+	if len(c.Args()) == 0 {
+		panic("bad arg")
+	}
+
+	var rangeEnd []byte
+	key := []byte(c.Args()[0])
+	if len(c.Args()) > 1 {
+		rangeEnd = []byte(c.Args()[1])
+	}
+	conn, err := grpc.Dial("127.0.0.1:12379")
+	if err != nil {
+		panic(err)
+	}
+	etcd := pb.NewEtcdClient(conn)
+	req := &pb.RangeRequest{Key: key, RangeEnd: rangeEnd}
+
+	resp, err := etcd.Range(context.Background(), req)
+	for _, kv := range resp.Kvs {
+		fmt.Printf("%s %s\n", string(kv.Key), string(kv.Value))
+	}
+}

+ 37 - 0
v3etcdctl/main.go

@@ -0,0 +1,37 @@
+// Copyright 2015 CoreOS, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+	"os"
+
+	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
+	"github.com/coreos/etcd/v3etcdctl/command"
+	"github.com/coreos/etcd/version"
+)
+
+func main() {
+	app := cli.NewApp()
+	app.Name = "v3etcdctl"
+	app.Version = version.Version
+	app.Usage = "A simple command line client for etcd3."
+	app.Commands = []cli.Command{
+		command.NewRangeCommand(),
+		command.NewPutCommand(),
+		command.NewDeleteRangeCommand(),
+	}
+
+	app.Run(os.Args)
+}