-
Notifications
You must be signed in to change notification settings - Fork 4
/
gm.go
81 lines (69 loc) · 1.46 KB
/
gm.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
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
)
// version will be set by goreleaser based on the git tag
var version string = "--"
var goldmarkVersion string = "--"
// printError acts only is error is present:
// - print the error message
// - panic if necessary
func printError(fatal bool, e error, m ...interface{}) {
if e != nil {
if len(m) > 0 {
fmt.Fprint(os.Stderr, "Error: ")
fmt.Fprintln(os.Stderr, m...)
} else {
fmt.Fprintln(os.Stderr, "Error.")
}
fmt.Fprintln(os.Stderr, e)
if fatal {
panic(e)
}
}
}
// check print the error message and panic if there is an error
func check(e error, m ...interface{}) {
printError(true, e, m...)
}
// check print the error message (no panic) if there is an error
func try(e error, m ...interface{}) {
printError(false, e, m...)
}
// end is the last function executed in this program.
func mainEnd() {
// in case of error return status is 1
if r := recover(); r != nil {
os.Exit(1)
}
// the normal return status is 0
os.Exit(0)
}
// If we terminate with Ctrl/Cmd-C we call end()
func catchCtrlC() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
info("Bye.\n")
mainEnd()
}()
}
// main is the entry point
func main() {
// error handling
defer mainEnd()
// interrupt handling
catchCtrlC()
// check the flags and initialize the parser
SetParameters()
// serve or build ?
if serve {
serveFiles()
} else {
buildFiles()
}
}