-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_binary.go
More file actions
195 lines (163 loc) · 3.95 KB
/
runtime_binary.go
File metadata and controls
195 lines (163 loc) · 3.95 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package bbox
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
)
var packageRootRuntimeCaller = runtime.Caller
var packageRootGetwd = os.Getwd
var runtimeBinaryExecutablePath = os.Executable
const (
runtimeBinaryName = "bbox"
)
type runtimeBinaryResolver struct {
once sync.Once
executablePath func() (string, error)
packageRoot func() (string, error)
makeTempDir func(string, string) (string, error)
buildBBox func(string, string) error
removeAll func(string) error
path string
dir string
err error
}
func newRuntimeBinaryResolver() *runtimeBinaryResolver {
return &runtimeBinaryResolver{
executablePath: runtimeBinaryExecutablePath,
packageRoot: packageRoot,
makeTempDir: os.MkdirTemp,
buildBBox: buildBBoxBinary,
removeAll: os.RemoveAll,
}
}
func (r *runtimeBinaryResolver) RuntimeBinary() (string, error) {
if r == nil {
return "", fmt.Errorf("runtime binary resolver is required")
}
r.once.Do(func() {
if path, ok := r.packagedBBox(); ok {
r.path = path
return
}
moduleRoot, err := r.packageRoot()
if err != nil {
r.err = err
return
}
buildDir, err := r.makeTempDir("", "bbox-runtime-build-")
if err != nil {
r.err = fmt.Errorf("create runtime build dir: %w", err)
return
}
bboxPath := filepath.Join(buildDir, runtimeBinaryName)
if err := r.buildBBox(moduleRoot, bboxPath); err != nil {
_ = r.removeAll(buildDir)
r.err = err
return
}
r.dir = buildDir
r.path = bboxPath
})
if r.err != nil {
return "", r.err
}
if r.path == "" {
return "", fmt.Errorf("runtime binary path is empty")
}
return r.path, nil
}
func (r *runtimeBinaryResolver) Cleanup() error {
if r == nil || r.dir == "" {
return nil
}
return r.removeAll(r.dir)
}
func (r *runtimeBinaryResolver) packagedBBox() (string, bool) {
if r == nil || r.executablePath == nil {
return "", false
}
exePath, err := r.executablePath()
if err != nil || strings.TrimSpace(exePath) == "" {
return "", false
}
exePath = filepath.Clean(exePath)
if regularFileExists(exePath) && isBBoxBinaryName(exePath) {
return exePath, true
}
siblingPath := filepath.Join(filepath.Dir(exePath), runtimeBinaryName)
if regularFileExists(siblingPath) {
return siblingPath, true
}
return "", false
}
func isBBoxBinaryName(path string) bool {
name := strings.ToLower(filepath.Base(path))
return name == runtimeBinaryName || name == runtimeBinaryName+".exe"
}
func regularFileExists(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.Mode().IsRegular()
}
func packageRoot() (string, error) {
if _, file, _, ok := packageRootRuntimeCaller(0); ok {
if root, found := findPackageRoot(filepath.Dir(file)); found {
return root, nil
}
}
cwd, err := packageRootGetwd()
if err != nil {
return "", fmt.Errorf("determine package root: %w", err)
}
if root, found := findPackageRoot(cwd); found {
return root, nil
}
return "", fmt.Errorf("locate package root from working directory %q", cwd)
}
func findPackageRoot(start string) (string, bool) {
if start == "" {
return "", false
}
dir, err := filepath.Abs(start)
if err != nil {
return "", false
}
for {
if isPackageRoot(dir) {
return dir, true
}
parent := filepath.Dir(dir)
if parent == dir {
return "", false
}
dir = parent
}
}
func isPackageRoot(dir string) bool {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err != nil {
return false
}
if _, err := os.Stat(filepath.Join(dir, "cmd", "bbox", "main.go")); err == nil {
return true
}
return false
}
func buildBBoxBinary(moduleRoot, bboxPath string) error {
cmd := exec.Command("go", "build", "-o", bboxPath, "./cmd/bbox")
cmd.Dir = moduleRoot
output, err := cmd.CombinedOutput()
if err == nil {
return nil
}
msg := strings.TrimSpace(string(output))
if msg != "" {
return fmt.Errorf("build bbox runtime binary: %w: %s", err, msg)
}
return fmt.Errorf("build bbox runtime binary: %w", err)
}