-
Notifications
You must be signed in to change notification settings - Fork 1
/
box.go
125 lines (107 loc) · 3.56 KB
/
box.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
package goxr
import (
"errors"
"github.com/echocat/goxr/box/fs"
"github.com/echocat/goxr/box/packed"
"github.com/echocat/goxr/common"
"github.com/echocat/goxr/runtime"
"github.com/echocat/slf4g"
"io"
"path/filepath"
sr "runtime"
"strings"
)
type OnFallbackToFsBoxFunc func(packedBoxCandidateFilename string, bases []string, fsBox Box) error
var (
// AllowFallbackToFsBox could easily set while build time using:
// go build -ldflags="-X github.com/echocat/goxr.AllowFallbackToFsBox=false" .
// This is useful in case for behave differently for build versions of you application
AllowFallbackToFsBox = true
OnFallbackToFsBox OnFallbackToFsBoxFunc = OnFallbackToFsBox_Default
ErrBoxIterationNotSupported = errors.New("box iteration not supported")
)
type Box interface {
io.Closer
Open(name string) (common.File, error)
Info(pathname string) (common.FileInfo, error)
}
type Iterable interface {
ForEach(common.FilePredicate, func(common.FileInfo) error) error
}
func OpenBox(base ...string) (Box, error) {
if executable, err := runtime.Executable(); err != nil {
return nil, err
} else {
return OpenBoxBy(executable, base...)
}
}
func OpenPackedBox() (Box, error) {
if executable, err := runtime.Executable(); err != nil {
return nil, err
} else {
return packed.OpenBox(executable)
}
}
func OpenBoxBy(packedBoxCandidateFilename string, base ...string) (Box, error) {
if packedBox, err := packed.OpenBox(packedBoxCandidateFilename); common.IsDoesNotContainBox(err) {
if box, err := openFsBox(base); err != nil {
return nil, err
} else if OnFallbackToFsBox == nil {
return box, nil
} else if err := OnFallbackToFsBox(packedBoxCandidateFilename, base, box); err != nil {
return nil, err
} else {
return box, nil
}
} else if err != nil {
return nil, err
} else {
return packedBox, nil
}
}
func openFsBox(bases []string) (Box, error) {
callingDir := resolveCallingDir(2)
boxes := make(CombinedBox, len(bases))
for i, base := range bases {
if !filepath.IsAbs(base) {
var err error
if base, err = filepath.Abs(filepath.Join(callingDir, base)); err != nil {
return nil, common.NewPathError("openBox", base, err)
}
}
if box, err := fs.OpenBox(base); err != nil {
return nil, err
} else {
boxes[i] = box
}
}
return boxes, nil
}
func resolveCallingDir(skipCallerFrames int) string {
_, filename, _, _ := sr.Caller(skipCallerFrames + 1)
result := filepath.Dir(filename)
// this little hack courtesy of the `-cover` flag!!
cov := filepath.Join("_test", "_obj_test")
result = strings.Replace(result, string(filepath.Separator)+cov, "", 1)
if result != "" && !filepath.IsAbs(result) {
result = filepath.Join(runtime.GoPath(), "src", result)
}
return result
}
// noinspection GoSnakeCaseUsage
var OnFallbackToFsBox_Default = func(packedBoxCandidateFilename string, bases []string, fsBox Box) error {
if AllowFallbackToFsBox {
return OnFallbackToFsBox_Warn(packedBoxCandidateFilename, bases, fsBox)
}
return OnFallbackToFsBox_Fail(packedBoxCandidateFilename, bases, fsBox)
}
// noinspection GoSnakeCaseUsage
var OnFallbackToFsBox_Warn = func(packedBoxCandidateFilename string, bases []string, fsBox Box) error {
log.With("candidate", packedBoxCandidateFilename).
Warn("Candidate does not contain a packed box version. This could happen in development mode.")
return nil
}
// noinspection GoSnakeCaseUsage
var OnFallbackToFsBox_Fail = func(packedBoxCandidateFilename string, bases []string, fsBox Box) error {
return common.NewPathError("openBox", packedBoxCandidateFilename, common.ErrDoesNotContainBox)
}