-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathmain.go
More file actions
88 lines (76 loc) · 1.94 KB
/
Copy pathmain.go
File metadata and controls
88 lines (76 loc) · 1.94 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
package main
import (
gocontext "context"
"os"
"os/signal"
"syscall"
"github.com/helmfile/helmfile/cmd"
"github.com/helmfile/helmfile/pkg/app"
"github.com/helmfile/helmfile/pkg/config"
"github.com/helmfile/helmfile/pkg/errors"
"github.com/helmfile/helmfile/pkg/telemetry"
)
func main() {
var sig os.Signal
sigs := make(chan os.Signal, 1)
errChan := make(chan error, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
globalConfig := new(config.GlobalOptions)
rootCmd, err := cmd.NewRootCmd(globalConfig)
if err != nil {
errChan <- err
return
}
errChan <- rootCmd.Execute()
}()
select {
case sig = <-sigs:
if sig != nil {
app.Cancel()
app.CleanWaitGroup.Wait()
shutdownTelemetry(nil, signalExitCode(sig))
// See http://tldp.org/LDP/abs/html/exitcodes.html
switch sig {
case syscall.SIGINT:
os.Exit(130)
case syscall.SIGTERM:
os.Exit(143)
}
}
case err := <-errChan:
shutdownTelemetry(err, exitCodeOf(err))
errors.HandleExitCoder(err)
}
}
// shutdownTelemetry flushes buffered spans before the process exits. It is a
// no-op when tracing was never enabled. Flush failures are deliberately
// ignored: telemetry must not mask the command's own result.
func shutdownTelemetry(runErr error, exitCode int) {
ctx, cancel := gocontext.WithTimeout(gocontext.Background(), telemetry.ShutdownTimeout)
defer cancel()
_ = telemetry.Shutdown(ctx, runErr, exitCode)
}
// signalExitCode maps termination signals to conventional exit codes, matching
// the os.Exit calls in main.
func signalExitCode(sig os.Signal) int {
switch sig {
case syscall.SIGINT:
return 130
case syscall.SIGTERM:
return 143
default:
return 1
}
}
// exitCodeOf extracts the exit code from the command result for the root-span
// attribute; success is 0.
func exitCodeOf(err error) int {
if err == nil {
return 0
}
if exitErr, ok := err.(errors.ExitCoder); ok {
return exitErr.ExitCode()
}
return 1
}