-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
135 lines (106 loc) · 2.61 KB
/
conf.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package errific
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
// Configure errific options.
func Configure(opts ...Option) {
// defaults
c.caller = Suffix
c.layout = Newline
c.withStack = false
c.trimPrefixes = nil
c.trimCWD = false
for _, opt := range opts {
switch o := opt.(type) {
case callerOption:
c.caller = o
case layoutOption:
c.layout = o
case withStackTraceOption:
c.withStack = o
case trimPrefixesOption:
c.trimPrefixes = o.Prefixes()
case trimCWDOption:
c.trimCWD = o
}
}
if c.trimCWD {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
c.trimPrefixes = append([]string{filepath.Dir(cwd) + "/"}, c.trimPrefixes...)
}
}
var c struct {
// Caller will configure the caller: Suffix|Prefix|Disabled.
// Default is Suffix.
caller callerOption
// Layout will configure the layout of wrapped errors: Newline|Inline.
// Default is Newline.
layout layoutOption
// WithStack will append stacktrace to end of message.
// Default is not including the stack.
withStack withStackTraceOption
// TrimPrefixes will trim prefixes from caller frame filenames.
trimPrefixes []string
// TrimCWD will trim the current working directory from filenames.
// Default is false.
trimCWD trimCWDOption
}
type callerOption int
func (callerOption) ErrificOption() {}
const (
// Suffix adds caller information at the end of the error message.
// This is default.
Suffix callerOption = iota
// Prefix adds caller information at the beginning of the error message.
Prefix
// Disabled does not include caller information in the error message.
Disabled
)
type layoutOption int
func (layoutOption) ErrificOption() {}
const (
// Newline joins errors with \n.
// This is default.
Newline layoutOption = iota
// Inline wraps errors with ↩.
Inline
)
type withStackTraceOption bool
func (withStackTraceOption) ErrificOption() {}
const (
// Include stacktrace in error message.
WithStack withStackTraceOption = true
)
type trimPrefixesOption struct {
prefixes []string
}
func (trimPrefixesOption) ErrificOption() {}
func (t trimPrefixesOption) Prefixes() []string {
return t.prefixes
}
var (
// TrimPrefixes from caller frame filenames.
TrimPrefixes = func(prefixes ...string) trimPrefixesOption {
return trimPrefixesOption{prefixes: prefixes}
}
)
type trimCWDOption bool
func (trimCWDOption) ErrificOption() {}
const (
// Trim current working directory from filenames.
TrimCWD trimCWDOption = true
)
type Option interface {
ErrificOption()
}
var root string
func init() {
_, file, _, _ := runtime.Caller(0)
root = fmt.Sprintf("%s/", filepath.Join(filepath.Dir(file), ".."))
}