forked from mszostok/codeowners-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (73 loc) · 2.08 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
package main
import (
"context"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/mszostok/codeowners-validator/internal/check"
"github.com/mszostok/codeowners-validator/internal/envconfig"
"github.com/mszostok/codeowners-validator/internal/load"
"github.com/mszostok/codeowners-validator/internal/runner"
"github.com/mszostok/codeowners-validator/pkg/codeowners"
"github.com/mszostok/codeowners-validator/pkg/version"
"github.com/sirupsen/logrus"
)
// Config holds the application configuration
type Config struct {
RepositoryPath string
CheckFailureLevel check.SeverityType `envconfig:"default=warning"`
Checks []string `envconfig:"optional"`
ExperimentalChecks []string `envconfig:"optional"`
}
func main() {
version.Init()
if version.ShouldPrintVersion() {
version.PrintVersion(os.Stdout)
os.Exit(0)
}
var cfg Config
err := envconfig.Init(&cfg)
exitOnError(err)
log := logrus.New()
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
cancelOnInterrupt(ctx, cancelFunc)
// init codeowners entries
codeownersEntries, err := codeowners.NewFromPath(cfg.RepositoryPath)
exitOnError(err)
// init checks
checks, err := load.Checks(ctx, cfg.Checks, cfg.ExperimentalChecks)
exitOnError(err)
// run check runner
absRepoPath, err := filepath.Abs(cfg.RepositoryPath)
exitOnError(err)
checkRunner := runner.NewCheckRunner(log, codeownersEntries, absRepoPath, cfg.CheckFailureLevel, checks...)
checkRunner.Run(ctx)
if ctx.Err() != nil {
log.Error("Application was interrupted by operating system")
os.Exit(2)
}
if checkRunner.ShouldExitWithCheckFailure() {
os.Exit(3)
}
}
func exitOnError(err error) {
if err != nil {
logrus.Fatal(err)
}
}
// cancelOnInterrupt calls cancel func when os.Interrupt or SIGTERM is received
func cancelOnInterrupt(ctx context.Context, cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-ctx.Done():
case <-c:
cancel()
<-c
os.Exit(1) // second signal. Exit directly.
}
}()
}