From a8acbe5cfb587d31ca0aa1be841913e428abed71 Mon Sep 17 00:00:00 2001 From: poornas Date: Tue, 10 Jan 2017 13:42:34 -0800 Subject: [PATCH] sort output of mc config host list (#1959) --- cmd/config-host-main.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/config-host-main.go b/cmd/config-host-main.go index f7a619aa..9d10e48c 100644 --- a/cmd/config-host-main.go +++ b/cmd/config-host-main.go @@ -19,6 +19,7 @@ package cmd import ( "encoding/json" "fmt" + "sort" "strings" "github.com/fatih/color" @@ -265,24 +266,44 @@ func removeHost(alias string) { printMsg(hostMessage{op: "remove", Alias: alias}) } +//byAlias is a collection satisfying sort.Interface +type byAlias []hostMessage + +func (d byAlias) Len() int { return len(d) } +func (d byAlias) Swap(i, j int) { d[i], d[j] = d[j], d[i] } +func (d byAlias) Less(i, j int) bool { return d[i].Alias < d[j].Alias } + // listHosts - list all host URLs. func listHosts() { conf, err := loadMcConfig() fatalIf(err.Trace(globalMCConfigVersion), "Unable to load config version ‘"+globalMCConfigVersion+"’.") + var maxAlias = 0 for k := range conf.Hosts { if len(k) > maxAlias { maxAlias = len(k) } } + + var hosts []hostMessage for k, v := range conf.Hosts { - printMsg(hostMessage{ + hosts = append(hosts, hostMessage{ op: "list", - Alias: fmt.Sprintf("%*.*s", maxAlias, maxAlias, k), + Alias: k, URL: v.URL, AccessKey: v.AccessKey, SecretKey: v.SecretKey, API: v.API, }) } + + // Sort hosts by alias names lexically. + sort.Sort(byAlias(hosts)) + + // Display all the hosts. + for _, host := range hosts { + // Format properly for alignment based on alias length. + host.Alias = fmt.Sprintf("%-*.*s", maxAlias, maxAlias, host.Alias) + printMsg(host) + } }