todo: crontab upgrade
This commit is contained in:
parent
7cf976d94a
commit
ef1b483b5f
1
crontab.go
Normal file
1
crontab.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package curl2info
|
139
crontab_test.go
Normal file
139
crontab_test.go
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
package curl2info
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TimePointer struct {
|
||||||
|
left, right int
|
||||||
|
leftlimit, rightlimit int
|
||||||
|
per int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tp *TimePointer) String() string {
|
||||||
|
return fmt.Sprintf("left: %d, right: %d, leftlimit: %d, rightlimit: %d, per: %d", tp.left, tp.right, tp.leftlimit, tp.rightlimit, tp.per)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Crontab struct {
|
||||||
|
Min []TimePointer
|
||||||
|
Hour []TimePointer
|
||||||
|
Day []TimePointer
|
||||||
|
Month []TimePointer
|
||||||
|
Week []TimePointer
|
||||||
|
|
||||||
|
logMissedTime bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCrontab(crontab string) *Crontab {
|
||||||
|
cron := &Crontab{}
|
||||||
|
cron.FromString(crontab)
|
||||||
|
return cron
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cron *Crontab) SetMissedLog(missedlog bool) {
|
||||||
|
cron.logMissedTime = missedlog
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cron *Crontab) TimeUp() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cron *Crontab) String() string {
|
||||||
|
return fmt.Sprintf("min:%s\nhour:%s\nday:%s\nmonth:%s\nweek:%s\n", spew.Sdump(cron.Min), spew.Sdump(cron.Hour), spew.Sdump(cron.Day), spew.Sdump(cron.Month), spew.Sdump(cron.Week))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cron *Crontab) FromString(crontab string) error {
|
||||||
|
crontab = strings.TrimSpace(crontab)
|
||||||
|
|
||||||
|
matches := regexp.MustCompile("[^ ]+").FindAllString(crontab, -1)
|
||||||
|
mlen := len(matches)
|
||||||
|
switch mlen {
|
||||||
|
case 5:
|
||||||
|
cron.Min = createTimePointer(matches[0], 0, 59)
|
||||||
|
cron.Hour = createTimePointer(matches[1], 0, 23)
|
||||||
|
cron.Day = createTimePointer(matches[2], 1, 31)
|
||||||
|
cron.Month = createTimePointer(matches[3], 1, 12)
|
||||||
|
cron.Week = createTimePointer(matches[4], 1, 7)
|
||||||
|
default:
|
||||||
|
return errors.New("mathches len != 5, check crontab string")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTimePointer(min string, llimit, rlimit int) []TimePointer {
|
||||||
|
|
||||||
|
var result []TimePointer
|
||||||
|
|
||||||
|
exelist := strings.Split(min, ",")
|
||||||
|
for _, exe := range exelist {
|
||||||
|
tp := TimePointer{}
|
||||||
|
|
||||||
|
takeper := strings.Split(exe, "/") // per
|
||||||
|
var rangevalue, per string
|
||||||
|
if len(takeper) == 1 {
|
||||||
|
rangevalue = exe
|
||||||
|
per = "0"
|
||||||
|
} else {
|
||||||
|
rangevalue = takeper[0]
|
||||||
|
per = takeper[1]
|
||||||
|
}
|
||||||
|
// takeRange
|
||||||
|
be := strings.Split(rangevalue, "-")
|
||||||
|
var left, rigth string
|
||||||
|
switch len(be) {
|
||||||
|
case 1:
|
||||||
|
left = be[0]
|
||||||
|
rigth = be[0]
|
||||||
|
case 2:
|
||||||
|
left = be[0]
|
||||||
|
rigth = be[1]
|
||||||
|
default:
|
||||||
|
panic(errors.New("range value is > 2"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if left == "*" {
|
||||||
|
tp.left = llimit
|
||||||
|
} else {
|
||||||
|
ileft, err := strconv.Atoi(strings.Replace(left, "^", "-", -1))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
tp.left = ileft
|
||||||
|
}
|
||||||
|
|
||||||
|
if rigth == "*" {
|
||||||
|
tp.right = rlimit
|
||||||
|
} else {
|
||||||
|
iright, err := strconv.Atoi(strings.Replace(rigth, "^", "-", -1))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
tp.right = iright
|
||||||
|
}
|
||||||
|
|
||||||
|
iper, err := strconv.Atoi(per)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
tp.per = iper
|
||||||
|
tp.leftlimit = llimit
|
||||||
|
tp.rightlimit = rlimit
|
||||||
|
|
||||||
|
result = append(result, tp)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseCrontab(t *testing.T) {
|
||||||
|
crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
|
||||||
|
|
||||||
|
t.Error(NewCrontab(crontab))
|
||||||
|
}
|
@ -30,10 +30,12 @@ func init() {
|
|||||||
//"--"
|
//"--"
|
||||||
{"--header", 10, parseHeader, nil},
|
{"--header", 10, parseHeader, nil},
|
||||||
{"--insecure", 15, parseInsecure, nil},
|
{"--insecure", 15, parseInsecure, nil},
|
||||||
{"--task", 10, parseITask, &extract{re: "--task +(.+)", execute: extractData}},
|
|
||||||
{"--user-agent", 15, parseUserAgent, &extract{re: "--user-agent +(.+)", execute: extractData}},
|
{"--user-agent", 15, parseUserAgent, &extract{re: "--user-agent +(.+)", execute: extractData}},
|
||||||
{"--user", 15, parseUser, &extract{re: "--user +(.+)", execute: extractData}},
|
{"--user", 15, parseUser, &extract{re: "--user +(.+)", execute: extractData}},
|
||||||
{"--connect-timeout", 15, parseTimeout, &extract{re: "--connect-timeout +(.+)", execute: extractData}},
|
{"--connect-timeout", 15, parseTimeout, &extract{re: "--connect-timeout +(.+)", execute: extractData}},
|
||||||
|
// 自定义
|
||||||
|
{"--task", 10, parseITask, &extract{re: "--task +(.+)", execute: extractData}},
|
||||||
|
{"--crontab", 10, parseCrontab, &extract{re: "--crontab +(.+)", execute: extractData}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, oe := range oelist {
|
for _, oe := range oelist {
|
||||||
@ -93,6 +95,10 @@ func extractData(re, soption string) string {
|
|||||||
return strings.Trim(datas[1], "'")
|
return strings.Trim(datas[1], "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseCrontab(u *CURL, value string) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func parseITask(u *CURL, value string) {
|
func parseITask(u *CURL, value string) {
|
||||||
u.ITask = value
|
u.ITask = value
|
||||||
}
|
}
|
||||||
|
@ -117,7 +117,14 @@ func ParseRawCURL(scurl string) (cURL *CURL, err error) {
|
|||||||
scurl = strings.TrimSpace(scurl)
|
scurl = strings.TrimSpace(scurl)
|
||||||
scurl = strings.TrimLeft(scurl, "curl")
|
scurl = strings.TrimLeft(scurl, "curl")
|
||||||
|
|
||||||
mathches := regexp.MustCompile(`--[^ ]+ +'[^']+'|--[^ ]+ +[^ ]+|-[A-Za-z] +'[^']+'|-[A-Za-z] +[^ ]+| '[^']+'|--[a-z]+ {0,}`).FindAllString(scurl, -1)
|
mathches := regexp.MustCompile(
|
||||||
|
`--[^ ]+ +'[^']+'|`+
|
||||||
|
`--[^ ]+ +[^ ]+|`+
|
||||||
|
`-[A-Za-z] +'[^']+'|`+
|
||||||
|
`-[A-Za-z] +[^ ]+|`+
|
||||||
|
` '[^']+'|`+
|
||||||
|
`--[a-z]+ {0,}`,
|
||||||
|
).FindAllString(scurl, -1)
|
||||||
for _, m := range mathches {
|
for _, m := range mathches {
|
||||||
m = strings.TrimSpace(m)
|
m = strings.TrimSpace(m)
|
||||||
switch v := m[0]; v {
|
switch v := m[0]; v {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user