Merge pull request #4532 from xiang90/ctlv3

etcdctlv3: refactoring
This commit is contained in:
Xiang Li 2016-02-15 20:12:04 -08:00
commit 30e4d7d6aa
11 changed files with 47 additions and 30 deletions

View File

@ -43,7 +43,7 @@ func compactionCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitError, err)
}
c := mustClient(cmd)
c := mustClientFromCmd(cmd)
if cerr := clientv3.NewKV(c).Compact(context.TODO(), rev); cerr != nil {
ExitWithError(ExitError, cerr)
return

View File

@ -44,7 +44,7 @@ func deleteRangeCommandFunc(cmd *cobra.Command, args []string) {
}
req := &pb.DeleteRangeRequest{Key: key, RangeEnd: rangeEnd}
mustClient(cmd).KV.DeleteRange(context.Background(), req)
mustClientFromCmd(cmd).KV.DeleteRange(context.Background(), req)
if rangeEnd != nil {
fmt.Printf("range [%s, %s) is deleted\n", string(key), string(rangeEnd))

View File

@ -32,35 +32,52 @@ type GlobalFlags struct {
TLS transport.TLSInfo
}
func mustClient(cmd *cobra.Command) *clientv3.Client {
func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
endpoint, err := cmd.Flags().GetString("endpoint")
if err != nil {
ExitWithError(ExitError, err)
}
var cert, key, cacert string
if cert, err = cmd.Flags().GetString("cert"); err != nil {
ExitWithError(ExitBadArgs, err)
} else if cert == "" && cmd.Flags().Changed("cert") {
ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cert option"))
}
if key, err = cmd.Flags().GetString("key"); err != nil {
ExitWithError(ExitBadArgs, err)
} else if key == "" && cmd.Flags().Changed("key") {
ExitWithError(ExitBadArgs, errors.New("empty string is passed to --key option"))
}
if cacert, err = cmd.Flags().GetString("cacert"); err != nil {
ExitWithError(ExitBadArgs, err)
} else if cacert == "" && cmd.Flags().Changed("cacert") {
ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cacert option"))
}
return mustClient(endpoint, cert, key, cacert)
}
func mustClient(endpoint, cert, key, cacert string) *clientv3.Client {
// set tls if any one tls option set
var cfgtls *transport.TLSInfo
tls := transport.TLSInfo{}
var file string
if file, err = cmd.Flags().GetString("cert"); err == nil && file != "" {
tls.CertFile = file
if cert != "" {
tls.CertFile = cert
cfgtls = &tls
} else if cmd.Flags().Changed("cert") {
ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cert option"))
}
if file, err = cmd.Flags().GetString("key"); err == nil && file != "" {
tls.KeyFile = file
if key != "" {
tls.KeyFile = key
cfgtls = &tls
} else if cmd.Flags().Changed("key") {
ExitWithError(ExitBadArgs, errors.New("empty string is passed to --key option"))
}
if file, err = cmd.Flags().GetString("cacert"); err == nil && file != "" {
if cacert != "" {
tls.CAFile = file
cfgtls = &tls
} else if cmd.Flags().Changed("cacert") {
ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cacert option"))
}
cfg := clientv3.Config{

View File

@ -62,7 +62,7 @@ func leaseCreateCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadArgs, fmt.Errorf("bad TTL (%v)", err))
}
c := mustClient(cmd)
c := mustClientFromCmd(cmd)
l := clientv3.NewLease(c)
resp, err := l.Create(context.TODO(), ttl)
if err != nil {
@ -95,7 +95,7 @@ func leaseRevokeCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
}
c := mustClient(cmd)
c := mustClientFromCmd(cmd)
l := clientv3.NewLease(c)
_, err = l.Revoke(context.TODO(), lease.LeaseID(id))
if err != nil {
@ -128,7 +128,7 @@ func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadArgs, fmt.Errorf("bad lease ID arg (%v), expecting ID in Hex", err))
}
c := mustClient(cmd)
c := mustClientFromCmd(cmd)
l := clientv3.NewLease(c)
respc, kerr := l.KeepAlive(context.TODO(), lease.LeaseID(id))
if kerr != nil {

View File

@ -109,7 +109,7 @@ func memberAddCommandFunc(cmd *cobra.Command, args []string) {
urls := strings.Split(memberPeerURLs, ",")
req := &pb.MemberAddRequest{PeerURLs: urls}
resp, err := mustClient(cmd).Cluster.MemberAdd(context.TODO(), req)
resp, err := mustClientFromCmd(cmd).Cluster.MemberAdd(context.TODO(), req)
if err != nil {
ExitWithError(ExitError, err)
}
@ -129,7 +129,7 @@ func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
}
req := &pb.MemberRemoveRequest{ID: uint64(id)}
resp, err := mustClient(cmd).Cluster.MemberRemove(context.TODO(), req)
resp, err := mustClientFromCmd(cmd).Cluster.MemberRemove(context.TODO(), req)
if err != nil {
ExitWithError(ExitError, err)
}
@ -155,7 +155,7 @@ func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
urls := strings.Split(memberPeerURLs, ",")
req := &pb.MemberUpdateRequest{ID: uint64(id), PeerURLs: urls}
resp, err := mustClient(cmd).Cluster.MemberUpdate(context.TODO(), req)
resp, err := mustClientFromCmd(cmd).Cluster.MemberUpdate(context.TODO(), req)
if err != nil {
ExitWithError(ExitError, err)
}
@ -165,7 +165,7 @@ func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
// memberListCommandFunc executes the "member list" command.
func memberListCommandFunc(cmd *cobra.Command, args []string) {
resp, err := mustClient(cmd).Cluster.MemberList(context.TODO(), &pb.MemberListRequest{})
resp, err := mustClientFromCmd(cmd).Cluster.MemberList(context.TODO(), &pb.MemberListRequest{})
if err != nil {
ExitWithError(ExitError, err)
}

View File

@ -71,7 +71,7 @@ func putCommandFunc(cmd *cobra.Command, args []string) {
}
req := &pb.PutRequest{Key: key, Value: value, Lease: id}
_, err = mustClient(cmd).KV.Put(context.Background(), req)
_, err = mustClientFromCmd(cmd).KV.Put(context.Background(), req)
if err != nil {
ExitWithError(ExitError, err)
}

View File

@ -93,7 +93,7 @@ func rangeCommandFunc(cmd *cobra.Command, args []string) {
SortTarget: sortByTarget,
Limit: int64(rangeLimit),
}
resp, err := mustClient(cmd).KV.Range(context.Background(), req)
resp, err := mustClientFromCmd(cmd).KV.Range(context.Background(), req)
if err != nil {
ExitWithError(ExitError, err)
}

View File

@ -40,9 +40,9 @@ func NewSnapshotCommand() *cobra.Command {
func snapshotCommandFunc(cmd *cobra.Command, args []string) {
switch {
case len(args) == 0:
snapshotToStdout(mustClient(cmd))
snapshotToStdout(mustClientFromCmd(cmd))
case len(args) == 1:
snapshotToFile(mustClient(cmd), args[0])
snapshotToFile(mustClientFromCmd(cmd), args[0])
default:
err := fmt.Errorf("snapshot takes at most one argument")
ExitWithError(ExitBadArgs, err)

View File

@ -49,7 +49,7 @@ func txnCommandFunc(cmd *cobra.Command, args []string) {
next = next(txn, reader)
}
resp, err := mustClient(cmd).KV.Txn(context.Background(), txn)
resp, err := mustClientFromCmd(cmd).KV.Txn(context.Background(), txn)
if err != nil {
ExitWithError(ExitError, err)
}

View File

@ -38,7 +38,7 @@ func NewWatchCommand() *cobra.Command {
// watchCommandFunc executes the "watch" command.
func watchCommandFunc(cmd *cobra.Command, args []string) {
wStream, err := mustClient(cmd).Watch.Watch(context.TODO())
wStream, err := mustClientFromCmd(cmd).Watch.Watch(context.TODO())
if err != nil {
ExitWithError(ExitBadConnection, err)
}

View File

@ -43,9 +43,9 @@ var (
func init() {
rootCmd.PersistentFlags().StringVar(&globalFlags.Endpoints, "endpoint", "127.0.0.1:2378", "gRPC endpoint")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CertFile, "cert", "", "identify HTTPS client using this SSL certificate file")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.KeyFile, "key", "", "identify HTTPS client using this SSL key file")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CAFile, "cacert", "", "verify certificates of HTTPS-enabled servers using this CA bundle")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CertFile, "cert", "", "identify secure client using this TLS certificate file")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.KeyFile, "key", "", "identify secure client using this TLS key file")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CAFile, "cacert", "", "verify certificates of TLS-enabled secure servers using this CA bundle")
rootCmd.AddCommand(
command.NewRangeCommand(),