-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
69 lines (55 loc) · 1.29 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
package main
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
"github.com/stateful/runme/v3/internal/cmd"
"github.com/stateful/runme/v3/internal/version"
)
func main() {
os.Exit(root())
}
func root() (status int) {
root := cmd.Root()
root.Version = version.BaseVersionInfo()
rootWithCPUProfile(func() {
if err := root.Execute(); err != nil {
logf("could not execute command: %v\n", err)
status = 1
}
})
return
}
func rootWithCPUProfile(fn func()) {
if profile := os.Getenv("RUNME_PROFILE_CPU"); profile != "" {
f, err := os.Create(profile)
if err != nil {
fatalf("could not create CPU profile: %v\n", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
fatalf("could not start CPU profile: %v\n", err)
}
defer pprof.StopCPUProfile()
}
fn()
if profile := os.Getenv("RUNME_PROFILE_MEM"); profile != "" {
f, err := os.Create(profile)
if err != nil {
fatalf("could not create mem profile: %v\n", err)
}
defer f.Close()
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
fatalf("could not write heap profile: %v\n", err)
}
}
}
func fatalf(format string, args ...interface{}) {
logf(format, args...)
os.Exit(1)
}
func logf(format string, args ...interface{}) {
_, _ = fmt.Fprintf(os.Stderr, format, args...)
}