-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (106 loc) · 2.73 KB
/
main.go
File metadata and controls
126 lines (106 loc) · 2.73 KB
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
118
119
120
121
122
123
124
125
126
// Package main is the entry point for patchfiles, a tool that generates patch and revert
// bash scripts from YAML patch definitions. It reads YAML files from an embedded filesystem,
// parses them, and generates executable bash scripts for applying and reverting system patches.
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 is the embedded filesystem containing all YAML patch definition files.
content embed.FS
// verbose controls whether the logger should output debug-level messages.
verbose = flag.Bool("VERBOSE", true, "disable or enable verbose")
)
const (
// contextTimeout is the maximum time to wait for parsing to complete before canceling.
contextTimeout = 10 * time.Second
)
// main initializes the logger, sets up signal handling for graceful shutdown,
// determines the environment, creates patch/revert script generators, and processes
// all YAML patch files from the embedded filesystem.
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)
}
}
}