-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathutil.go
267 lines (242 loc) · 5.44 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* @Description:公用工具类
* @Author: gphper
* @Date: 2021-07-04 11:58:45
*/
package common
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io/fs"
"log"
"math/rand"
"net"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
/**
获取项目根目录
*/
func RootPath() (path string, err error) {
path = getCurrentAbPathByExecutable()
if strings.Contains(path, getTmpDir()) {
path = getCurrentAbPathByCaller()
}
path = strings.Replace(path, "/common", "", 1)
return
}
// 获取系统临时目录,兼容go run
func getTmpDir() string {
dir := os.Getenv("TEMP")
if dir == "" {
dir = os.Getenv("TMP")
}
res, _ := filepath.EvalSymlinks(dir)
return res
}
// 获取当前执行文件绝对路径
func getCurrentAbPathByExecutable() string {
exePath, err := os.Executable()
if err != nil {
log.Fatal(err)
}
res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
return res
}
// 获取当前执行文件绝对路径(go run)
func getCurrentAbPathByCaller() string {
var abPath string
_, filename, _, ok := runtime.Caller(0)
if ok {
abPath = path.Dir(filename)
}
return abPath
}
/**
生成随机字符串
*/
func RandString(len int) string {
r := rand.New(rand.NewSource(time.Now().Unix()))
bytes := make([]byte, len)
for i := 0; i < len; i++ {
b := r.Intn(26) + 65
bytes[i] = byte(b)
}
return string(bytes)
}
/**
密码加密
*/
func Encryption(password string, salt string) string {
str := fmt.Sprintf("%s%s", password, salt)
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
/**
*首字母大写
**/
func StrFirstToUpper(str string) (string, string) {
temp := strings.Split(str, "_")
var upperStr string
var firstStr string
for y := 0; y < len(temp); y++ {
vv := []rune(temp[y])
for i := 0; i < len(vv); i++ {
if i == 0 {
firstStr += string(vv[i])
vv[i] -= 32
upperStr += string(vv[i])
} else {
upperStr += string(vv[i])
}
}
}
return upperStr, firstStr
}
/*
*比较第二个slice一第一个slice的区别
*/
func CompareSlice(first []string, second []string) (add []string, incre []string) {
secondMap := make(map[string]struct{})
for _, v := range second {
secondMap[v] = struct{}{}
}
for _, v := range first {
_, ok := secondMap[v]
if !ok {
incre = append(incre, v)
} else {
delete(secondMap, v)
}
}
for k, _ := range secondMap {
add = append(add, k)
}
return
}
//interface 转字符串
func InterfaceToString(value interface{}) (s string) {
var key string
if value == nil {
return key
}
switch value.(type) {
case float64:
ft := value.(float64)
key = strconv.FormatFloat(ft, 'f', -1, 64)
case float32:
ft := value.(float32)
key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
case int:
it := value.(int)
key = strconv.Itoa(it)
case uint:
it := value.(uint)
key = strconv.Itoa(int(it))
case int8:
it := value.(int8)
key = strconv.Itoa(int(it))
case uint8:
it := value.(uint8)
key = strconv.Itoa(int(it))
case int16:
it := value.(int16)
key = strconv.Itoa(int(it))
case uint16:
it := value.(uint16)
key = strconv.Itoa(int(it))
case int32:
it := value.(int32)
key = strconv.Itoa(int(it))
case uint32:
it := value.(uint32)
key = strconv.Itoa(int(it))
case int64:
it := value.(int64)
key = strconv.FormatInt(it, 10)
case uint64:
it := value.(uint64)
key = strconv.FormatUint(it, 10)
case string:
key = value.(string)
case bool:
val, _ := value.(bool)
if val {
key = "True"
} else {
key = "False"
}
case []byte:
key = string(value.([]byte))
default:
newValue, _ := json.Marshal(value)
key = string(newValue)
}
return key
}
// 获取局域网ip
func GetOutBoundIP() (ip string, err error) {
conn, err := net.Dial("udp", "8.8.8.8:53")
if err != nil {
return "", err
}
localAddr := conn.LocalAddr().(*net.UDPAddr)
ip = strings.Split(localAddr.String(), ":")[0]
return
}
// 输出LOGO
func ShowLogo(host, port string) {
fmt.Printf("%c[%d;%d;%dm%s%c[0m \n", 0x1B, 0, 40, 32, " _____ _____ __ __ ", 0x1B)
fmt.Printf("%c[%d;%d;%dm%s%c[0m \n", 0x1B, 0, 40, 32, " / ____| __ \\| \\/ | ", 0x1B)
fmt.Printf("%c[%d;%d;%dm%s%c[0m \n", 0x1B, 0, 40, 32, "| | __| |__) | \\ / | ", 0x1B)
fmt.Printf("%c[%d;%d;%dm%s%c[0m \n", 0x1B, 0, 40, 32, "| | |_ | _ /| |\\/| | ", 0x1B)
fmt.Printf("%c[%d;%d;%dm%s%c[0m \n", 0x1B, 0, 40, 32, "| |__| | | \\ \\| | | | ", 0x1B)
fmt.Printf("%c[%d;%d;%dm%s%c[0m \n", 0x1B, 0, 40, 32, " \\_____|_| \\_\\_| |_|", 0x1B)
fmt.Println("")
fmt.Printf("%c[%d;%d;%dm%s%c[0m \n", 0x1B, 0, 40, 32, "Go Redis Manage", 0x1B)
fmt.Println("App running at:")
if host == "0.0.0.0" {
fmt.Printf("- Local: %c[%d;%d;%dm%s%c[0m \n", 0x1B, 0, 40, 34, "http://127.0.0.1:"+port, 0x1B)
ip, err := GetOutBoundIP()
if err == nil {
fmt.Printf("- Network: %c[%d;%d;%dm%s%c[0m \n", 0x1B, 0, 40, 34, "http://"+ip+":"+port, 0x1B)
}
} else {
fmt.Printf("- Local: %c[%d;%d;%dm%s%c[0m \n", 0x1B, 0, 40, 34, "http://"+host+":"+port, 0x1B)
}
}
/**
* 创建文件
*/
/**
* 打开文件句柄
**/
func OpenFile(filepath string) (file *os.File, err error) {
file, err = os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE, 0666)
if err == nil {
return
}
dir := path.Dir(filepath)
_, err = os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(dir, fs.FileMode(os.O_CREATE))
if err != nil {
return
}
}
}
file, err = os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return
}
return
}