From bd4bb9a9fbe51451cbfb6f5abd11169809f5d275 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 26 May 2022 09:15:49 +0200 Subject: [PATCH 1/2] feat: added cli command for previewing cron schedules --- src/console/schedule/schedule.v | 34 +++++++++++++++++++++++++++++++++ src/main.v | 2 ++ 2 files changed, 36 insertions(+) create mode 100644 src/console/schedule/schedule.v diff --git a/src/console/schedule/schedule.v b/src/console/schedule/schedule.v new file mode 100644 index 0000000..b086b53 --- /dev/null +++ b/src/console/schedule/schedule.v @@ -0,0 +1,34 @@ +module schedule + +import cli +import cron.expression { parse_expression } + +// cmd returns the cli submodule for previewing a cron schedule. +pub fn cmd() cli.Command { + return cli.Command{ + name: 'schedule' + description: 'Preview the behavior of a cron schedule.' + flags: [ + cli.Flag{ + name: 'count' + description: 'How many scheduled times to show.' + flag: cli.FlagType.int + default_value: ['5'] + }, + ] + execute: fn (cmd cli.Command) ? { + exp := parse_expression(cmd.args.join(' '))? + + mut t := exp.next_from_now()? + println(t) + + count := cmd.flags.get_int('count')? + + for _ in 1 .. count { + t = exp.next(t)? + + println(t) + } + } + } +} diff --git a/src/main.v b/src/main.v index 6df45dc..885e0f3 100644 --- a/src/main.v +++ b/src/main.v @@ -5,6 +5,7 @@ import server import cli import console.git import console.logs +import console.schedule import cron fn main() { @@ -27,6 +28,7 @@ fn main() { git.cmd(), cron.cmd(), logs.cmd(), + schedule.cmd(), ] } app.setup() From 768da5b7907e27282744aa25946325d7df2d79f8 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 26 May 2022 13:34:57 +0200 Subject: [PATCH 2/2] refactor: added CronExpression.next_n function --- src/console/schedule/schedule.v | 12 ++++-------- src/cron/expression/expression.v | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/console/schedule/schedule.v b/src/console/schedule/schedule.v index b086b53..8fceddd 100644 --- a/src/console/schedule/schedule.v +++ b/src/console/schedule/schedule.v @@ -2,11 +2,13 @@ module schedule import cli import cron.expression { parse_expression } +import time // cmd returns the cli submodule for previewing a cron schedule. pub fn cmd() cli.Command { return cli.Command{ name: 'schedule' + usage: 'schedule' description: 'Preview the behavior of a cron schedule.' flags: [ cli.Flag{ @@ -17,16 +19,10 @@ pub fn cmd() cli.Command { }, ] execute: fn (cmd cli.Command) ? { - exp := parse_expression(cmd.args.join(' '))? - - mut t := exp.next_from_now()? - println(t) - + ce := parse_expression(cmd.args.join(' '))? count := cmd.flags.get_int('count')? - for _ in 1 .. count { - t = exp.next(t)? - + for t in ce.next_n(time.now(), count)? { println(t) } } diff --git a/src/cron/expression/expression.v b/src/cron/expression/expression.v index 5eae332..17d2dde 100644 --- a/src/cron/expression/expression.v +++ b/src/cron/expression/expression.v @@ -121,6 +121,20 @@ pub fn (ce &CronExpression) next_from_now() ?time.Time { return ce.next(time.now()) } +// next_n returns the n next occurences of the expression, given a starting +// time. +pub fn (ce &CronExpression) next_n(ref time.Time, n int) ?[]time.Time { + mut times := []time.Time{cap: n} + + times << ce.next(ref)? + + for i in 1 .. n { + times << ce.next(times[i - 1])? + } + + return times +} + // parse_range parses a given string into a range of sorted integers, if // possible. fn parse_range(s string, min int, max int, mut bitv []bool) ? {