Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: more supported cron values #345

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading