diff --git a/etcdctl/ctlv3/command/lease_command.go b/etcdctl/ctlv3/command/lease_command.go index d0649775f03..0afb3d69c7c 100644 --- a/etcdctl/ctlv3/command/lease_command.go +++ b/etcdctl/ctlv3/command/lease_command.go @@ -67,7 +67,7 @@ func leaseGrantCommandFunc(cmd *cobra.Command, args []string) { if err != nil { ExitWithError(ExitError, fmt.Errorf("failed to grant lease (%v)\n", err)) } - fmt.Printf("lease %016x granted with TTL(%ds)\n", resp.ID, resp.TTL) + display.Grant(*resp) } // NewLeaseRevokeCommand returns the cobra command for "lease revoke". @@ -90,12 +90,12 @@ func leaseRevokeCommandFunc(cmd *cobra.Command, args []string) { id := leaseFromArgs(args[0]) ctx, cancel := commandCtx(cmd) - _, err := mustClientFromCmd(cmd).Revoke(ctx, id) + resp, err := mustClientFromCmd(cmd).Revoke(ctx, id) cancel() if err != nil { ExitWithError(ExitError, fmt.Errorf("failed to revoke lease (%v)\n", err)) } - fmt.Printf("lease %016x revoked\n", id) + display.Revoke(id, *resp) } var timeToLiveKeys bool @@ -154,9 +154,12 @@ func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) { } for resp := range respc { - fmt.Printf("lease %016x keepalived with TTL(%d)\n", resp.ID, resp.TTL) + display.KeepAlive(*resp) + } + + if _, ok := (display).(*simplePrinter); ok { + fmt.Printf("lease %016x expired or revoked.\n", id) } - fmt.Printf("lease %016x expired or revoked.\n", id) } func leaseFromArgs(arg string) v3.LeaseID { diff --git a/etcdctl/ctlv3/command/printer.go b/etcdctl/ctlv3/command/printer.go index 6df3bfe5c2a..9926030471d 100644 --- a/etcdctl/ctlv3/command/printer.go +++ b/etcdctl/ctlv3/command/printer.go @@ -32,6 +32,9 @@ type printer interface { Txn(v3.TxnResponse) Watch(v3.WatchResponse) + Grant(r v3.LeaseGrantResponse) + Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse) + KeepAlive(r v3.LeaseKeepAliveResponse) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) MemberAdd(v3.MemberAddResponse) @@ -81,13 +84,18 @@ type printerRPC struct { p func(interface{}) } -func (p *printerRPC) Del(r v3.DeleteResponse) { p.p((*pb.DeleteRangeResponse)(&r)) } -func (p *printerRPC) Get(r v3.GetResponse) { p.p((*pb.RangeResponse)(&r)) } -func (p *printerRPC) Put(r v3.PutResponse) { p.p((*pb.PutResponse)(&r)) } -func (p *printerRPC) Txn(r v3.TxnResponse) { p.p((*pb.TxnResponse)(&r)) } -func (p *printerRPC) Watch(r v3.WatchResponse) { p.p(&r) } +func (p *printerRPC) Del(r v3.DeleteResponse) { p.p((*pb.DeleteRangeResponse)(&r)) } +func (p *printerRPC) Get(r v3.GetResponse) { p.p((*pb.RangeResponse)(&r)) } +func (p *printerRPC) Put(r v3.PutResponse) { p.p((*pb.PutResponse)(&r)) } +func (p *printerRPC) Txn(r v3.TxnResponse) { p.p((*pb.TxnResponse)(&r)) } +func (p *printerRPC) Watch(r v3.WatchResponse) { p.p(&r) } + +func (p *printerRPC) Grant(r v3.LeaseGrantResponse) { p.p(r) } +func (p *printerRPC) Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse) { p.p(r) } +func (p *printerRPC) KeepAlive(r v3.LeaseKeepAliveResponse) { p.p(r) } func (p *printerRPC) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) { p.p(&r) } -func (p *printerRPC) MemberAdd(r v3.MemberAddResponse) { p.p((*pb.MemberAddResponse)(&r)) } + +func (p *printerRPC) MemberAdd(r v3.MemberAddResponse) { p.p((*pb.MemberAddResponse)(&r)) } func (p *printerRPC) MemberRemove(id uint64, r v3.MemberRemoveResponse) { p.p((*pb.MemberRemoveResponse)(&r)) } diff --git a/etcdctl/ctlv3/command/printer_fields.go b/etcdctl/ctlv3/command/printer_fields.go index 3f42604a400..2f19a5cf080 100644 --- a/etcdctl/ctlv3/command/printer_fields.go +++ b/etcdctl/ctlv3/command/printer_fields.go @@ -92,6 +92,22 @@ func (p *fieldsPrinter) Watch(resp v3.WatchResponse) { } } +func (p *fieldsPrinter) Grant(r v3.LeaseGrantResponse) { + p.hdr(r.ResponseHeader) + fmt.Println(`"ID" :`, r.ID) + fmt.Println(`"TTL" :`, r.TTL) +} + +func (p *fieldsPrinter) Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse) { + p.hdr(r.Header) +} + +func (p *fieldsPrinter) KeepAlive(r v3.LeaseKeepAliveResponse) { + p.hdr(r.ResponseHeader) + fmt.Println(`"ID" :`, r.ID) + fmt.Println(`"TTL" :`, r.TTL) +} + func (p *fieldsPrinter) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) { p.hdr(r.ResponseHeader) fmt.Println(`"ID" :`, r.ID) diff --git a/etcdctl/ctlv3/command/printer_simple.go b/etcdctl/ctlv3/command/printer_simple.go index 96e1032e6e1..baa79e23d7c 100644 --- a/etcdctl/ctlv3/command/printer_simple.go +++ b/etcdctl/ctlv3/command/printer_simple.go @@ -79,6 +79,18 @@ func (s *simplePrinter) Watch(resp v3.WatchResponse) { } } +func (s *simplePrinter) Grant(resp v3.LeaseGrantResponse) { + fmt.Printf("lease %016x granted with TTL(%ds)\n", resp.ID, resp.TTL) +} + +func (p *simplePrinter) Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse) { + fmt.Printf("lease %016x revoked\n", id) +} + +func (p *simplePrinter) KeepAlive(resp v3.LeaseKeepAliveResponse) { + fmt.Printf("lease %016x keepalived with TTL(%d)\n", resp.ID, resp.TTL) +} + func (s *simplePrinter) TimeToLive(resp v3.LeaseTimeToLiveResponse, keys bool) { txt := fmt.Sprintf("lease %016x granted with TTL(%ds), remaining(%ds)", resp.ID, resp.GrantedTTL, resp.TTL) if keys {