-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
51 lines (43 loc) · 1.1 KB
/
utils.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
package dockerdb
import (
"errors"
"fmt"
"github.com/docker/go-connections/nat"
"net"
"strconv"
"time"
)
func SetMaxWaitTime(sec time.Duration) {
_maxWaitTime = sec
}
func SetMaxActualPortTries(n int) {
_maxActualPortTries = n
}
func getFreePort() (nat.Port, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return "0", err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return "0", err
}
defer l.Close()
port := strconv.Itoa(l.Addr().(*net.TCPAddr).Port)
return nat.Port(port), nil
}
// buildConnStr builds connection string by CustomDB config.
func buildConnStr(conf Config) (connStr string, err error) {
switch conf.vendorName {
case postgres:
return fmt.Sprintf(
"host=localhost user=%s password='%s' dbname=%s port=%s sslmode=disable",
conf.db.User, conf.db.Password, conf.db.Name, conf.actualPort), nil
case mysql:
return fmt.Sprintf(
"%s:%s@tcp(127.0.0.1:%s)/%s",
conf.db.User, conf.db.Password, conf.actualPort, conf.db.Name), nil
default:
return "", errors.New("use of unsupported db vendor and no connection string provided")
}
}