-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
140 lines (126 loc) · 2.92 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
package main
import (
"fmt"
"net"
"os"
"os/signal"
"syscall"
mathrand "math/rand"
"github.com/astaxie/beego/logs"
)
func SaveToFile(name string, body []byte) error {
return os.WriteFile(name, body, 0664)
}
func GetToken(length int) string {
token := make([]byte, length)
bytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!#$%^&*"
for i := 0; i < length; i++ {
token[i] = bytes[mathrand.Int()%len(bytes)]
}
return string(token)
}
func GetUser(length int) string {
token := make([]byte, length)
bytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
for i := 0; i < length; i++ {
token[i] = bytes[mathrand.Int()%len(bytes)]
}
return string(token)
}
func CapSignal(proc func()) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGKILL, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-signalChan
proc()
logs.Error("recv signcal %s, ready to exit", sig.String())
os.Exit(-1)
}()
}
func InterfaceAddsGet(iface *net.Interface) ([]net.IP, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
ips := make([]net.IP, 0)
for _, v := range addrs {
ipone, _, err := net.ParseCIDR(v.String())
if err != nil {
continue
}
if len(ipone) > 0 {
ips = append(ips, ipone)
}
}
if len(ips) == 0 {
return nil, fmt.Errorf("interface not any address.")
}
return ips, nil
}
func ByteViewLite(size int64) string {
if size < 1024 {
return fmt.Sprintf("%db", size)
} else if size < (1024 * 1024) {
return fmt.Sprintf("%.1fKb", float64(size)/float64(1024))
} else {
return fmt.Sprintf("%.1fMb", float64(size)/float64(1024*1024))
}
}
func ByteView(size int64) string {
if size < 1024 {
return fmt.Sprintf("%dB", size)
} else if size < (1024 * 1024) {
return fmt.Sprintf("%.1fKB", float64(size)/float64(1024))
} else if size < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.1fMB", float64(size)/float64(1024*1024))
} else if size < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.1fGB", float64(size)/float64(1024*1024*1024))
} else {
return fmt.Sprintf("%.1fTB", float64(size)/float64(1024*1024*1024*1024))
}
}
func StringList(list []string) string {
var body string
for idx, v := range list {
if idx == len(list)-1 {
body += fmt.Sprintf("%s", v)
} else {
body += fmt.Sprintf("%s;", v)
}
}
return body
}
func StringDiff(oldlist []string, newlist []string) ([]string, []string) {
del := make([]string, 0)
add := make([]string, 0)
for _, v1 := range oldlist {
flag := false
for _, v2 := range newlist {
if v1 == v2 {
flag = true
break
}
}
if !flag {
del = append(del, v1)
}
}
for _, v1 := range newlist {
flag := false
for _, v2 := range oldlist {
if v1 == v2 {
flag = true
break
}
}
if !flag {
add = append(add, v1)
}
}
return del, add
}
func StringClone(list []string) []string {
output := make([]string, len(list))
copy(output, list)
return output
}