From 1547a48dde8fa37aa3e6b992d647e9458e531f1f Mon Sep 17 00:00:00 2001 From: Guillaume Giamarchi Date: Fri, 12 Dec 2014 00:35:20 +0100 Subject: [PATCH] Sort `machine ls` output by machine name Signed-off-by: Guillaume Giamarchi --- commands.go | 28 ++++++++++++++++++++++------ host.go | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/commands.go b/commands.go index 7c6c06f094..3a62e1fa10 100644 --- a/commands.go +++ b/commands.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "reflect" + "sort" "strings" "sync" "text/tabwriter" @@ -115,6 +116,8 @@ func (cli *DockerCli) CmdLs(args ...string) error { wg := sync.WaitGroup{} + hostStateList := []HostState{} + for _, host := range hostList { host := host if *quiet { @@ -142,19 +145,32 @@ func (cli *DockerCli) CmdLs(args ...string) error { host.Name, err) } - activeString := "" - if isActive { - activeString = "*" - } + hostStateList = append(hostStateList, HostState{ + Name: host.Name, + Active: isActive, + DriverName: host.Driver.DriverName(), + State: currentState, + URL: url, + }) - fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", - host.Name, activeString, host.Driver.DriverName(), currentState, url) wg.Done() }() } } wg.Wait() + + sort.Sort(HostStateByName(hostStateList)) + + for _, hostState := range hostStateList { + activeString := "" + if hostState.Active { + activeString = "*" + } + fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", + hostState.Name, activeString, hostState.DriverName, hostState.State, hostState.URL) + } + w.Flush() return nil diff --git a/host.go b/host.go index aa3476034b..a5356c6aa6 100644 --- a/host.go +++ b/host.go @@ -7,9 +7,11 @@ import ( "os" "path" "regexp" + "strings" log "github.com/Sirupsen/logrus" "github.com/docker/machine/drivers" + "github.com/docker/machine/state" ) var ( @@ -24,6 +26,28 @@ type Host struct { storePath string } +type HostState struct { + Name string + Active bool + DriverName string + State state.State + URL string +} + +type HostStateByName []HostState + +func (h HostStateByName) Len() int { + return len(h) +} + +func (h HostStateByName) Swap(i, j int) { + h[i], h[j] = h[j], h[i] +} + +func (h HostStateByName) Less(i, j int) bool { + return strings.ToLower(h[i].Name) < strings.ToLower(h[j].Name) +} + type hostConfig struct { DriverName string }