Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- id: lint-crontab
name: Lint crontab files
description: Lint crontab files for scheduling syntax
language: golang
types: [file]
files: \.crontab$
entry: supercronic -test
34 changes: 27 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ var Usage = func() {
flag.PrintDefaults()
}

func testCrontabFiles(fileNames []string) int {
exitCode := 0

for _, fileName := range fileNames {
logrus.Infof("read crontab: %s", fileName)
_, err := readCrontabAtPath(fileName)

if err != nil {
logrus.Error(err)
exitCode = 1
} else {
logrus.Infof("crontab is valid: %s", fileName)
}
}

return exitCode
}

func main() {
debug := flag.Bool("debug", false, "enable debug logging")
quiet := flag.Bool("quiet", false, "do not log informational messages (takes precedence over debug)")
Expand Down Expand Up @@ -77,12 +95,20 @@ func main() {
)
}

if flag.NArg() != 1 {
// Allow FILE... for test flag to use with linting
validArgs := flag.NArg() == 1 || (*test && flag.NArg() > 1)
if !validArgs {
Usage()
os.Exit(2)
return
}

if *test {
fileNames := flag.Args()
os.Exit(testCrontabFiles(fileNames))
return
}

crontabFileName := flag.Args()[0]

var sentryHook *logrus_sentry.SentryHook
Expand Down Expand Up @@ -131,12 +157,6 @@ func main() {
break
}

if *test {
logrus.Info("crontab is valid")
os.Exit(0)
break
}

var wg sync.WaitGroup
exitCtx, notifyExit := context.WithCancel(context.Background())

Expand Down