-
Notifications
You must be signed in to change notification settings - Fork 374
/
extparser.go
53 lines (43 loc) · 1.38 KB
/
extparser.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
package extcron
import (
"fmt"
"strings"
"time"
"github.com/robfig/cron/v3"
)
// ExtParser is a parser extending robfig/cron v3 standard parser with
// several additional descriptors
type ExtParser struct {
parser cron.Parser
}
// NewParser creates an ExtParser instance
func NewParser() cron.ScheduleParser {
return ExtParser{cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)}
}
// Parse parses a cron schedule specification. It accepts the cron spec with
// mandatory seconds parameter, descriptors and the custom descriptors
// "@at <date>", "@manually" and "@minutely".
func (p ExtParser) Parse(spec string) (cron.Schedule, error) {
switch spec {
case "@manually":
return At(time.Time{}), nil
case "@minutely":
spec = "0 * * * * *"
}
const at = "@at "
if strings.HasPrefix(spec, at) {
date, err := time.Parse(time.RFC3339, spec[len(at):])
if err != nil {
return nil, fmt.Errorf("failed to parse date %s: %s", spec, err)
}
return At(date), nil
}
// It's not a dkron specific spec: Let the regular cron schedule parser have it
return p.parser.Parse(spec)
}
var standaloneParser = NewParser()
// Parse parses a cron schedule. This is a convenience function to not have
// to instantiate a parser with NewParser for every call.
func Parse(spec string) (cron.Schedule, error) {
return standaloneParser.Parse(spec)
}