From 154eff212d277ceb293eec08a2a2152df8626fdf Mon Sep 17 00:00:00 2001 From: Sergey Grebenshchikov Date: Fri, 10 Jul 2020 12:43:46 +0200 Subject: [PATCH] Add --print-next-match-and-exit and --format --- README.md | 12 ++++++++++-- README.template.md | 8 ++++++-- main.go | 13 +++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fb206e1..a1682bc 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,7 @@ A tiny tool that waits until a given cron expression would trigger, and then jus ![image](doc/screenshot.png) -## Example - +## Examples ```sh $ wait-for-cron-expression-match "*/5 * * * *" @@ -13,6 +12,11 @@ $ wait-for-cron-expression-match "*/5 * * * *" [wait-for-cron-expression-match] 2020/07/10 12:00:00.966919 done ``` +```sh +$ wait-for-cron-expression-match -print-next-match-and-exit "*/3 * * * *" +2020-07-10T12:48:00+02:00 +``` + ## Contents - [Get it](#get-it) @@ -36,6 +40,10 @@ wait-for-cron-expression-match [OPTIONS] [CRON_EXPRESSION [CRON_EXPRESSIONS...]] Usage of wait-for-cron-expression-match: -dots Print dots to stdout while waiting + -format string + Timestamp format (default "2006-01-02T15:04:05Z07:00") + -print-next-match-and-exit + Only print the timestamp of the next expression match and exit (without waiting) -q (alias for -quiet) -quiet Suppress all output diff --git a/README.template.md b/README.template.md index cd7e3f2..f93c539 100644 --- a/README.template.md +++ b/README.template.md @@ -4,8 +4,7 @@ A tiny tool that waits until a given cron expression would trigger, and then jus ![image](doc/screenshot.png) -## Example - +## Examples ```sh $ ${APP} "*/5 * * * *" @@ -13,6 +12,11 @@ $ ${APP} "*/5 * * * *" [${APP}] 2020/07/10 12:00:00.966919 done ``` +```sh +$ ${APP} -print-next-match-and-exit "*/3 * * * *" +2020-07-10T12:48:00+02:00 +``` + ## Contents - [Get it](#get-it) diff --git a/main.go b/main.go index 3764272..ec2828d 100644 --- a/main.go +++ b/main.go @@ -11,8 +11,10 @@ import ( ) var config struct { - Quiet bool - PrintDots bool + Quiet bool + PrintDots bool + PrintNextTimestampAndExit bool + TimestampFormat string cronExpressions []string schedules []cron.Schedule @@ -24,6 +26,9 @@ var app = "wait-for-cron-expression-match" func init() { log.SetFlags(log.Ltime | log.Ldate | log.Lmicroseconds) log.SetPrefix(fmt.Sprintf("[%s] ", app)) + config.TimestampFormat = time.RFC3339 + flag.BoolVar(&config.PrintNextTimestampAndExit, "print-next-match-and-exit", config.PrintNextTimestampAndExit, "Only print the timestamp of the next expression match and exit (without waiting)") + flag.StringVar(&config.TimestampFormat, "format", config.TimestampFormat, "Timestamp format") flag.BoolVar(&config.Quiet, "quiet", config.Quiet, "Suppress all output") flag.BoolVar(&config.Quiet, "q", config.Quiet, "(alias for -quiet)") flag.BoolVar(&config.PrintDots, "dots", config.PrintDots, "Print dots to stdout while waiting") @@ -60,6 +65,10 @@ func main() { next = nextNew } } + if config.PrintNextTimestampAndExit { + fmt.Println(next.Format(config.TimestampFormat)) + return + } delta := next.Sub(now) if delta < tickInterval { tickInterval = delta