-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.go
More file actions
66 lines (62 loc) · 1.51 KB
/
utils.go
File metadata and controls
66 lines (62 loc) · 1.51 KB
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
package scriptcat
import (
"errors"
"regexp"
"strings"
)
func ParseMeta(script string) string {
return regexp.MustCompile(`// ==UserScript==([\\s\\S]+?)// ==/UserScript==`).FindString(script)
}
func ParseMetaToJson(meta string) map[string][]string {
reg := regexp.MustCompile("(?im)^//\\s*@(.+?)([\r\n]+|$|\\s+(.+?)$)")
list := reg.FindAllStringSubmatch(meta, -1)
ret := make(map[string][]string)
for _, v := range list {
v[1] = strings.ToLower(v[1])
if _, ok := ret[v[1]]; !ok {
ret[v[1]] = make([]string, 0)
}
ret[v[1]] = append(ret[v[1]], strings.TrimSpace(v[3]))
}
return ret
}
// ConvCron 转换cron表达式
func ConvCron(cron string) (string, error) {
// 对once进行处理
unit := strings.Split(cron, " ")
if len(unit) == 5 {
unit = append([]string{"0"}, unit...)
}
if len(unit) != 6 {
return "", errors.New("cron format error: " + cron)
}
for i, v := range unit {
if v == "once" {
// once -> *
unit[i] = "*"
for j := i - 1; j >= 0; j-- {
if unit[j] == "*" {
unit[j] = "0"
}
}
break
} else if strings.HasPrefix(v, "once(") && strings.HasSuffix(v, ")") {
// once(expr) -> expr
expr := v[len("once(") : len(v)-1]
if expr == "" {
return "", errors.New("once() expression cannot be empty: " + v)
}
unit[i] = expr
for j := i - 1; j >= 0; j-- {
if unit[j] == "*" {
unit[j] = "0"
}
}
break
} else if strings.Contains(v, "-") {
// 取最小的时间
unit[i] = strings.Split(v, "-")[0]
}
}
return strings.Join(unit, " "), nil
}