-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (51 loc) · 1.31 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
package main
import (
"context"
"fmt"
"github.com/cirruslabs/chacha/internal/command"
"github.com/cirruslabs/chacha/internal/logginglevel"
"github.com/cirruslabs/chacha/internal/opentelemetry"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"os/signal"
"syscall"
)
func main() {
if !mainImpl() {
os.Exit(1)
}
}
func mainImpl() bool {
// Set up a signal-interruptible context
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
// Initialize OpenTelemetry
opentelemetryCore, opentelemetryDeinit, err := opentelemetry.Init(ctx)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to initialize OpenTelemetry: %v", err)
return false
}
defer opentelemetryDeinit()
// Initialize logger
cfg := zap.NewProductionConfig()
cfg.Level = logginglevel.Level
logger, err := cfg.Build(zap.WrapCore(func(originalCore zapcore.Core) zapcore.Core {
return zapcore.NewTee(originalCore, opentelemetryCore)
}))
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return false
}
defer func() {
_ = logger.Sync()
}()
// Replace zap.L() and zap.S() to avoid
// propagating the *zap.Logger by hand
zap.ReplaceGlobals(logger)
if err := command.NewRootCommand().ExecuteContext(ctx); err != nil {
logger.Sugar().Error(err)
return false
}
return true
}