forked from perlogix/libdetectcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
62 lines (52 loc) · 928 Bytes
/
detect.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
61
62
package libdetectcloud
import (
"context"
"net/http"
"sync"
"time"
)
var hc = &http.Client{Timeout: 300 * time.Millisecond}
func Detect() string {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
detectFuncs := []func() string{
detectAlibabaCloud,
detectAWS,
detectAzure,
detectDigitalOcean,
detectGCE,
detectOracle,
detectIBMCloud,
detectOpenStack,
detectSoftlayer,
detectVultr,
detectContainer,
}
results := make(chan string, len(detectFuncs))
var wg sync.WaitGroup
wg.Add(len(detectFuncs))
for _, fn := range detectFuncs {
go func(f func() string) {
defer wg.Done()
select {
case <-ctx.Done():
return
default:
if result := f(); result != "" {
results <- result
cancel()
}
}
}(fn)
}
go func() {
wg.Wait()
close(results)
}()
for result := range results {
if result != "" {
return result
}
}
return ""
}