-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.go
More file actions
44 lines (36 loc) · 894 Bytes
/
Copy pathbinary.go
File metadata and controls
44 lines (36 loc) · 894 Bytes
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
package matcha
import (
"fmt"
"os"
"path/filepath"
)
// installBinary copies the current executable to the binary path.
func (m *Matcha) installBinary() error {
if os.Getenv("ENV") == "test" {
return nil
}
currentExe, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get executable path: %w", err)
}
// Resolve symlinks to compare real paths.
realExe, err := filepath.EvalSymlinks(currentExe)
if err != nil {
realExe = currentExe
}
realTarget, err := filepath.EvalSymlinks(m.config.BinaryPath)
if err != nil {
realTarget = m.config.BinaryPath
}
if realExe == realTarget {
return nil
}
data, err := os.ReadFile(currentExe)
if err != nil {
return fmt.Errorf("failed to read binary: %w", err)
}
if err := os.WriteFile(m.config.BinaryPath, data, 0755); err != nil {
return fmt.Errorf("failed to write binary: %w", err)
}
return nil
}