forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 119
/
active.go
60 lines (48 loc) · 1.15 KB
/
active.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package commands
import (
"errors"
"fmt"
"time"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/persist"
"github.com/docker/machine/libmachine/state"
)
const (
activeDefaultTimeout = 10
)
var (
errNoActiveHost = errors.New("No active host found")
errActiveTimeout = errors.New("Error getting active host: timeout")
)
func cmdActive(c CommandLine, api libmachine.API) error {
if len(c.Args()) > 0 {
return ErrTooManyArguments
}
hosts, hostsInError, err := persist.LoadAllHosts(api)
if err != nil {
return fmt.Errorf("Error getting active host: %s", err)
}
timeout := time.Duration(c.Int("timeout")) * time.Second
items := getHostListItems(hosts, hostsInError, timeout)
active, err := activeHost(items)
if err != nil {
return err
}
fmt.Println(active.Name)
return nil
}
func activeHost(items []HostListItem) (HostListItem, error) {
timeout := false
for _, item := range items {
if item.ActiveHost || item.ActiveSwarm {
return item, nil
}
if item.State == state.Timeout {
timeout = true
}
}
if timeout {
return HostListItem{}, errActiveTimeout
}
return HostListItem{}, errNoActiveHost
}