forked from juju/errors
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.go
51 lines (44 loc) · 1.12 KB
/
path.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
// Copyright 2013, 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package errgo
import (
"os"
"runtime"
"strings"
)
// prefixSize is used internally to trim the user specific path from the
// front of the returned filenames from the runtime call stack.
var prefixSize int
// goPath is the deduced path based on the location of this file as compiled.
var goPath string
func init() {
_, file, _, ok := runtime.Caller(0)
if file == "?" {
return
}
if ok {
// We know that the end of the file should be:
// github.com/hifx/errgo/path.go
size := len(file)
suffix := len("github.com/hifx/errgo/path.go")
goPath = file[:size-suffix]
prefixSize = len(goPath)
}
}
func trimGoPath(filename string) string {
if strings.HasPrefix(filename, goPath) {
return filename[prefixSize:]
}
return filename
}
func trimPackage(function string) string {
slashIndex := strings.LastIndex(function, string(os.PathSeparator))
if slashIndex < 0 {
slashIndex = 0
}
dotIndex := strings.Index(function[slashIndex:], ".")
if dotIndex == -1 {
return function
}
return function[slashIndex+dotIndex+1:]
}