Skip to content

Commit

Permalink
add alfred-cron workflow
Browse files Browse the repository at this point in the history
x1ah committed Nov 17, 2020

Verified

This commit was signed with the committer’s verified signature.
x1ah x1ah
1 parent a4f1455 commit d3c4af3
Showing 5 changed files with 116 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cmd/cron/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package main

import (
"fmt"
"time"

"github.com/robfig/cron/v3"
)

func main() {
println("hello cron")
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
s, err := parser.Parse("1 * * * *")
if err != nil {
panic(err)
}
curr := time.Now()
for i := 0; i <= 10; i++ {
curr = s.Next(curr)
fmt.Println(curr)
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/x1ah/alfred-cron

go 1.14

require (
github.com/deanishe/awgo v0.27.1
github.com/robfig/cron/v3 v3.0.1
)
41 changes: 41 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"strings"
"time"

aw "github.com/deanishe/awgo"
"github.com/robfig/cron/v3"
)

// AlfredCron alfred-cron struct
type AlfredCron struct {
wf *aw.Workflow
// how many times
repeated int
cronParser cron.Parser
}

// NewAlfredCron create new AlfredCron instance
func NewAlfredCron() *AlfredCron {
return &AlfredCron{
wf: aw.New(),
repeated: 7,
cronParser: cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow),
}
}

// 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()
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()
}

0 comments on commit d3c4af3

Please sign in to comment.