From 8ee700425b9b63cabd4db9c6beec2e952ff93d22 Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Mon, 9 Sep 2024 15:56:08 +1000 Subject: [PATCH] fix: support for cron with dual hour ranges (#70) --- utils/cron/cron.go | 10 ++++++++++ utils/cron/cron_test.go | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/utils/cron/cron.go b/utils/cron/cron.go index b9f632a..3629f4c 100644 --- a/utils/cron/cron.go +++ b/utils/cron/cron.go @@ -145,6 +145,16 @@ func ConvertCrontab(namespace, cron string) (string, error) { hours = val continue } + sVal := strings.Split(val, ",") + if len(sVal) > 1 { + fmt.Println(sVal) + for _, r := range sVal { + if isInRange(r, 0, 23) { + continue + } + } + hours = val + } // if the value is not valid, return an error with where the issue is if hours == "" { return "", fmt.Errorf("cron definition '%s' is invalid, unable to determine hours value", cron) diff --git a/utils/cron/cron_test.go b/utils/cron/cron_test.go index bc7b5aa..23a2f82 100644 --- a/utils/cron/cron_test.go +++ b/utils/cron/cron_test.go @@ -191,6 +191,14 @@ func TestConvertCrontab(t *testing.T) { wantErrMsg: "cron definition '*/1 * * * * 7' is invalid, 6 fields provided, required 5", wantErr: true, }, + { + name: "test22 - split range hours", + args: args{ + namespace: "example-com-main", + cron: "*/30 0-12,22-23 * * *", + }, + want: "1,31 0-12,22-23 * * *", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {