-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
86 lines (80 loc) · 1.83 KB
/
utils.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
82
83
84
85
86
package errorist
import (
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"github.com/pkg/errors"
)
func callerPackageName(skip int) string {
pc := make([]uintptr, 1)
n := runtime.Callers(2+skip, pc)
if n == 0 {
return "unknown"
}
frame, _ := runtime.CallersFrames(pc).Next()
frags := strings.Split(frame.Function, ".")
if len(frags) == 0 {
if frame.Function != "" {
return frame.Function
}
return frame.File
}
return strings.Join(frags[:len(frags)-1], ".")
}
func callerTrace(skip int) string {
pc := make([]uintptr, 1)
n := runtime.Callers(2+skip, pc)
if n == 0 {
return "unknown"
}
frame, _ := runtime.CallersFrames(pc).Next()
return fmt.Sprintf("%s (%s:%d)", frame.Function, filepath.Base(frame.File), frame.Line)
}
func maybeWrap(err error, opts Options) error {
if err == nil {
return nil
}
if len(opts.WrapArguments) == 0 {
return err
}
switch format := opts.WrapArguments[0].(type) {
case string:
return errors.Wrapf(err, format, opts.WrapArguments[1:]...)
default:
return errors.Wrap(err, fmt.Sprint(opts.WrapArguments...))
}
}
// getGOPATHs returns parsed GOPATH or its default, using "/" as path separator.
func getGOPATHs() []string {
var out []string
if gp := os.Getenv("GOPATH"); gp != "" {
for _, v := range filepath.SplitList(gp) {
// Disallow non-absolute paths?
if v != "" {
v = strings.Replace(v, "\\", "/", -1)
// Trim trailing "/".
if l := len(v); v[l-1] == '/' {
v = v[:l-1]
}
out = append(out, v)
}
}
}
if len(out) == 0 {
homeDir := ""
u, err := user.Current()
if err != nil {
homeDir = os.Getenv("HOME")
if homeDir == "" {
panic(fmt.Sprintf("Could not get current user or $HOME: %s\n", err.Error()))
}
} else {
homeDir = u.HomeDir
}
out = []string{strings.Replace(homeDir+"/go", "\\", "/", -1)}
}
return out
}