command/members: USe the columnize library for members

This commit is contained in:
Armon Dadgar 2014-03-05 19:52:31 -08:00
parent e914c0a70b
commit 76578ea752

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/hashicorp/consul/command/agent" "github.com/hashicorp/consul/command/agent"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/ryanuber/columnize"
"net" "net"
"regexp" "regexp"
"strings" "strings"
@ -85,6 +86,7 @@ func (c *MembersCommand) Run(args []string) int {
return 1 return 1
} }
result := make([]string, 0, len(members))
for _, member := range members { for _, member := range members {
// Skip the non-matching members // Skip the non-matching members
if !roleRe.MatchString(member.Tags["role"]) || !statusRe.MatchString(member.Status) { if !roleRe.MatchString(member.Tags["role"]) || !statusRe.MatchString(member.Status) {
@ -99,17 +101,21 @@ func (c *MembersCommand) Run(args []string) int {
tags := strings.Join(tagPairs, ",") tags := strings.Join(tagPairs, ",")
addr := net.TCPAddr{IP: member.Addr, Port: int(member.Port)} addr := net.TCPAddr{IP: member.Addr, Port: int(member.Port)}
c.Ui.Output(fmt.Sprintf("%s %s %s %s", line := fmt.Sprintf("%s|%s|%s|%s",
member.Name, addr.String(), member.Status, tags)) member.Name, addr.String(), member.Status, tags)
if detailed { if detailed {
c.Ui.Output(fmt.Sprintf(" Protocol Version: %d", line += fmt.Sprintf(
member.DelegateCur)) "|Protocol Version: %d|Available Protocol Range: [%d, %d]",
c.Ui.Output(fmt.Sprintf(" Available Protocol Range: [%d, %d]", member.DelegateCur, member.DelegateMin, member.DelegateMax)
member.DelegateMin, member.DelegateMax))
} }
result = append(result, line)
} }
// Generate the columnized version
output, _ := columnize.SimpleFormat(result)
c.Ui.Output(string(output))
return 0 return 0
} }