From f7c0ad860cd265659ce20217f1dd13be56d96d47 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Tue, 30 Jul 2024 21:03:54 +1000 Subject: [PATCH] fix: more supported cron values --- internal/helpers/helpers_cron.go | 21 +++++++++++++++++++++ internal/helpers/helpers_cron_test.go | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/internal/helpers/helpers_cron.go b/internal/helpers/helpers_cron.go index e83a6f51..54c0a97a 100644 --- a/internal/helpers/helpers_cron.go +++ b/internal/helpers/helpers_cron.go @@ -4,6 +4,7 @@ import ( "fmt" "math" "regexp" + "slices" "strconv" "strings" @@ -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) @@ -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) @@ -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, "-") diff --git a/internal/helpers/helpers_cron_test.go b/internal/helpers/helpers_cron_test.go index 1f69ff58..0137077d 100644 --- a/internal/helpers/helpers_cron_test.go +++ b/internal/helpers/helpers_cron_test.go @@ -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) {