Sort `machine ls` output by machine name

Signed-off-by: Guillaume Giamarchi <guillaume.giamarchi@gmail.com>
This commit is contained in:
Guillaume Giamarchi 2014-12-12 00:35:20 +01:00
parent 932779fed1
commit 1547a48dde
2 changed files with 46 additions and 6 deletions

View File

@ -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

24
host.go
View File

@ -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
}