Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"reflect"
"sort"
"strings"
"sync"
"text/tabwriter"
Expand All @@ -17,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{}
Expand Down Expand Up @@ -92,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")
Expand All @@ -115,6 +139,8 @@ func (cli *DockerCli) CmdLs(args ...string) error {

wg := sync.WaitGroup{}

hostListItem := []HostListItem{}

for _, host := range hostList {
host := host
if *quiet {
Expand Down Expand Up @@ -142,19 +168,32 @@ func (cli *DockerCli) CmdLs(args ...string) error {
host.Name, err)
}

activeString := ""
if isActive {
activeString = "*"
}
hostListItem = append(hostListItem, HostListItem{
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(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()

return nil
Expand Down