From 1547a48dde8fa37aa3e6b992d647e9458e531f1f Mon Sep 17 00:00:00 2001 From: Guillaume Giamarchi Date: Fri, 12 Dec 2014 00:35:20 +0100 Subject: [PATCH 1/3] 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 } From 8797ca2d505bf19c45232f766f9e4b6741847360 Mon Sep 17 00:00:00 2001 From: Guillaume Giamarchi Date: Fri, 12 Dec 2014 01:44:34 +0100 Subject: [PATCH 2/3] Rename HostState struct to HostListItem Signed-off-by: Guillaume Giamarchi --- commands.go | 8 ++++---- host.go | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/commands.go b/commands.go index 3a62e1fa10..d9a21d3e8e 100644 --- a/commands.go +++ b/commands.go @@ -116,7 +116,7 @@ func (cli *DockerCli) CmdLs(args ...string) error { wg := sync.WaitGroup{} - hostStateList := []HostState{} + hostListItem := []HostListItem{} for _, host := range hostList { host := host @@ -145,7 +145,7 @@ func (cli *DockerCli) CmdLs(args ...string) error { host.Name, err) } - hostStateList = append(hostStateList, HostState{ + hostListItem = append(hostListItem, HostListItem{ Name: host.Name, Active: isActive, DriverName: host.Driver.DriverName(), @@ -160,9 +160,9 @@ func (cli *DockerCli) CmdLs(args ...string) error { wg.Wait() - sort.Sort(HostStateByName(hostStateList)) + sort.Sort(HostListItemByName(hostListItem)) - for _, hostState := range hostStateList { + for _, hostState := range hostListItem { activeString := "" if hostState.Active { activeString = "*" diff --git a/host.go b/host.go index a5356c6aa6..03adb808fc 100644 --- a/host.go +++ b/host.go @@ -26,7 +26,7 @@ type Host struct { storePath string } -type HostState struct { +type HostListItem struct { Name string Active bool DriverName string @@ -34,17 +34,17 @@ type HostState struct { URL string } -type HostStateByName []HostState +type HostListItemByName []HostListItem -func (h HostStateByName) Len() int { +func (h HostListItemByName) Len() int { return len(h) } -func (h HostStateByName) Swap(i, j int) { +func (h HostListItemByName) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h HostStateByName) Less(i, j int) bool { +func (h HostListItemByName) Less(i, j int) bool { return strings.ToLower(h[i].Name) < strings.ToLower(h[j].Name) } From d8921ee432f23e5c46244a846a128a13271975a9 Mon Sep 17 00:00:00 2001 From: Guillaume Giamarchi Date: Fri, 12 Dec 2014 01:49:31 +0100 Subject: [PATCH 3/3] Move HostListItem from host.go to command.go Signed-off-by: Guillaume Giamarchi --- commands.go | 23 +++++++++++++++++++++++ host.go | 24 ------------------------ 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/commands.go b/commands.go index d9a21d3e8e..0b62e080c1 100644 --- a/commands.go +++ b/commands.go @@ -18,6 +18,7 @@ import ( _ "github.com/docker/machine/drivers/digitalocean" _ "github.com/docker/machine/drivers/none" _ "github.com/docker/machine/drivers/virtualbox" + "github.com/docker/machine/state" ) type DockerCli struct{} @@ -93,6 +94,28 @@ func (cli *DockerCli) CmdHelp(args ...string) error { 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 { cmd := cli.Subcmd("ls", "", "List machines") quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only display names") diff --git a/host.go b/host.go index 03adb808fc..aa3476034b 100644 --- a/host.go +++ b/host.go @@ -7,11 +7,9 @@ import ( "os" "path" "regexp" - "strings" log "github.com/Sirupsen/logrus" "github.com/docker/machine/drivers" - "github.com/docker/machine/state" ) var ( @@ -26,28 +24,6 @@ type Host struct { storePath string } -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) -} - type hostConfig struct { DriverName string }