-
Notifications
You must be signed in to change notification settings - Fork 0
/
sugar.go
55 lines (48 loc) · 1004 Bytes
/
sugar.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
package rotate
import "os"
// Common bytes.
const (
B int64 = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
)
// OpenPerm is used in *Open shortcuts to create a file.
const OpenPerm os.FileMode = 0644
// MustWrap is like Wrap, but panics on error. ErrNotSupported is skipped.
func MustWrap(f File, c Config) File {
r, err := Wrap(f, c)
if mustPanic(err) {
panic(err)
}
return r
}
func mustPanic(err error) bool {
if err == ErrNotSupported {
return false
}
return err != nil
}
// MustOpen is like Open, but panics on error. ErrNotSupported is skipped.
func MustOpen(name string, c Config) File {
f, err := Open(name, c)
if mustPanic(err) {
panic(err)
}
return f
}
// Open opens a file and wraps it.
func Open(name string, c Config) (File, error) {
f, err := os.OpenFile(name, OpenFlag, OpenPerm)
if err != nil {
return nil, err
}
r, err := Wrap(f, c)
if err == ErrNotSupported {
return r, err
}
if err != nil {
_ = f.Close()
}
return r, err
}