-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
164 lines (151 loc) · 3.7 KB
/
util.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package test
import (
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"strings"
"sync/atomic"
"text/template"
"time"
)
const (
// define min and max port range
maxPort = 65535
minPort = 10000
)
var (
curPort = int32(maxPort + 1)
)
// BookPorts books free portsx
func BookPorts(num int, host ...string) ([]int, error) {
result := make([]int, 0, num)
for len(result) < num {
newPort := atomic.AddInt32(&curPort, -1)
if newPort < minPort {
return nil, errors.New("running out of available ports")
}
if portAvailable(newPort, host...) {
result = append(result, int(newPort))
}
}
return result, nil
}
// CheckExecutable checks if given names are executables
func CheckExecutable(names ...string) error {
for _, name := range names {
if _, err := exec.LookPath(name); err != nil {
return err
}
}
return nil
}
// ApplyTemplate applies given variables to the "tplStr" and stores to "filePath"
func ApplyTemplate(filePath string, tplStr string, vars map[string]interface{}) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
tpl, err := template.New("tpl").Parse(tplStr)
if err != nil {
return err
}
return tpl.Execute(file, vars)
}
// CheckListening checks if localhost is listening given ports
func CheckListening(ports ...int) bool {
for _, p := range ports {
conn, err := net.DialTimeout("tcp4", fmt.Sprintf("localhost:%d", p), 500*time.Millisecond)
if err != nil {
return false
}
conn.Close()
}
return true
}
// Exec runs "name" and "arg" in directory "workdir" with environements "envs" and wait util
// process finined. It returns error if fail to execute or exit code is not zero.
func Exec(workdir string, envs []string, stdin io.Reader, name string, arg ...interface{}) error {
argStr := make([]string, 0, len(arg))
for _, a := range arg {
argStr = append(argStr, fmt.Sprintf("%v", a))
}
cmd := exec.Command(name, argStr...)
cmd.Env = append(os.Environ(), envs...)
cmd.Dir = workdir
if stdin != nil {
cmd.Stdin = stdin
}
bs, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("fail to start process, output:%s, err:%v", string(bs), err)
}
if !cmd.ProcessState.Success() {
return fmt.Errorf("fail to start process, output:%s", string(bs))
}
return nil
}
// CombineError given errors into one err, note that nil error will be ignored
func CombineError(errs ...error) error {
result := []string{}
for _, err := range errs {
if err != nil {
result = append(result, err.Error())
}
}
if len(result) != 0 {
return errors.New(strings.Join(result, ", "))
}
return nil
}
func portAvailable(port int32, host ...string) bool {
// Listen to tcp4 only instead of tcp which may return an ipv6 address port.
h := ""
if len(h) != 0 {
h = host[0]
}
l, err := net.Listen("tcp4", fmt.Sprintf("%s:%d", h, port))
if err != nil {
return false
}
l.Close()
return true
}
// WaitPortAvail waits until the port becomes dialable. It returns error if it's not
// available after timeout.
func WaitPortAvail(port int, timeout time.Duration, host ...string) error {
h := "localhost"
if len(host) != 0 {
h = host[0]
}
addr := fmt.Sprintf("%s:%d", h, port)
timer := time.NewTimer(timeout)
wait := 1 * time.Second
for {
c, err := net.Dial("tcp", addr)
if err == nil {
c.Close()
timer.Stop()
return nil
}
fmt.Printf("Attempt to dial %v failed, err %v\n", addr, err)
select {
case <-timer.C:
return fmt.Errorf("attempt to dial %v timeout after %v", port, timeout)
case <-time.After(wait):
fmt.Printf("Do another try after waiting for %v\n", wait)
wait *= 2
}
}
}
func GetPort() int {
l, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}