|
|
@@ -51,10 +51,57 @@ func NewPrinter(printerType string, isHex bool) printer {
|
|
|
return &jsonPrinter{}
|
|
|
case "protobuf":
|
|
|
return &pbPrinter{}
|
|
|
+ case "table":
|
|
|
+ return &tablePrinter{}
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func makeMemberListTable(r v3.MemberListResponse) (hdr []string, rows [][]string) {
|
|
|
+ hdr = []string{"ID", "Status", "Name", "Peer Addrs", "Client Addrs"}
|
|
|
+ for _, m := range r.Members {
|
|
|
+ status := "started"
|
|
|
+ if len(m.Name) == 0 {
|
|
|
+ status = "unstarted"
|
|
|
+ }
|
|
|
+ rows = append(rows, []string{
|
|
|
+ fmt.Sprintf("%x", m.ID),
|
|
|
+ status,
|
|
|
+ m.Name,
|
|
|
+ strings.Join(m.PeerURLs, ","),
|
|
|
+ strings.Join(m.ClientURLs, ","),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func makeEndpointStatusTable(statusList []epStatus) (hdr []string, rows [][]string) {
|
|
|
+ hdr = []string{"endpoint", "ID", "version", "db size", "is leader", "raft term", "raft index"}
|
|
|
+ for _, status := range statusList {
|
|
|
+ rows = append(rows, []string{
|
|
|
+ fmt.Sprint(status.Ep),
|
|
|
+ fmt.Sprintf("%x", status.Resp.Header.MemberId),
|
|
|
+ fmt.Sprint(status.Resp.Version),
|
|
|
+ fmt.Sprint(humanize.Bytes(uint64(status.Resp.DbSize))),
|
|
|
+ fmt.Sprint(status.Resp.Leader == status.Resp.Header.MemberId),
|
|
|
+ fmt.Sprint(status.Resp.RaftTerm),
|
|
|
+ fmt.Sprint(status.Resp.RaftIndex),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func makeDBStatusTable(ds dbstatus) (hdr []string, rows [][]string) {
|
|
|
+ hdr = []string{"hash", "revision", "total keys", "total size"}
|
|
|
+ rows = append(rows, []string{
|
|
|
+ fmt.Sprintf("%x", ds.Hash),
|
|
|
+ fmt.Sprint(ds.Revision),
|
|
|
+ fmt.Sprint(ds.TotalKey),
|
|
|
+ humanize.Bytes(uint64(ds.TotalSize)),
|
|
|
+ })
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
type simplePrinter struct {
|
|
|
isHex bool
|
|
|
}
|
|
|
@@ -107,57 +154,71 @@ func (s *simplePrinter) Alarm(resp v3.AlarmResponse) {
|
|
|
}
|
|
|
|
|
|
func (s *simplePrinter) MemberList(resp v3.MemberListResponse) {
|
|
|
- table := tablewriter.NewWriter(os.Stdout)
|
|
|
- table.SetHeader([]string{"ID", "Status", "Name", "Peer Addrs", "Client Addrs"})
|
|
|
+ _, rows := makeMemberListTable(resp)
|
|
|
+ for _, row := range rows {
|
|
|
+ fmt.Println(strings.Join(row, ", "))
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- for _, m := range resp.Members {
|
|
|
- status := "started"
|
|
|
- if len(m.Name) == 0 {
|
|
|
- status = "unstarted"
|
|
|
- }
|
|
|
+func (s *simplePrinter) EndpointStatus(statusList []epStatus) {
|
|
|
+ _, rows := makeEndpointStatusTable(statusList)
|
|
|
+ for _, row := range rows {
|
|
|
+ fmt.Println(strings.Join(row, ", "))
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- table.Append([]string{
|
|
|
- fmt.Sprintf("%x", m.ID),
|
|
|
- status,
|
|
|
- m.Name,
|
|
|
- strings.Join(m.PeerURLs, ","),
|
|
|
- strings.Join(m.ClientURLs, ","),
|
|
|
- })
|
|
|
+func (s *simplePrinter) DBStatus(ds dbstatus) {
|
|
|
+ _, rows := makeDBStatusTable(ds)
|
|
|
+ for _, row := range rows {
|
|
|
+ fmt.Println(strings.Join(row, ", "))
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
+type tablePrinter struct{}
|
|
|
+
|
|
|
+func (tp *tablePrinter) Del(r v3.DeleteResponse) {
|
|
|
+ ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
|
|
|
+}
|
|
|
+func (tp *tablePrinter) Get(r v3.GetResponse) {
|
|
|
+ ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
|
|
|
+}
|
|
|
+func (tp *tablePrinter) Put(r v3.PutResponse) {
|
|
|
+ ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
|
|
|
+}
|
|
|
+func (tp *tablePrinter) Txn(r v3.TxnResponse) {
|
|
|
+ ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
|
|
|
+}
|
|
|
+func (tp *tablePrinter) Watch(r v3.WatchResponse) {
|
|
|
+ ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
|
|
|
+}
|
|
|
+func (tp *tablePrinter) Alarm(r v3.AlarmResponse) {
|
|
|
+ ExitWithError(ExitBadFeature, errors.New("table is not supported as output format"))
|
|
|
+}
|
|
|
+func (tp *tablePrinter) MemberList(r v3.MemberListResponse) {
|
|
|
+ hdr, rows := makeMemberListTable(r)
|
|
|
+ table := tablewriter.NewWriter(os.Stdout)
|
|
|
+ table.SetHeader(hdr)
|
|
|
+ for _, row := range rows {
|
|
|
+ table.Append(row)
|
|
|
+ }
|
|
|
table.Render()
|
|
|
}
|
|
|
-
|
|
|
-func (s *simplePrinter) EndpointStatus(statusList []epStatus) {
|
|
|
+func (tp *tablePrinter) EndpointStatus(r []epStatus) {
|
|
|
+ hdr, rows := makeEndpointStatusTable(r)
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
- table.SetHeader([]string{"endpoint", "ID", "version", "db size", "is leader", "raft term", "raft index"})
|
|
|
-
|
|
|
- for _, status := range statusList {
|
|
|
- table.Append([]string{
|
|
|
- fmt.Sprint(status.Ep),
|
|
|
- fmt.Sprintf("%x", status.Resp.Header.MemberId),
|
|
|
- fmt.Sprint(status.Resp.Version),
|
|
|
- fmt.Sprint(humanize.Bytes(uint64(status.Resp.DbSize))),
|
|
|
- fmt.Sprint(status.Resp.Leader == status.Resp.Header.MemberId),
|
|
|
- fmt.Sprint(status.Resp.RaftTerm),
|
|
|
- fmt.Sprint(status.Resp.RaftIndex),
|
|
|
- })
|
|
|
+ table.SetHeader(hdr)
|
|
|
+ for _, row := range rows {
|
|
|
+ table.Append(row)
|
|
|
}
|
|
|
-
|
|
|
table.Render()
|
|
|
}
|
|
|
-
|
|
|
-func (s *simplePrinter) DBStatus(ds dbstatus) {
|
|
|
+func (tp *tablePrinter) DBStatus(r dbstatus) {
|
|
|
+ hdr, rows := makeDBStatusTable(r)
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
- table.SetHeader([]string{"hash", "revision", "total keys", "total size"})
|
|
|
-
|
|
|
- table.Append([]string{
|
|
|
- fmt.Sprintf("%x", ds.Hash),
|
|
|
- fmt.Sprint(ds.Revision),
|
|
|
- fmt.Sprint(ds.TotalKey),
|
|
|
- humanize.Bytes(uint64(ds.TotalSize)),
|
|
|
- })
|
|
|
-
|
|
|
+ table.SetHeader(hdr)
|
|
|
+ for _, row := range rows {
|
|
|
+ table.Append(row)
|
|
|
+ }
|
|
|
table.Render()
|
|
|
}
|
|
|
|