generated from xiachufang/go-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
60 lines (53 loc) · 1.32 KB
/
main.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
package main
import (
"strings"
"time"
aw "github.com/deanishe/awgo"
crondesc "github.com/lnquy/cron"
"github.com/robfig/cron/v3"
)
// AlfredCron alfred-cron struct
type AlfredCron struct {
wf *aw.Workflow
// how many times
repeated int
cronParser cron.Parser
descr *crondesc.ExpressionDescriptor
}
// NewAlfredCron create new AlfredCron instance
func NewAlfredCron() *AlfredCron {
descr, _ := crondesc.NewDescriptor()
return &AlfredCron{
wf: aw.New(),
repeated: 7,
cronParser: cron.NewParser(cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow),
descr: descr,
}
}
// Run run alfred-cron workflow
func (ac *AlfredCron) Run() {
spec := strings.Join(ac.wf.Args(), "")
if spec == "" {
ac.wf.NewItem("no crontab expression found.").Subtitle("Try input a crontab expression?")
ac.wf.SendFeedback()
return
}
expr, err := ac.cronParser.Parse(spec)
if err != nil {
ac.wf.NewWarningItem("Invalid expression", err.Error())
} else {
curr := time.Now()
if description, err := ac.descr.ToDescription(spec, crondesc.Locale_en); err == nil {
ac.wf.NewItem(description)
}
for i := 0; i < ac.repeated; i++ {
curr = expr.Next(curr)
ac.wf.NewItem(curr.String())
}
}
ac.wf.SendFeedback()
}
func main() {
alfredCron := NewAlfredCron()
alfredCron.Run()
}