Merge pull request #87 from ggiamarchi/machine-ls-sort

Sort `machine ls` output by machine name
This commit is contained in:
Ben Firshman 2014-12-12 01:14:12 +00:00
commit e12df0cbc2
1 changed files with 45 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
"sort"
"strings" "strings"
"sync" "sync"
"text/tabwriter" "text/tabwriter"
@ -17,6 +18,7 @@ import (
_ "github.com/docker/machine/drivers/digitalocean" _ "github.com/docker/machine/drivers/digitalocean"
_ "github.com/docker/machine/drivers/none" _ "github.com/docker/machine/drivers/none"
_ "github.com/docker/machine/drivers/virtualbox" _ "github.com/docker/machine/drivers/virtualbox"
"github.com/docker/machine/state"
) )
type DockerCli struct{} type DockerCli struct{}
@ -92,6 +94,28 @@ func (cli *DockerCli) CmdHelp(args ...string) error {
return nil return nil
} }
type HostListItem struct {
Name string
Active bool
DriverName string
State state.State
URL string
}
type HostListItemByName []HostListItem
func (h HostListItemByName) Len() int {
return len(h)
}
func (h HostListItemByName) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h HostListItemByName) Less(i, j int) bool {
return strings.ToLower(h[i].Name) < strings.ToLower(h[j].Name)
}
func (cli *DockerCli) CmdLs(args ...string) error { func (cli *DockerCli) CmdLs(args ...string) error {
cmd := cli.Subcmd("ls", "", "List machines") cmd := cli.Subcmd("ls", "", "List machines")
quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only display names") quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only display names")
@ -115,6 +139,8 @@ func (cli *DockerCli) CmdLs(args ...string) error {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
hostListItem := []HostListItem{}
for _, host := range hostList { for _, host := range hostList {
host := host host := host
if *quiet { if *quiet {
@ -142,19 +168,32 @@ func (cli *DockerCli) CmdLs(args ...string) error {
host.Name, err) host.Name, err)
} }
activeString := "" hostListItem = append(hostListItem, 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(hostListItem))
for _, hostState := range hostListItem {
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() w.Flush()
return nil return nil