-
Notifications
You must be signed in to change notification settings - Fork 10
/
root.go
64 lines (51 loc) · 1.47 KB
/
root.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
package command
import (
"fmt"
"os"
"path/filepath"
"runtime/pprof"
"github.com/spf13/cobra"
"github.com/simplesurance/baur/exec"
"github.com/simplesurance/baur/log"
"github.com/simplesurance/baur/version"
)
var rootCmd = &cobra.Command{
Use: "baur",
Short: "baur manages builds and artifacts in mono repositories.",
PersistentPreRun: initSb,
}
var verboseFlag bool
var cpuProfilingFlag bool
var defCPUProfFile = filepath.Join(os.TempDir(), "baur-cpu.prof")
func initSb(_ *cobra.Command, _ []string) {
if verboseFlag {
log.StdLogger.EnableDebug(verboseFlag)
exec.DefaultDebugfFn = log.StdLogger.Debugf
}
if cpuProfilingFlag {
cpuProfFile, err := os.Create(defCPUProfFile)
if err != nil {
log.Fatalln(err)
}
if err := pprof.StartCPUProfile(cpuProfFile); err != nil {
log.Fatalln(err)
}
}
}
// Execute parses commandline flags and execute their actions
func Execute() {
if err := version.LoadPackageVars(); err != nil {
log.Errorln("setting version failed", err)
}
rootCmd.Version = version.CurSemVer.String()
rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().BoolVar(&cpuProfilingFlag, "cpu-prof", false,
fmt.Sprintf("enable cpu profiling, result is written to %q", defCPUProfFile))
if err := rootCmd.Execute(); err != nil {
log.Fatalln(err)
}
if cpuProfilingFlag {
fmt.Printf("\ncpu profile written to %q\n", defCPUProfFile)
pprof.StopCPUProfile()
}
}