Skip to content

Commit

Permalink
fix: more supported cron values (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Jul 30, 2024
1 parent 86b7de6 commit 367f68d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/helpers/helpers_cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"
"regexp"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -180,6 +181,10 @@ func ConvertCrontab(namespace, cron string) (string, error) {
months = val
continue
}
if isMonth(val) {
months = val
continue
}
// if the value is not valid, return an error with where the issue is
if months == "" {
return "", fmt.Errorf("cron definition '%s' is invalid, unable to determine months value", cron)
Expand All @@ -198,6 +203,10 @@ func ConvertCrontab(namespace, cron string) (string, error) {
dayweek = val
continue
}
if isDayOfWeek(val) {
dayweek = val
continue
}
// if the value is not valid, return an error with where the issue is
if dayweek == "" {
return "", fmt.Errorf("cron definition '%s' is invalid, unable to determine day(week) value", cron)
Expand Down Expand Up @@ -268,6 +277,18 @@ func isInCSVRange(s string, min, max int) bool {
return true
}

// check if the provided cron day string is a valid
func isDayOfWeek(s string) bool {
days := []string{"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}
return slices.Contains(days, strings.ToUpper(s))
}

// check if the provided cron month string is a valid
func isMonth(s string) bool {
days := []string{"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}
return slices.Contains(days, strings.ToUpper(s))
}

// check if the provided cron time definition is a valid `1-2` type range
func isInRange(s string, min, max int) bool {
items := strings.Split(s, "-")
Expand Down
8 changes: 8 additions & 0 deletions internal/helpers/helpers_cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ func TestConvertCrontab(t *testing.T) {
},
want: "31 1,7,13,19 * * *",
},
{
name: "test18 - day and month string",
args: args{
namespace: "example-com-main",
cron: "M */6 * JAN MON",
},
want: "31 1,7,13,19 * JAN MON",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 367f68d

Please sign in to comment.