mirror of https://github.com/docker/docs.git
Merge pull request #223 from nathanleclaire/cleanup_ls
Refactor ls to use a channel instead
This commit is contained in:
commit
012e4a1a38
73
commands.go
73
commands.go
|
@ -7,7 +7,6 @@ import (
|
|||
"os/exec"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"text/tabwriter"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
|
@ -45,6 +44,36 @@ func (h hostListItemByName) Less(i, j int) bool {
|
|||
return strings.ToLower(h[i].Name) < strings.ToLower(h[j].Name)
|
||||
}
|
||||
|
||||
func getHostState(host Host, store Store, hostListItems chan<- hostListItem) {
|
||||
currentState, err := host.Driver.GetState()
|
||||
if err != nil {
|
||||
log.Errorf("error getting state for host %s: %s", host.Name, err)
|
||||
}
|
||||
|
||||
url, err := host.GetURL()
|
||||
if err != nil {
|
||||
if err == drivers.ErrHostIsNotRunning {
|
||||
url = ""
|
||||
} else {
|
||||
log.Errorf("error getting URL for host %s: %s", host.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
isActive, err := store.IsActive(&host)
|
||||
if err != nil {
|
||||
log.Errorf("error determining whether host %q is active: %s",
|
||||
host.Name, err)
|
||||
}
|
||||
|
||||
hostListItems <- hostListItem{
|
||||
Name: host.Name,
|
||||
Active: isActive,
|
||||
DriverName: host.Driver.DriverName(),
|
||||
State: currentState,
|
||||
URL: url,
|
||||
}
|
||||
}
|
||||
|
||||
var Commands = []cli.Command{
|
||||
{
|
||||
Name: "active",
|
||||
|
@ -177,50 +206,24 @@ var Commands = []cli.Command{
|
|||
fmt.Fprintln(w, "NAME\tACTIVE\tDRIVER\tSTATE\tURL")
|
||||
}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
items := []hostListItem{}
|
||||
hostListItems := make(chan hostListItem)
|
||||
|
||||
for _, host := range hostList {
|
||||
host := host
|
||||
if quiet {
|
||||
if !quiet {
|
||||
go getHostState(host, *store, hostListItems)
|
||||
} else {
|
||||
fmt.Fprintf(w, "%s\n", host.Name)
|
||||
} else {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
currentState, err := host.Driver.GetState()
|
||||
if err != nil {
|
||||
log.Errorf("error getting state for host %s: %s", host.Name, err)
|
||||
}
|
||||
|
||||
url, err := host.GetURL()
|
||||
if err != nil {
|
||||
if err == drivers.ErrHostIsNotRunning {
|
||||
url = ""
|
||||
} else {
|
||||
log.Errorf("error getting URL for host %s: %s", host.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
isActive, err := store.IsActive(&host)
|
||||
if err != nil {
|
||||
log.Errorf("error determining whether host %q is active: %s",
|
||||
host.Name, err)
|
||||
}
|
||||
|
||||
items = append(items, hostListItem{
|
||||
Name: host.Name,
|
||||
Active: isActive,
|
||||
DriverName: host.Driver.DriverName(),
|
||||
State: currentState,
|
||||
URL: url,
|
||||
})
|
||||
|
||||
wg.Done()
|
||||
}()
|
||||
if !quiet {
|
||||
for i := 0; i < len(hostList); i++ {
|
||||
items = append(items, <-hostListItems)
|
||||
}
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(hostListItems)
|
||||
|
||||
sort.Sort(hostListItemByName(items))
|
||||
|
||||
|
|
Loading…
Reference in New Issue