Restore sorting of ls output

Signed-off-by: Ben Firshman <ben@firshman.co.uk>
This commit is contained in:
Ben Firshman 2014-12-12 16:47:34 -08:00
parent 91172449c0
commit a913c06888
1 changed files with 26 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"sort"
"strings" "strings"
"sync" "sync"
"text/tabwriter" "text/tabwriter"
@ -21,7 +22,7 @@ import (
"github.com/docker/machine/state" "github.com/docker/machine/state"
) )
type HostListItem struct { type hostListItem struct {
Name string Name string
Active bool Active bool
DriverName string DriverName string
@ -29,17 +30,17 @@ type HostListItem struct {
URL string URL string
} }
type HostListItemByName []HostListItem type hostListItemByName []hostListItem
func (h HostListItemByName) Len() int { func (h hostListItemByName) Len() int {
return len(h) return len(h)
} }
func (h HostListItemByName) Swap(i, j int) { func (h hostListItemByName) Swap(i, j int) {
h[i], h[j] = h[j], h[i] h[i], h[j] = h[j], h[i]
} }
func (h HostListItemByName) Less(i, j int) bool { func (h hostListItemByName) Less(i, j int) bool {
return strings.ToLower(h[i].Name) < strings.ToLower(h[j].Name) return strings.ToLower(h[i].Name) < strings.ToLower(h[j].Name)
} }
@ -176,6 +177,7 @@ var Commands = []cli.Command{
} }
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
items := []hostListItem{}
for _, host := range hostList { for _, host := range hostList {
host := host host := host
@ -204,19 +206,32 @@ var Commands = []cli.Command{
host.Name, err) host.Name, err)
} }
activeString := "" items = append(items, hostListItem{
if isActive { Name: host.Name,
activeString = "*" 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.Done()
}() }()
} }
} }
wg.Wait() wg.Wait()
sort.Sort(hostListItemByName(items))
for _, item := range items {
activeString := ""
if item.Active {
activeString = "*"
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
item.Name, activeString, item.DriverName, item.State, item.URL)
}
w.Flush() w.Flush()
}, },
}, },