Skip to content

Commit

Permalink
Merge pull request #48 from winebarrel/add_crongrep
Browse files Browse the repository at this point in the history
Add crongrep
  • Loading branch information
winebarrel authored Oct 1, 2023
2 parents 527c694 + e68bc41 commit 8ef4b16
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ builds:
- -X main.version={{.Version}}
env:
- CGO_ENABLED=0
- id: crongrep
binary: crongrep
main: ./cmd/crongrep
ldflags:
- -X main.version={{.Version}}
env:
- CGO_ENABLED=0
checksum:
name_template: "checksums.txt"
archives:
Expand All @@ -35,6 +42,9 @@ archives:
- id: cronviz
builds: [cronviz]
name_template: "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
- id: crongrep
builds: [crongrep]
name_template: "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
brews:
- name: cronplan
ids: [cronplan]
Expand Down Expand Up @@ -66,6 +76,16 @@ brews:
license: MIT
install: |
bin.install 'cronviz'
- name: crongrep
ids: [crongrep]
tap:
owner: winebarrel
name: homebrew-cronplan
homepage: https://github.com/winebarrel/cronplan
description: crongrep is a tool to grep with cron expression.
license: MIT
install: |
bin.install 'crongrep'
nfpms:
- id: cronplan-nfpms
builds: [cronplan]
Expand Down Expand Up @@ -100,3 +120,14 @@ nfpms:
- deb
- rpm
bindir: /usr/bin
- id: crongrep-nfpms
builds: [crongrep]
file_name_template: "{{ .Binary }}_{{ .Version }}_{{ .Arch }}"
homepage: https://github.com/winebarrel/cronplan
maintainer: Genki Sugawara <[email protected]>
description: crongrep is a tool to visualize cron schedule.
license: MIT
formats:
- deb
- rpm
bindir: /usr/bin
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

N/A

## [1.10.1] - 2023-10-01

### Added

* Add crongrep CLI.

## [1.10.0] - 2023-10-01

### Added
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,35 @@ $ open output.html

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

# crongrep CLI

CLI to grep with cron expression.

## Installation

```
brew install winebarrel/cronplan/crongrep
```

## Usage

```
Usage: crongrep [OPTION] CRON_EXPR
-version
print version and exit
```

```
$ for i in {1..5}; do
LANG=C date
done | crongrep '0 * * * ? *'
Sun Oct 1 21:00:00 JST 2023
Sun Oct 1 21:00:00 JST 2023
Sun Oct 1 21:00:00 JST 2023
Sun Oct 1 21:00:00 JST 2023
Sun Oct 1 21:00:00 JST 2023
```

## Related Links

* [Cron expressions reference - Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-cron-expressions.html)
Expand Down
64 changes: 64 additions & 0 deletions cmd/crongrep/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)

var (
version string
)

type flags struct {
expr string
}

func init() {
cmdLine := flag.NewFlagSet(filepath.Base(os.Args[0]), flag.ExitOnError)

cmdLine.Usage = func() {
fmt.Fprintf(cmdLine.Output(), "Usage: %s [OPTION] CRON_EXPR\n", cmdLine.Name())
cmdLine.PrintDefaults()
}

flag.CommandLine = cmdLine
}

func parseFlags() *flags {
flags := &flags{}
showVersion := flag.Bool("version", false, "print version and exit")
flag.Parse()

if *showVersion {
printVersionAndExit()
}

args := flag.Args()

if len(args) != 1 {
printUsageAndExit()
}

flags.expr = strings.TrimSpace(args[0])

return flags
}

func printVersionAndExit() {
v := version

if v == "" {
v = "<nil>"
}

fmt.Fprintln(flag.CommandLine.Output(), v)
os.Exit(0)
}

func printUsageAndExit() {
flag.CommandLine.Usage()
os.Exit(0)
}
39 changes: 39 additions & 0 deletions cmd/crongrep/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"bufio"
"fmt"
"log"
"os"

"github.com/araddon/dateparse"
"github.com/winebarrel/cronplan"
)

func init() {
log.SetFlags(0)
}

func main() {
flags := parseFlags()
cron, err := cronplan.Parse(flags.expr)

if err != nil {
log.Fatal(err)
}

scanner := bufio.NewScanner(os.Stdin)

for scanner.Scan() {
line := scanner.Text()
t, err := dateparse.ParseAny(line)

if err != nil {
log.Fatal(err)
}

if cron.Match(t) {
fmt.Println(line)
}
}
}

0 comments on commit 8ef4b16

Please sign in to comment.