2021-03-11 12:57:30 +08:00
|
|
|
|
package util
|
2020-03-27 11:31:19 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2020-03-28 17:16:23 +08:00
|
|
|
|
"crypto/md5"
|
|
|
|
|
|
"encoding/hex"
|
2020-03-27 11:31:19 +08:00
|
|
|
|
"fmt"
|
2020-04-05 21:01:26 +08:00
|
|
|
|
"io/ioutil"
|
2020-03-27 11:31:19 +08:00
|
|
|
|
"log"
|
2020-03-27 18:14:19 +08:00
|
|
|
|
"net/url"
|
2020-03-27 11:31:19 +08:00
|
|
|
|
"os"
|
2020-03-27 18:14:19 +08:00
|
|
|
|
"strings"
|
2020-04-05 21:01:26 +08:00
|
|
|
|
"time"
|
2020-03-27 18:14:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2020-03-29 12:51:39 +08:00
|
|
|
|
//true=>no error
|
2020-04-05 21:01:26 +08:00
|
|
|
|
func Min(x, y int) int {
|
|
|
|
|
|
if x < y {
|
|
|
|
|
|
return x
|
|
|
|
|
|
}
|
|
|
|
|
|
return y
|
|
|
|
|
|
}
|
2020-03-27 11:31:19 +08:00
|
|
|
|
func CheckErr(err error) bool {
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Println(err)
|
2020-03-29 12:51:39 +08:00
|
|
|
|
fmt.Println("ERROR")
|
2020-03-27 11:31:19 +08:00
|
|
|
|
panic(err)
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2020-04-03 13:19:53 +08:00
|
|
|
|
func PathExists(path string) bool {
|
|
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
return true
|
2020-03-27 11:31:19 +08:00
|
|
|
|
}
|
2020-04-03 13:19:53 +08:00
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
2020-03-27 11:31:19 +08:00
|
|
|
|
}
|
2020-03-27 18:14:19 +08:00
|
|
|
|
func GetBetweenStr(str, start, end string) string {
|
|
|
|
|
|
n := strings.Index(str, start)
|
|
|
|
|
|
if n == -1 {
|
|
|
|
|
|
n = 0
|
|
|
|
|
|
} else {
|
|
|
|
|
|
n = n + len(start)
|
|
|
|
|
|
}
|
|
|
|
|
|
str = string([]byte(str)[n:])
|
|
|
|
|
|
m := strings.Index(str, end)
|
|
|
|
|
|
if m == -1 {
|
|
|
|
|
|
m = len(str)
|
|
|
|
|
|
}
|
|
|
|
|
|
str = string([]byte(str)[:m])
|
|
|
|
|
|
return str
|
|
|
|
|
|
}
|
|
|
|
|
|
func GetURLValue(Url, key string) string {
|
|
|
|
|
|
u, _ := url.Parse(Url)
|
|
|
|
|
|
query := u.Query()
|
2020-03-27 18:25:31 +08:00
|
|
|
|
//fmt.Println(query.Get(key))
|
|
|
|
|
|
return query.Get(key)
|
2020-03-27 18:14:19 +08:00
|
|
|
|
}
|
2020-03-28 10:21:01 +08:00
|
|
|
|
|
2020-03-28 17:16:23 +08:00
|
|
|
|
//返回一个32位md5加密后的字符串
|
|
|
|
|
|
func GetMD5Encode(data string) string {
|
|
|
|
|
|
h := md5.New()
|
|
|
|
|
|
h.Write([]byte(data))
|
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//返回一个16位md5加密后的字符串
|
|
|
|
|
|
func Get16MD5Encode(data string) string {
|
|
|
|
|
|
return GetMD5Encode(data)[8:24]
|
|
|
|
|
|
}
|
2020-04-05 21:01:26 +08:00
|
|
|
|
|
|
|
|
|
|
//只返回文件名
|
|
|
|
|
|
func GetPathFiles(path string) []string {
|
|
|
|
|
|
files, _ := ioutil.ReadDir(path)
|
|
|
|
|
|
var t []string
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
|
if file.IsDir() {
|
|
|
|
|
|
continue
|
|
|
|
|
|
} else {
|
|
|
|
|
|
t = append(t, file.Name())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return t
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//输入文件夹路径,返回最近n个log的路径,不到n个返回所有
|
|
|
|
|
|
func GetRecentLogs(path string, n int) []string {
|
|
|
|
|
|
var paths []string
|
|
|
|
|
|
if !PathExists(path) {
|
|
|
|
|
|
return paths
|
|
|
|
|
|
}
|
|
|
|
|
|
//path末尾检查/
|
|
|
|
|
|
if path[len(path)-1:] != "/" {
|
|
|
|
|
|
path += "/"
|
|
|
|
|
|
}
|
|
|
|
|
|
data := time.Now()
|
|
|
|
|
|
d, _ := time.ParseDuration("-24h")
|
|
|
|
|
|
//不到n个返回所有
|
|
|
|
|
|
nt := Min(n, len(GetPathFiles(path)))
|
|
|
|
|
|
//fmt.Println(nt)
|
|
|
|
|
|
for i := 1; i <= nt; {
|
|
|
|
|
|
if PathExists(path + data.Format("2006-01-02") + ".log") {
|
2020-04-07 10:57:03 +08:00
|
|
|
|
paths = append(paths, data.Format("2006-01-02")+".log")
|
2020-04-05 21:01:26 +08:00
|
|
|
|
i++
|
|
|
|
|
|
}
|
|
|
|
|
|
data = data.Add(d)
|
|
|
|
|
|
}
|
|
|
|
|
|
return paths
|
|
|
|
|
|
}
|