-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
76 lines (63 loc) · 1.37 KB
/
main.go
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
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"os"
"os/exec"
"time"
"github.com/moov-io/base"
"github.com/moov-io/base/log"
)
func main() {
logger := log.NewDefaultLogger()
location := time.UTC
if ll := os.Getenv("TZ"); ll != "" {
if loc, err := time.LoadLocation(ll); err != nil {
logger.Error().LogErrorf("unable to load location: %w", err)
os.Exit(1)
} else {
location = loc
}
}
logger = logger.Info().With(log.Fields{
"location": log.String(location.String()),
})
logger.Logf("starting bankcron %s", Version)
rr := &runner{
logger: logger,
now: base.Now(location),
args: os.Args[1:],
}
output, err := rr.run()
if err != nil {
os.Exit(1)
}
logger.Log(string(output))
}
type runner struct {
logger log.Logger
now base.Time
args []string
}
func (r *runner) run() ([]byte, error) {
if !r.now.IsBankingDay() {
r.logger.Info().Log("skipping since it's not a banking day")
return nil, nil
}
var name string
var args []string
if len(r.args) < 1 {
return nil, r.logger.Error().LogErrorf("no command specified").Err()
} else {
name = r.args[0]
}
if len(r.args) > 1 {
args = r.args[1:]
}
r.logger.Info().Logf("running %s", name)
cmd := exec.Command(name, args...)
cmd.Stderr = os.Stdout
output, err := cmd.Output()
if err != nil {
return nil, r.logger.Error().LogErrorf("problem running %s: %v", name, err).Err()
}
return output, nil
}