Skip to content

Commit

Permalink
Add --print-next-match-and-exit and --format
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Jul 10, 2020
1 parent c60bba9 commit 154eff2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ 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 * * * *"
[wait-for-cron-expression-match] 2020/07/10 11:59:29.965045 waiting 30.035156s until next match (2020-07-10T12:00:00+02:00) of cron expression ["*/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)
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ A tiny tool that waits until a given cron expression would trigger, and then jus

![image](doc/screenshot.png)

## Example

## Examples

```sh
$ ${APP} "*/5 * * * *"
[${APP}] 2020/07/10 11:59:29.965045 waiting 30.035156s until next match (2020-07-10T12:00:00+02:00) of cron expression ["*/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)
Expand Down
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 154eff2

Please sign in to comment.