Skip to content

Commit

Permalink
feat: add custom tag/expr
Browse files Browse the repository at this point in the history
Signed-off-by: rfyiamcool <[email protected]>
  • Loading branch information
rfyiamcool authored and adhocore committed Mar 10, 2024
1 parent 91cb0b0 commit 15c9cff
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions gronx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gronx

import (
"errors"
"fmt"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -29,6 +30,23 @@ var expressions = map[string]string{
"@everysecond": "* * * * * *",
}

func AddTag(tag, expr string) error {
_, ok := expressions[tag]
if ok {
return errors.New("conflict tag")
}

segs, err := Segments(expr)
if err != nil {
return err
}
expr = strings.Join(segs, " ")

expressions[tag] = expr
fmt.Println(expr)
return nil
}

// SpaceRe is regex for whitespace.
var SpaceRe = regexp.MustCompile(`\s+`)
var yearRe = regexp.MustCompile(`\d{4}`)
Expand Down
23 changes: 23 additions & 0 deletions gronx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ func TestIsValid(t *testing.T) {

}

func TestAddTag(t *testing.T) {
t.Run("add good tag", func(t *testing.T) {
err := AddTag("@2s", "*/2 * * * * *")
if err != nil {
t.Error("expected nil, got err")
}
})

t.Run("add conflict tag", func(t *testing.T) {
err := AddTag("@2s", "*/2 * * * * *")
if err == nil {
t.Error("expected err, got nil")
}
})

t.Run("add wrong tag", func(t *testing.T) {
err := AddTag("@3s", "* * * *")
if err == nil {
t.Error("expected err, got nil")
}
})
}

func TestIsDue(t *testing.T) {
gron := New()

Expand Down

0 comments on commit 15c9cff

Please sign in to comment.