-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
177 lines (150 loc) · 3.59 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Entry-point for our application.
//
package main
import (
"fmt"
"io"
"log/slog"
"os"
"strings"
"github.com/skx/subcommands"
)
var (
// logger contains a shared logging handle, used by our sub-commands.
logger *slog.Logger
// loggerLevel allows changing the log-level at runtime
loggerLevel *slog.LevelVar
)
// Recovery is good
func recoverPanic() {
if r := recover(); r != nil {
logger.Error("recovered from a panic", slog.String("error", fmt.Sprintf("%s", r)))
}
}
// Register the subcommands, and run the one the user chose.
func main() {
//
// Setup our default logging level, which will show
// both warnings and errors.
//
loggerLevel = &slog.LevelVar{}
loggerLevel.Set(slog.LevelWarn)
//
// If the user wants a different level they can choose it.
//
level := os.Getenv("LOG_LEVEL")
//
// Legacy/Compatibility
//
if os.Getenv("LOG_ALL") != "" {
level = "DEBUG"
}
// Simplify things by only caring about upper-case
level = strings.ToUpper(level)
switch level {
case "DEBUG":
loggerLevel.Set(slog.LevelDebug)
case "WARN":
loggerLevel.Set(slog.LevelWarn)
case "ERROR":
loggerLevel.Set(slog.LevelError)
case "":
// NOP
default:
fmt.Printf("Unknown logging-level '%s'\n", level)
return
}
// Those handler options
opts := &slog.HandlerOptions{
Level: loggerLevel,
AddSource: true,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.SourceKey {
s := a.Value.Any().(*slog.Source)
// Assume we have a source-path containing "rss2email"
// if we do strip everything before that out.
start := strings.Index(s.File, "rss2email")
if start > 0 {
s.File = s.File[start:]
}
// Assume we have a function containing "rss2email"
// if we do strip everything before that out.
start = strings.Index(s.Function, "rss2email")
if start > 0 {
s.Function = s.Function[start:]
}
}
return a
},
}
//
// Create a default writer, which the logger will use.
// This will mostly go to STDERR, however it might also
// be duplicated to a file.
//
multi := io.MultiWriter(os.Stderr)
//
// Default logfile path can be changed by LOG_FILE
// environmental variable.
//
logPath := "rss2email.log"
if os.Getenv("LOG_FILE_PATH") != "" {
logPath = os.Getenv("LOG_FILE_PATH")
}
//
// Create a logfile, if we can.
//
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
//
// No error? Then update our writer to use it.
//
if err == nil {
defer file.Close()
//
// Unless we've been disabled then update our
// writer.
//
if os.Getenv("LOG_FILE_DISABLE") != "" {
multi = io.MultiWriter(file, os.Stderr)
}
}
//
// Default to showing to STDERR [+file] in text.
//
var handler slog.Handler
handler = slog.NewTextHandler(multi, opts)
//
// But allow JSON formatting too.
//
if os.Getenv("LOG_JSON") != "" {
handler = slog.NewJSONHandler(multi, opts)
}
//
// Create our logging handler, using the level we've just setup
//
logger = slog.New(handler)
//
// Catch errors
//
defer recoverPanic()
//
// Register each of our subcommands.
//
subcommands.Register(&addCmd{})
subcommands.Register(&cronCmd{})
subcommands.Register(&configCmd{})
subcommands.Register(&daemonCmd{})
subcommands.Register(&delCmd{})
subcommands.Register(&exportCmd{})
subcommands.Register(&importCmd{})
subcommands.Register(&listCmd{})
subcommands.Register(&listDefaultTemplateCmd{})
subcommands.Register(&seenCmd{})
subcommands.Register(&unseeCmd{})
subcommands.Register(&versionCmd{})
//
// Execute the one the user chose.
//
os.Exit(subcommands.Execute())
}