Skip to content

Commit

Permalink
update getVMInfoList
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed May 24, 2020
1 parent ed5f0f5 commit 6bf3ba2
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions vms/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"sort"
"strings"
"sync"
)

var (
Expand Down Expand Up @@ -81,34 +82,22 @@ func List(all bool) ([]string, error) {

func getVMInfoList(vms []string) string {

if len(vms) == 0 {
numVms := len(vms)
if numVms == 0 {
return header
}

ch := make(chan *vmInfo, len(vms))
infoList := make(vmInfoList, numVms)
var wg sync.WaitGroup
wg.Add(numVms)

for _, vmName := range vms {
go func(vm string) {
ch <- getVMInfo(vm)
}(vmName)
}

// collect from channel
var i int
infoList := make(vmInfoList, 0)

for {
select {
case vmInfo := <-ch:
if vmInfo != nil {
infoList = append(infoList, vmInfo)
}
i++
}
if i == len(vms) {
break
}
for i, vmName := range vms {
go func(vm string, i int) {
infoList[i] = getVMInfo(vm)
wg.Done()
}(vmName, i)
}
wg.Wait()

return infoList.String()
}
Expand Down

0 comments on commit 6bf3ba2

Please sign in to comment.