-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
154 lines (127 loc) · 3.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"
)
type CmdArgs struct {
file *string
pattern *string
dryRun *bool
noFollow *bool
verbose *bool
fromLineNumber *int
config *string
}
var (
_isDryRun bool
_verbose bool
)
var log = logrus.New()
func isDryRun() bool {
return _isDryRun
}
func isVerbose() bool {
return _verbose
}
func initSentry(config *Config) {
if isDryRun() {
log.Println("Dry-run mode enabled, not initializing Sentry client")
return
}
dsn := ""
if config.SentryDsn != "" {
dsn = config.SentryDsn
} else {
dsn = os.Getenv("SENTLOG_SENTRY_DSN")
}
if dsn == "" {
log.Fatal("No DSN found\n")
}
err := sentry.Init(sentry.ClientOptions{Dsn: dsn})
if err != nil {
log.Fatalf("Sentry initialization failed: %v\n", err)
}
}
// Catches Ctrl-C
func catchInterrupt() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGINT)
go func() {
<-c
log.Println("Cleaning up...")
sentry.Flush(5 * time.Second)
os.Exit(1)
}()
}
func initLogging() {
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
var logLevel = logrus.InfoLevel
logLevelEnv := strings.ToLower(os.Getenv("SENTLOG_LOG_LEVEL"))
switch logLevelEnv {
case "debug":
logLevel = logrus.DebugLevel
case "info":
logLevel = logrus.InfoLevel
}
log.SetLevel(logLevel)
}
func showGreeting() {
}
func main() {
initLogging()
args := CmdArgs{
file: kingpin.Arg("file", "File to parse").String(),
pattern: kingpin.Flag("pattern", "Pattern to look for").Short('p').String(),
dryRun: kingpin.Flag("dry-run", "Dry-run mode").Default("false").Bool(),
noFollow: kingpin.Flag("no-follow", "Do not wait for the new data").Bool(),
fromLineNumber: kingpin.Flag("from-line", "Start reading from this line number").Default("-1").Int(),
config: kingpin.Flag("config", "Path to the configuration").Short('c').String(),
verbose: kingpin.Flag("verbose", "Print every match").Short('v').Default("false").Bool(),
}
kingpin.Parse()
showGreeting()
_isDryRun = *args.dryRun
_verbose = *args.verbose
var config *Config
if *args.config == "" {
if *args.pattern == "" || *args.file == "" {
log.Fatalln("Both file and pattern have to be specified, aborting")
}
log.Println("Using parameters from the command line")
follow := !*args.noFollow
config = &Config{
SentryDsn: "",
Inputs: []FileInputConfig{
FileInputConfig{
File: *args.file,
Follow: &follow,
FromLineNumber: args.fromLineNumber,
Patterns: []string{*args.pattern},
},
},
}
} else {
log.Println("Using parameters from the configuration file")
if *args.pattern != "" || *args.file != "" {
log.Fatalln("No pattern/file allowed when configuration file is provided, aborting")
}
configPath := *args.config
parsedConfig, err := ReadConfigFromFile(configPath)
if err != nil {
log.Fatal(err)
}
config = parsedConfig
log.Printf("Configuration file loaded: \"%s\"\n", configPath)
}
initSentry(config)
catchInterrupt()
runWithConfig(config)
}