Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Jul 9, 2020
0 parents commit abca67d
Show file tree
Hide file tree
Showing 18 changed files with 1,236 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
VERSION := $(shell jq -r .version <package.json)

APP := $(shell jq -r .name <package.json)
PACKAGES := $(shell go list -f {{.Dir}} ./...)
GOFILES := $(addsuffix /*.go,$(PACKAGES))
GOFILES := $(wildcard $(GOFILES))

.PHONY: clean release README.md

clean:
rm -rf binaries/
rm -rf release/

release-exists:
hub release show "$(VERSION)"

release: README.md zip
git add package.json
git add README.md
git add Makefile
git commit -am "Release $(VERSION)" || true
git push
hub release create $(VERSION) -m "$(VERSION)" -a release/$(APP)_$(VERSION)_osx_x86_64.tar.gz -a release/$(APP)_$(VERSION)_windows_x86_64.zip -a release/$(APP)_$(VERSION)_linux_x86_64.tar.gz -a release/$(APP)_$(VERSION)_osx_x86_32.tar.gz -a release/$(APP)_$(VERSION)_windows_x86_32.zip -a release/$(APP)_$(VERSION)_linux_x86_32.tar.gz -a release/$(APP)_$(VERSION)_linux_arm64.tar.gz

README.md:
go get github.com/keilerkonzept/$(APP) && <README.template.md subst \
VERSION="$(VERSION)" APP="$(APP)" USAGE="$$($(APP) -h 2>&1)" > README.md

zip: release/$(APP)_$(VERSION)_osx_x86_64.tar.gz release/$(APP)_$(VERSION)_windows_x86_64.zip release/$(APP)_$(VERSION)_linux_x86_64.tar.gz release/$(APP)_$(VERSION)_osx_x86_32.tar.gz release/$(APP)_$(VERSION)_windows_x86_32.zip release/$(APP)_$(VERSION)_linux_x86_32.tar.gz release/$(APP)_$(VERSION)_linux_arm64.tar.gz

binaries: binaries/osx_x86_64/$(APP) binaries/windows_x86_64/$(APP).exe binaries/linux_x86_64/$(APP) binaries/osx_x86_32/$(APP) binaries/windows_x86_32/$(APP).exe binaries/linux_x86_32/$(APP)

release/$(APP)_$(VERSION)_osx_x86_64.tar.gz: binaries/osx_x86_64/$(APP)
mkdir -p release
tar cfz release/$(APP)_$(VERSION)_osx_x86_64.tar.gz -C binaries/osx_x86_64 $(APP)

binaries/osx_x86_64/$(APP): $(GOFILES)
GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION) -X main.app=$(APP)" -o binaries/osx_x86_64/$(APP) .

release/$(APP)_$(VERSION)_windows_x86_64.zip: binaries/windows_x86_64/$(APP).exe
mkdir -p release
cd ./binaries/windows_x86_64 && zip -r -D ../../release/$(APP)_$(VERSION)_windows_x86_64.zip $(APP).exe

binaries/windows_x86_64/$(APP).exe: $(GOFILES)
GOOS=windows GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION) -X main.app=$(APP)" -o binaries/windows_x86_64/$(APP).exe .

release/$(APP)_$(VERSION)_linux_x86_64.tar.gz: binaries/linux_x86_64/$(APP)
mkdir -p release
tar cfz release/$(APP)_$(VERSION)_linux_x86_64.tar.gz -C binaries/linux_x86_64 $(APP)

binaries/linux_x86_64/$(APP): $(GOFILES)
GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION) -X main.app=$(APP)" -o binaries/linux_x86_64/$(APP) .

release/$(APP)_$(VERSION)_osx_x86_32.tar.gz: binaries/osx_x86_32/$(APP)
mkdir -p release
tar cfz release/$(APP)_$(VERSION)_osx_x86_32.tar.gz -C binaries/osx_x86_32 $(APP)

binaries/osx_x86_32/$(APP): $(GOFILES)
GOOS=darwin GOARCH=386 go build -ldflags "-X main.version=$(VERSION) -X main.app=$(APP)" -o binaries/osx_x86_32/$(APP) .

release/$(APP)_$(VERSION)_windows_x86_32.zip: binaries/windows_x86_32/$(APP).exe
mkdir -p release
cd ./binaries/windows_x86_32 && zip -r -D ../../release/$(APP)_$(VERSION)_windows_x86_32.zip $(APP).exe

binaries/windows_x86_32/$(APP).exe: $(GOFILES)
GOOS=windows GOARCH=386 go build -ldflags "-X main.version=$(VERSION) -X main.app=$(APP)" -o binaries/windows_x86_32/$(APP).exe .

release/$(APP)_$(VERSION)_linux_x86_32.tar.gz: binaries/linux_x86_32/$(APP)
mkdir -p release
tar cfz release/$(APP)_$(VERSION)_linux_x86_32.tar.gz -C binaries/linux_x86_32 $(APP)

binaries/linux_x86_32/$(APP): $(GOFILES)
GOOS=linux GOARCH=386 go build -ldflags "-X main.version=$(VERSION) -X main.app=$(APP)" -o binaries/linux_x86_32/$(APP) .

release/$(APP)_$(VERSION)_linux_arm64.tar.gz: binaries/linux_arm64/$(APP)
mkdir -p release
tar cfz release/$(APP)_$(VERSION)_linux_arm64.tar.gz -C binaries/linux_arm64 $(APP)

binaries/linux_arm64/$(APP): $(GOFILES)
GOOS=linux GOARCH=arm64 go build -ldflags "-X main.version=$(VERSION) -X main.app=$(APP)" -o binaries/linux_arm64/$(APP) .
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# wait-for-cron-expression-match

A tiny tool that waits until a given cron expression would trigger, and then just exits. If multiple expressions are given, it waits until the _first_ (earliest) match.

## Example


```sh
$ wait-for-cron-expression-match "*/5 * * * * *"
2020/07/08 17:57:17 waiting 2.998536s until next match (2020-07-08T17:57:20+02:00) of cron expression "*/5 * * * * *"
$ # 2.998s later
```
## Contents
- [Get it](#get-it)
- [Usage](#usage)
## Get it
Using go get:
```bash
go get -u github.com/keilerkonzept/wait-for-cron-expression-match
```
Or [download the binary for your platform](https://github.com/keilerkonzept/wait-for-cron-expression-match/releases/latest) from the releases page.
## Usage
```text
wait-for-cron-expression-match [CRON_EXPRESSION [CRON_EXPRESSIONS...]]
```
33 changes: 33 additions & 0 deletions README.template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ${APP}

A tiny tool that waits until a given cron expression would trigger, and then just exits. If multiple expressions are given, it waits until the _first_ (earliest) match.

## Example


```sh
$ ${APP} "*/5 * * * * *"
2020/07/08 17:57:17 waiting 2.998536s until next match (2020-07-08T17:57:20+02:00) of cron expression "*/5 * * * * *"
$ # 2.998s later
```
## Contents
- [Get it](#get-it)
- [Usage](#usage)
## Get it
Using go get:
```bash
go get -u github.com/keilerkonzept/${APP}
```
Or [download the binary for your platform](https://github.com/keilerkonzept/${APP}/releases/latest) from the releases page.
## Usage
```text
${APP} [CRON_EXPRESSION [CRON_EXPRESSIONS...]]
```
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/keilerkonzept/wait-for-cron-expression-match

go 1.14

require github.com/robfig/cron v1.2.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
67 changes: 67 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"flag"
"fmt"
"log"
"time"

"github.com/robfig/cron"
)

var config struct {
cronExpressions []string
schedules []cron.Schedule
evaluationInterval time.Duration
}

var app = "wait-for-cron-expression-match"

func init() {
log.SetFlags(log.Ltime | log.Ldate | log.Lmicroseconds)
log.SetPrefix(fmt.Sprintf("[%s] ", app))
flag.Parse()

config.evaluationInterval = 250 * time.Millisecond
config.cronExpressions = flag.Args()

if len(config.cronExpressions) == 0 {
log.Fatal("at least one expression is required")
}
for _, e := range config.cronExpressions {
schedule, err := cron.Parse(e)
if err != nil {
log.Fatalf("parse cron expression %q: %v", e, err)
}
config.schedules = append(config.schedules, schedule)
}
}

func main() {
now := time.Now()
tickInterval := config.evaluationInterval
var next time.Time
for i, s := range config.schedules {
nextNew := s.Next(now)
if i == 0 || nextNew.Before(next) {
next = nextNew
}
}
delta := next.Sub(now)
if delta < tickInterval {
tickInterval = delta
}
tick := time.Tick(tickInterval)

plural := ""
if len(config.cronExpressions) > 1 {
plural = "s"
}
log.Printf("waiting %v until next match (%v) of cron expression%s %q", delta, next.Format(time.RFC3339Nano), plural, config.cronExpressions)
defer log.Print("done")
for now = range tick {
if now.After(next) {
return
}
}
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "wait-for-cron-expression-match",
"version": "0.0.1"
}
6 changes: 6 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [
"config:base"
],
"automerge": true
}
22 changes: 22 additions & 0 deletions vendor/github.com/robfig/cron/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/robfig/cron/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/robfig/cron/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/robfig/cron/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/robfig/cron/constantdelay.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit abca67d

Please sign in to comment.