/
main.go
69 lines (62 loc) · 1.32 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 tjob - Test job management utility
Copyright (c) 2014 Ohmu Ltd.
Licensed under the Apache License, Version 2.0 (see LICENSE)
*/
package main
import (
"fmt"
"github.com/jessevdk/go-flags"
"log"
"os"
"path"
"runtime"
"runtime/pprof"
)
type topArgs struct {
ConfigFile string `short:"c" long:"config" description:"Config file path"`
}
var gParser *flags.Parser
var globalFlags topArgs
func globalParser() *flags.Parser {
if gParser == nil {
globalFlags.ConfigFile = path.Join(
os.Getenv("HOME"), ".tjob", "default.json")
gParser = flags.NewParser(&globalFlags, flags.Default)
}
return gParser
}
func writeManPage() {
if troffFile := os.Getenv("WRITE_MAN_PAGE"); troffFile != "" {
out, err := os.Create(troffFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
globalParser().WriteManPage(out)
os.Exit(0)
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
writeManPage()
profileFile := os.Getenv("TJOB_CPUPROFILE")
if profileFile != "" {
f, perr := os.Create(profileFile)
if perr != nil {
log.Fatal(perr)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal(err)
}
defer func() {
pprof.StopCPUProfile()
fmt.Println("CPU profiling results written to",
profileFile)
}()
}
_, err := globalParser().ParseArgs(os.Args[1:])
if err != nil {
os.Exit(1)
}
}