Skip to content

Commit 8ef4b16

Browse files
authored
Merge pull request #48 from winebarrel/add_crongrep
Add crongrep
2 parents 527c694 + e68bc41 commit 8ef4b16

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

.goreleaser.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ builds:
2323
- -X main.version={{.Version}}
2424
env:
2525
- CGO_ENABLED=0
26+
- id: crongrep
27+
binary: crongrep
28+
main: ./cmd/crongrep
29+
ldflags:
30+
- -X main.version={{.Version}}
31+
env:
32+
- CGO_ENABLED=0
2633
checksum:
2734
name_template: "checksums.txt"
2835
archives:
@@ -35,6 +42,9 @@ archives:
3542
- id: cronviz
3643
builds: [cronviz]
3744
name_template: "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
45+
- id: crongrep
46+
builds: [crongrep]
47+
name_template: "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
3848
brews:
3949
- name: cronplan
4050
ids: [cronplan]
@@ -66,6 +76,16 @@ brews:
6676
license: MIT
6777
install: |
6878
bin.install 'cronviz'
79+
- name: crongrep
80+
ids: [crongrep]
81+
tap:
82+
owner: winebarrel
83+
name: homebrew-cronplan
84+
homepage: https://github.com/winebarrel/cronplan
85+
description: crongrep is a tool to grep with cron expression.
86+
license: MIT
87+
install: |
88+
bin.install 'crongrep'
6989
nfpms:
7090
- id: cronplan-nfpms
7191
builds: [cronplan]
@@ -100,3 +120,14 @@ nfpms:
100120
- deb
101121
- rpm
102122
bindir: /usr/bin
123+
- id: crongrep-nfpms
124+
builds: [crongrep]
125+
file_name_template: "{{ .Binary }}_{{ .Version }}_{{ .Arch }}"
126+
homepage: https://github.com/winebarrel/cronplan
127+
maintainer: Genki Sugawara <sugawara@winebarrel.jp>
128+
description: crongrep is a tool to visualize cron schedule.
129+
license: MIT
130+
formats:
131+
- deb
132+
- rpm
133+
bindir: /usr/bin

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
N/A
66

7+
## [1.10.1] - 2023-10-01
8+
9+
### Added
10+
11+
* Add crongrep CLI.
12+
713
## [1.10.0] - 2023-10-01
814

915
### Added

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,35 @@ $ open output.html
215215

216216
cf. https://raw.githack.com/winebarrel/cronplan/main/_example/timeline.html
217217

218+
# crongrep CLI
219+
220+
CLI to grep with cron expression.
221+
222+
## Installation
223+
224+
```
225+
brew install winebarrel/cronplan/crongrep
226+
```
227+
228+
## Usage
229+
230+
```
231+
Usage: crongrep [OPTION] CRON_EXPR
232+
-version
233+
print version and exit
234+
```
235+
236+
```
237+
$ for i in {1..5}; do
238+
LANG=C date
239+
done | crongrep '0 * * * ? *'
240+
Sun Oct 1 21:00:00 JST 2023
241+
Sun Oct 1 21:00:00 JST 2023
242+
Sun Oct 1 21:00:00 JST 2023
243+
Sun Oct 1 21:00:00 JST 2023
244+
Sun Oct 1 21:00:00 JST 2023
245+
```
246+
218247
## Related Links
219248

220249
* [Cron expressions reference - Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-cron-expressions.html)

cmd/crongrep/flags.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
var (
12+
version string
13+
)
14+
15+
type flags struct {
16+
expr string
17+
}
18+
19+
func init() {
20+
cmdLine := flag.NewFlagSet(filepath.Base(os.Args[0]), flag.ExitOnError)
21+
22+
cmdLine.Usage = func() {
23+
fmt.Fprintf(cmdLine.Output(), "Usage: %s [OPTION] CRON_EXPR\n", cmdLine.Name())
24+
cmdLine.PrintDefaults()
25+
}
26+
27+
flag.CommandLine = cmdLine
28+
}
29+
30+
func parseFlags() *flags {
31+
flags := &flags{}
32+
showVersion := flag.Bool("version", false, "print version and exit")
33+
flag.Parse()
34+
35+
if *showVersion {
36+
printVersionAndExit()
37+
}
38+
39+
args := flag.Args()
40+
41+
if len(args) != 1 {
42+
printUsageAndExit()
43+
}
44+
45+
flags.expr = strings.TrimSpace(args[0])
46+
47+
return flags
48+
}
49+
50+
func printVersionAndExit() {
51+
v := version
52+
53+
if v == "" {
54+
v = "<nil>"
55+
}
56+
57+
fmt.Fprintln(flag.CommandLine.Output(), v)
58+
os.Exit(0)
59+
}
60+
61+
func printUsageAndExit() {
62+
flag.CommandLine.Usage()
63+
os.Exit(0)
64+
}

cmd/crongrep/main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"github.com/araddon/dateparse"
10+
"github.com/winebarrel/cronplan"
11+
)
12+
13+
func init() {
14+
log.SetFlags(0)
15+
}
16+
17+
func main() {
18+
flags := parseFlags()
19+
cron, err := cronplan.Parse(flags.expr)
20+
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
25+
scanner := bufio.NewScanner(os.Stdin)
26+
27+
for scanner.Scan() {
28+
line := scanner.Text()
29+
t, err := dateparse.ParseAny(line)
30+
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
35+
if cron.Match(t) {
36+
fmt.Println(line)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)