-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
81 lines (71 loc) · 1.36 KB
/
common.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
package helpers
import (
"github.com/urfave/cli"
"net"
"path"
"runtime"
"time"
)
// 获取当前目录
func GetCurrentDirectory() string {
_, filename, _, ok := runtime.Caller(1)
var cwdPath string
if ok {
cwdPath = path.Join(path.Dir(filename), "")
} else {
cwdPath = "./"
}
return cwdPath
}
// 获取本机IP
func GetLocalIp() (ipv4 string, err error) {
// 获取所有网卡
addrs, err := net.InterfaceAddrs();
if err != nil {
return
}
// 取第一个非lo的网卡IP
for _, addr := range addrs {
// 这个网络地址是IP地址: ipv4, ipv6
ipNet, isIpNet := addr.(*net.IPNet);
if isIpNet && !ipNet.IP.IsLoopback() {
// 跳过IPV6
if ipNet.IP.To4() != nil {
ipv4 = ipNet.IP.String()
return
}
}
}
return
}
// 初始化线程数
func InitThreadNum(num int) {
runtime.GOMAXPROCS(num)
}
func StringFlag(name, value, usage string) cli.StringFlag {
return cli.StringFlag{
Name: name,
Value: value,
Usage: usage,
}
}
func BoolFlag(name, usage string) cli.BoolFlag {
return cli.BoolFlag{
Name: name,
Usage: usage,
}
}
func IntFlag(name string, value int, usage string) cli.IntFlag {
return cli.IntFlag{
Name: name,
Value: value,
Usage: usage,
}
}
func DurationFlag(name string, value time.Duration, usage string) cli.DurationFlag {
return cli.DurationFlag{
Name: name,
Value: value,
Usage: usage,
}
}