From 6aa21152d13c752609b796d98eea88103c67f0a1 Mon Sep 17 00:00:00 2001 From: Adam Renberg Date: Fri, 22 May 2015 10:27:47 +0200 Subject: [PATCH 1/2] Sort tags in consul members -detailed output --- command/members.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/command/members.go b/command/members.go index 711195528f..3fcaa3969a 100644 --- a/command/members.go +++ b/command/members.go @@ -8,6 +8,7 @@ import ( "github.com/ryanuber/columnize" "net" "regexp" + "sort" "strings" ) @@ -152,11 +153,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)} From 7d0959b34e047ee9679546d17412b742ff396797 Mon Sep 17 00:00:00 2001 From: Adam Renberg Date: Fri, 22 May 2015 10:37:54 +0200 Subject: [PATCH 2/2] Sort members in by name for consul members --- command/members.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/command/members.go b/command/members.go index 3fcaa3969a..f9134a9a8c 100644 --- a/command/members.go +++ b/command/members.go @@ -96,6 +96,8 @@ func (c *MembersCommand) Run(args []string) int { return 2 } + sort.Sort(ByMemberName(members)) + // Generate the output var result []string if detailed { @@ -111,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 {