File: main.go

package info (click to toggle)
golang-github-adhocore-gronx 1.19.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 248 kB
  • sloc: makefile: 6
file content (65 lines) | stat: -rw-r--r-- 1,368 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main

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

	"github.com/adhocore/gronx/pkg/tasker"
)

var exit = os.Exit
var tick = time.Minute

var opt tasker.Option
var v bool

// Version of tasker, injected in build
var Version = "n/a"

func init() {
	flag.StringVar(&opt.File, "file", "", "The task file in crontab format (without user)")
	flag.StringVar(&opt.Tz, "tz", "Local", "The timezone to use for tasks")
	flag.StringVar(&opt.Shell, "shell", tasker.Shell()[0], "The shell to use for running tasks")
	flag.StringVar(&opt.Out, "out", "", "The fullpath to file where output from tasks are sent to")
	flag.BoolVar(&opt.Verbose, "verbose", false, "The verbose mode outputs as much as possible")
	flag.Int64Var(&opt.Until, "until", 0, "The timeout for task daemon in minutes")
	flag.BoolVar(&v, "v", false, "Show version")
}

func main() {
	mustParseOption()

	taskr := tasker.New(opt)
	for _, task := range tasker.MustParseTaskfile(opt) {
		taskr.Task(task.Expr, taskr.Taskify(task.Cmd, opt))
	}

	if opt.Until > 0 {
		taskr.Until(time.Duration(opt.Until) * tick)
	}

	taskr.Run()
}

func mustParseOption() {
	opt = tasker.Option{}
	flag.Parse()

	if v {
		fmt.Printf("v%s\n", Version)
		exit(0)
	}

	if opt.File == "" {
		flag.Usage()
		exit(1)
	}

	if _, err := os.Stat(opt.File); err != nil {
		log.Printf("can't read taskfile: %s", opt.File)
		exit(1)
	}
}