-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
117 lines (97 loc) · 2 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"context"
"embed"
"flag"
"os"
"os/signal"
"strings"
"syscall"
"time"
"patchfiles/generator"
"patchfiles/logger"
"patchfiles/parser"
"go.uber.org/zap"
)
var (
//go:embed patches/*.yaml
content embed.FS
verbose = flag.Bool("VERBOSE", true, "disable or enable verbose")
)
const (
contextTimeout = 10 * time.Second
)
func main() {
log, _ := logger.Setup(*verbose)
defer log.Sync()
log.Info("patchfiles started")
signals := make(chan os.Signal, 1)
signal.Notify(signals,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)
// determine environment
environment := os.Getenv("ENVIRONMENT")
environment = strings.ToLower(environment)
environment = strings.Trim(environment, " ")
if environment == "" {
environment = "dev"
}
gen := generator.Generator{
Log: log,
Environment: environment,
}
gen.Open()
// setup context timeout
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, contextTimeout)
defer cancel()
// gracefoul shutdown
go func() {
s := <-signals
log.Warn("received signal",
zap.String("signal", s.String()),
)
cancel()
os.Exit(1)
}()
// run parser
errors, results := parser.Run(log, &cancel, content)
stats := map[string]int{
"errors": 0,
"good": 0,
"total": 0,
}
for {
select {
case e := <-errors:
logger := log.WithOptions(zap.Fields(
zap.Error(e.Error),
zap.String("fileLoc", *e.FileLoc),
))
logger.Error("received error")
stats["errors"] += 1
stats["total"] += 1
case r := <-results:
logger := log.WithOptions(zap.Fields(
zap.String("fileLoc", *r.FileLoc),
zap.String("name", r.Name),
))
logger.Info("received result")
gen.Write(r)
stats["good"] += 1
stats["total"] += 1
case <-ctx.Done():
log.Info("context is done")
gen.Close()
log.Debug("processing is done. stats",
zap.Int("total", stats["total"]),
zap.Int("good", stats["good"]),
zap.Int("errors", stats["errors"]),
)
os.Exit(0)
}
}
}