etcdctlv3: consolidate dial code; use clientv3

This commit is contained in:
Anthony Romano 2016-01-27 10:25:04 -08:00
parent 5ccf7f5151
commit 9a5a3ebc79
9 changed files with 36 additions and 163 deletions

View File

@ -20,7 +20,6 @@ import (
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" "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" pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
) )
@ -44,17 +43,6 @@ func compactionCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitError, err) ExitWithError(ExitError, err)
} }
endpoint, err := cmd.Flags().GetString("endpoint")
if err != nil {
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
kv := pb.NewKVClient(conn)
req := &pb.CompactionRequest{Revision: rev} req := &pb.CompactionRequest{Revision: rev}
mustClient(cmd).KV.Compact(context.Background(), req)
kv.Compact(context.Background(), req)
} }

View File

@ -19,7 +19,6 @@ import (
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" "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" pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
) )
@ -44,19 +43,8 @@ func deleteRangeCommandFunc(cmd *cobra.Command, args []string) {
rangeEnd = []byte(args[1]) rangeEnd = []byte(args[1])
} }
endpoint, err := cmd.Flags().GetString("endpoint")
if err != nil {
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
kv := pb.NewKVClient(conn)
req := &pb.DeleteRangeRequest{Key: key, RangeEnd: rangeEnd} req := &pb.DeleteRangeRequest{Key: key, RangeEnd: rangeEnd}
mustClient(cmd).KV.DeleteRange(context.Background(), req)
kv.DeleteRange(context.Background(), req)
if rangeEnd != nil { if rangeEnd != nil {
fmt.Printf("range [%s, %s) is deleted\n", string(key), string(rangeEnd)) fmt.Printf("range [%s, %s) is deleted\n", string(key), string(rangeEnd))

View File

@ -14,8 +14,25 @@
package command package command
import (
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/clientv3"
)
// GlobalFlags are flags that defined globally // GlobalFlags are flags that defined globally
// and are inherited to all sub-commands. // and are inherited to all sub-commands.
type GlobalFlags struct { type GlobalFlags struct {
Endpoints string Endpoints string
} }
func mustClient(cmd *cobra.Command) *clientv3.Client {
endpoint, err := cmd.Flags().GetString("endpoint")
if err != nil {
ExitWithError(ExitError, err)
}
client, err := clientv3.NewFromURL(endpoint)
if err != nil {
ExitWithError(ExitBadConnection, err)
}
return client
}

View File

@ -23,7 +23,6 @@ import (
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" "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" pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
) )
@ -64,19 +63,8 @@ func leaseCreateCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadArgs, fmt.Errorf("bad TTL (%v)", err)) ExitWithError(ExitBadArgs, fmt.Errorf("bad TTL (%v)", err))
} }
endpoint, err := cmd.Flags().GetString("endpoint")
if err != nil {
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
lease := pb.NewLeaseClient(conn)
req := &pb.LeaseCreateRequest{TTL: ttl} req := &pb.LeaseCreateRequest{TTL: ttl}
resp, err := lease.LeaseCreate(context.Background(), req) resp, err := mustClient(cmd).Lease.LeaseCreate(context.Background(), req)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "failed to create lease (%v)\n", err) fmt.Fprintf(os.Stderr, "failed to create lease (%v)\n", err)
return return
@ -107,19 +95,8 @@ func leaseRevokeCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err)) ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
} }
endpoint, err := cmd.Flags().GetString("endpoint")
if err != nil {
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
lease := pb.NewLeaseClient(conn)
req := &pb.LeaseRevokeRequest{ID: id} req := &pb.LeaseRevokeRequest{ID: id}
_, err = lease.LeaseRevoke(context.Background(), req) _, err = mustClient(cmd).Lease.LeaseRevoke(context.Background(), req)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "failed to revoke lease (%v)\n", err) fmt.Fprintf(os.Stderr, "failed to revoke lease (%v)\n", err)
return return
@ -150,17 +127,7 @@ func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err)) ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
} }
endpoint, err := cmd.Flags().GetString("endpoint") kStream, err := mustClient(cmd).Lease.LeaseKeepAlive(context.TODO())
if err != nil {
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
lease := pb.NewLeaseClient(conn)
kStream, err := lease.LeaseKeepAlive(context.TODO())
if err != nil { if err != nil {
ExitWithError(ExitBadConnection, err) ExitWithError(ExitBadConnection, err)
} }

View File

@ -21,7 +21,6 @@ import (
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" "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" pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
) )
@ -109,18 +108,8 @@ func memberAddCommandFunc(cmd *cobra.Command, args []string) {
urls := strings.Split(memberPeerURLs, ",") urls := strings.Split(memberPeerURLs, ",")
endpoint, err := cmd.Flags().GetString("endpoint") req := &pb.MemberAddRequest{PeerURLs: urls}
if err != nil { resp, err := mustClient(cmd).Cluster.MemberAdd(context.TODO(), req)
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
mc := pb.NewClusterClient(conn)
resp, err := mc.MemberAdd(context.TODO(), &pb.MemberAddRequest{PeerURLs: urls})
if err != nil { if err != nil {
ExitWithError(ExitError, err) ExitWithError(ExitError, err)
} }
@ -139,18 +128,8 @@ func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err)) ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
} }
endpoint, err := cmd.Flags().GetString("endpoint") req := &pb.MemberRemoveRequest{ID: uint64(id)}
if err != nil { resp, err := mustClient(cmd).Cluster.MemberRemove(context.TODO(), req)
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
mc := pb.NewClusterClient(conn)
resp, err := mc.MemberRemove(context.TODO(), &pb.MemberRemoveRequest{ID: uint64(id)})
if err != nil { if err != nil {
ExitWithError(ExitError, err) ExitWithError(ExitError, err)
} }
@ -175,18 +154,8 @@ func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
urls := strings.Split(memberPeerURLs, ",") urls := strings.Split(memberPeerURLs, ",")
endpoint, err := cmd.Flags().GetString("endpoint") req := &pb.MemberUpdateRequest{ID: uint64(id), PeerURLs: urls}
if err != nil { resp, err := mustClient(cmd).Cluster.MemberUpdate(context.TODO(), req)
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
mc := pb.NewClusterClient(conn)
resp, err := mc.MemberUpdate(context.TODO(), &pb.MemberUpdateRequest{ID: uint64(id), PeerURLs: urls})
if err != nil { if err != nil {
ExitWithError(ExitError, err) ExitWithError(ExitError, err)
} }
@ -196,18 +165,7 @@ func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
// memberListCommandFunc executes the "member list" command. // memberListCommandFunc executes the "member list" command.
func memberListCommandFunc(cmd *cobra.Command, args []string) { func memberListCommandFunc(cmd *cobra.Command, args []string) {
endpoint, err := cmd.Flags().GetString("endpoint") resp, err := mustClient(cmd).Cluster.MemberList(context.TODO(), &pb.MemberListRequest{})
if err != nil {
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
mc := pb.NewClusterClient(conn)
resp, err := mc.MemberList(context.TODO(), &pb.MemberListRequest{})
if err != nil { if err != nil {
ExitWithError(ExitError, err) ExitWithError(ExitError, err)
} }

View File

@ -20,7 +20,6 @@ import (
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" "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" pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
) )
@ -62,18 +61,7 @@ func putCommandFunc(cmd *cobra.Command, args []string) {
key := []byte(args[0]) key := []byte(args[0])
value := []byte(args[1]) value := []byte(args[1])
endpoint, err := cmd.Flags().GetString("endpoint")
if err != nil {
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
kv := pb.NewKVClient(conn)
req := &pb.PutRequest{Key: key, Value: value, Lease: id} req := &pb.PutRequest{Key: key, Value: value, Lease: id}
mustClient(cmd).KV.Put(context.Background(), req)
kv.Put(context.Background(), req)
fmt.Printf("%s %s\n", key, value) fmt.Printf("%s %s\n", key, value)
} }

View File

@ -20,7 +20,6 @@ import (
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" "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" pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
) )
@ -55,11 +54,6 @@ func rangeCommandFunc(cmd *cobra.Command, args []string) {
rangeEnd = []byte(args[1]) rangeEnd = []byte(args[1])
} }
endpoint, err := cmd.Flags().GetString("endpoint")
if err != nil {
ExitWithError(ExitError, err)
}
sortByOrder := pb.RangeRequest_NONE sortByOrder := pb.RangeRequest_NONE
sortOrder := strings.ToUpper(rangeSortOrder) sortOrder := strings.ToUpper(rangeSortOrder)
switch { switch {
@ -92,12 +86,6 @@ func rangeCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadFeature, fmt.Errorf("bad sort target %v", rangeSortTarget)) ExitWithError(ExitBadFeature, fmt.Errorf("bad sort target %v", rangeSortTarget))
} }
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
kv := pb.NewKVClient(conn)
req := &pb.RangeRequest{ req := &pb.RangeRequest{
Key: key, Key: key,
RangeEnd: rangeEnd, RangeEnd: rangeEnd,
@ -105,7 +93,10 @@ func rangeCommandFunc(cmd *cobra.Command, args []string) {
SortTarget: sortByTarget, SortTarget: sortByTarget,
Limit: int64(rangeLimit), Limit: int64(rangeLimit),
} }
resp, err := kv.Range(context.Background(), req) resp, err := mustClient(cmd).KV.Range(context.Background(), req)
if err != nil {
ExitWithError(ExitError, err)
}
for _, kv := range resp.Kvs { for _, kv := range resp.Kvs {
fmt.Printf("%s %s\n", string(kv.Key), string(kv.Value)) fmt.Printf("%s %s\n", string(kv.Key), string(kv.Value))
} }

View File

@ -23,7 +23,6 @@ import (
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" "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" pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
) )
@ -50,18 +49,7 @@ func txnCommandFunc(cmd *cobra.Command, args []string) {
next = next(txn, reader) next = next(txn, reader)
} }
endpoint, err := cmd.Flags().GetString("endpoint") resp, err := mustClient(cmd).KV.Txn(context.Background(), txn)
if err != nil {
ExitWithError(ExitError, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
kv := pb.NewKVClient(conn)
resp, err := kv.Txn(context.Background(), txn)
if err != nil { if err != nil {
ExitWithError(ExitError, err) ExitWithError(ExitError, err)
} }

View File

@ -24,7 +24,6 @@ import (
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" "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" pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
) )
@ -39,18 +38,7 @@ func NewWatchCommand() *cobra.Command {
// watchCommandFunc executes the "watch" command. // watchCommandFunc executes the "watch" command.
func watchCommandFunc(cmd *cobra.Command, args []string) { func watchCommandFunc(cmd *cobra.Command, args []string) {
endpoint, err := cmd.Flags().GetString("endpoint") wStream, err := mustClient(cmd).Watch.Watch(context.TODO())
if err != nil {
ExitWithError(ExitInvalidInput, err)
}
// TODO: enable grpc.WithTransportCredentials(creds)
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
ExitWithError(ExitBadConnection, err)
}
wAPI := pb.NewWatchClient(conn)
wStream, err := wAPI.Watch(context.TODO())
if err != nil { if err != nil {
ExitWithError(ExitBadConnection, err) ExitWithError(ExitBadConnection, err)
} }