From d78cff2c4a8aa195b33dd15fc349019e3d0fba90 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 15 Feb 2016 14:44:25 -0800 Subject: [PATCH] etcdctlv3: separate out cmd parsing logic for creating client --- etcdctlv3/command/compaction_command.go | 2 +- etcdctlv3/command/delete_range_command.go | 2 +- etcdctlv3/command/global.go | 41 ++++++++++++++++------- etcdctlv3/command/lease_command.go | 6 ++-- etcdctlv3/command/member_command.go | 8 ++--- etcdctlv3/command/put_command.go | 2 +- etcdctlv3/command/range_command.go | 2 +- etcdctlv3/command/snapshot_command.go | 4 +-- etcdctlv3/command/txn_command.go | 2 +- etcdctlv3/command/watch_command.go | 2 +- 10 files changed, 44 insertions(+), 27 deletions(-) diff --git a/etcdctlv3/command/compaction_command.go b/etcdctlv3/command/compaction_command.go index 969922931..d68f3595b 100644 --- a/etcdctlv3/command/compaction_command.go +++ b/etcdctlv3/command/compaction_command.go @@ -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 diff --git a/etcdctlv3/command/delete_range_command.go b/etcdctlv3/command/delete_range_command.go index a5d0e8979..320f02ebb 100644 --- a/etcdctlv3/command/delete_range_command.go +++ b/etcdctlv3/command/delete_range_command.go @@ -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)) diff --git a/etcdctlv3/command/global.go b/etcdctlv3/command/global.go index 39c3dfaa1..6d0d814f5 100644 --- a/etcdctlv3/command/global.go +++ b/etcdctlv3/command/global.go @@ -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{ diff --git a/etcdctlv3/command/lease_command.go b/etcdctlv3/command/lease_command.go index 6e9650cac..97e861f73 100644 --- a/etcdctlv3/command/lease_command.go +++ b/etcdctlv3/command/lease_command.go @@ -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 { diff --git a/etcdctlv3/command/member_command.go b/etcdctlv3/command/member_command.go index e6ecaf02a..5540e67a2 100644 --- a/etcdctlv3/command/member_command.go +++ b/etcdctlv3/command/member_command.go @@ -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) } diff --git a/etcdctlv3/command/put_command.go b/etcdctlv3/command/put_command.go index 63435751c..5334a2918 100644 --- a/etcdctlv3/command/put_command.go +++ b/etcdctlv3/command/put_command.go @@ -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) } diff --git a/etcdctlv3/command/range_command.go b/etcdctlv3/command/range_command.go index 4788b2993..677131551 100644 --- a/etcdctlv3/command/range_command.go +++ b/etcdctlv3/command/range_command.go @@ -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) } diff --git a/etcdctlv3/command/snapshot_command.go b/etcdctlv3/command/snapshot_command.go index 247b338e8..4ea4446bb 100644 --- a/etcdctlv3/command/snapshot_command.go +++ b/etcdctlv3/command/snapshot_command.go @@ -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) diff --git a/etcdctlv3/command/txn_command.go b/etcdctlv3/command/txn_command.go index dec7b8d82..0360f2ece 100644 --- a/etcdctlv3/command/txn_command.go +++ b/etcdctlv3/command/txn_command.go @@ -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) } diff --git a/etcdctlv3/command/watch_command.go b/etcdctlv3/command/watch_command.go index b376e5cc8..6ff080ce6 100644 --- a/etcdctlv3/command/watch_command.go +++ b/etcdctlv3/command/watch_command.go @@ -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) }