Merge pull request #969 from tgwizard/members-command-sorted

Sort output in the consul members command
This commit is contained in:
Armon Dadgar 2015-05-22 11:16:11 -07:00
commit 0949790a2f
1 changed files with 20 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/ryanuber/columnize"
"net"
"regexp"
"sort"
"strings"
)
@ -95,6 +96,8 @@ func (c *MembersCommand) Run(args []string) int {
return 2
}
sort.Sort(ByMemberName(members))
// Generate the output
var result []string
if detailed {
@ -110,6 +113,13 @@ func (c *MembersCommand) Run(args []string) int {
return 0
}
// so we can sort members by name
type ByMemberName []agent.Member
func (m ByMemberName) Len() int { return len(m) }
func (m ByMemberName) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m ByMemberName) Less(i, j int) bool { return m[i].Name < m[j].Name }
// standardOutput is used to dump the most useful information about nodes
// in a more human-friendly format
func (c *MembersCommand) standardOutput(members []agent.Member) []string {
@ -152,11 +162,19 @@ func (c *MembersCommand) detailedOutput(members []agent.Member) []string {
header := "Node|Address|Status|Tags"
result = append(result, header)
for _, member := range members {
// Get the tags sorted by key
tagKeys := make([]string, 0, len(member.Tags))
for key := range member.Tags {
tagKeys = append(tagKeys, key)
}
sort.Strings(tagKeys)
// Format the tags as tag1=v1,tag2=v2,...
var tagPairs []string
for name, value := range member.Tags {
tagPairs = append(tagPairs, fmt.Sprintf("%s=%s", name, value))
for _, key := range tagKeys {
tagPairs = append(tagPairs, fmt.Sprintf("%s=%s", key, member.Tags[key]))
}
tags := strings.Join(tagPairs, ",")
addr := net.TCPAddr{IP: member.Addr, Port: int(member.Port)}