forked from bpicode/fritzctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (53 loc) · 860 Bytes
/
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
package main
import (
"fmt"
"os"
"strings"
"github.com/bpicode/fritzctl/cmd"
"github.com/bpicode/fritzctl/logger"
)
var (
exit = os.Exit
)
func main() {
defer func() {
r := recover()
if r != nil {
printErr(r)
}
exitCode := determineExitCode(r)
exit(exitCode)
}()
err := cmd.RootCmd.Execute()
if err != nil {
panic(err)
}
}
func determineExitCode(v interface{}) int {
if v == nil {
return 0
}
return 1
}
func printErr(r interface{}) {
st := stack(r)
logger.Error(strings.Join(st, "\n "))
}
func stack(e interface{}) []string {
if e == nil {
return nil
}
type causer interface {
error
Cause() error
Msg() string
}
var frames []string
csr, ok := e.(causer)
if !ok {
return []string{fmt.Sprint(e)}
}
frames = append(frames, csr.Msg()+":")
frames = append(frames, stack(csr.Cause())...)
return frames
}