mirror of
https://github.com/status-im/consul.git
synced 2025-01-11 14:24:39 +00:00
Merge pull request #969 from tgwizard/members-command-sorted
Sort output in the consul members command
This commit is contained in:
commit
0949790a2f
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/ryanuber/columnize"
|
"github.com/ryanuber/columnize"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -95,6 +96,8 @@ func (c *MembersCommand) Run(args []string) int {
|
|||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Sort(ByMemberName(members))
|
||||||
|
|
||||||
// Generate the output
|
// Generate the output
|
||||||
var result []string
|
var result []string
|
||||||
if detailed {
|
if detailed {
|
||||||
@ -110,6 +113,13 @@ func (c *MembersCommand) Run(args []string) int {
|
|||||||
return 0
|
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
|
// standardOutput is used to dump the most useful information about nodes
|
||||||
// in a more human-friendly format
|
// in a more human-friendly format
|
||||||
func (c *MembersCommand) standardOutput(members []agent.Member) []string {
|
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"
|
header := "Node|Address|Status|Tags"
|
||||||
result = append(result, header)
|
result = append(result, header)
|
||||||
for _, member := range members {
|
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,...
|
// Format the tags as tag1=v1,tag2=v2,...
|
||||||
var tagPairs []string
|
var tagPairs []string
|
||||||
for name, value := range member.Tags {
|
for _, key := range tagKeys {
|
||||||
tagPairs = append(tagPairs, fmt.Sprintf("%s=%s", name, value))
|
tagPairs = append(tagPairs, fmt.Sprintf("%s=%s", key, member.Tags[key]))
|
||||||
}
|
}
|
||||||
|
|
||||||
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)}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user