forked from inconshreveable/go-update
-
Notifications
You must be signed in to change notification settings - Fork 13
/
executable.go
78 lines (65 loc) · 1.49 KB
/
executable.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
package selfupdate
import (
"fmt"
"os"
"path/filepath"
"sync"
"time"
"github.com/fynelabs/selfupdate/internal/osext"
)
var (
exePath string
defaultOldExePath string
exeErr error
once sync.Once
)
// ExecutableRealPath returns the path to the original executable and an error if something went bad
func ExecutableRealPath() (string, error) {
if loadPath() != nil {
return "", exeErr
}
return exePath, nil
}
// ExecutableDefaultOldPath returns the path to the old executable and an error if something went bad
func ExecutableDefaultOldPath() (string, error) {
if loadPath() != nil {
return "", exeErr
}
return defaultOldExePath, nil
}
func loadPath() error {
once.Do(func() {
exePath, exeErr = getExecutableRealPath()
if exeErr != nil {
return
}
// get the directory the executable exists in
updateDir := filepath.Dir(exePath)
filename := filepath.Base(exePath)
// get file path to the old executable
defaultOldExePath = filepath.Join(updateDir, fmt.Sprintf(".%s.old", filename))
})
return exeErr
}
func lastModifiedExecutable() (time.Time, error) {
exe, err := ExecutableRealPath()
if err != nil {
return time.Time{}, err
}
fi, err := os.Stat(exe)
if err != nil {
return time.Time{}, err
}
return fi.ModTime(), nil
}
func getExecutableRealPath() (string, error) {
exe, err := osext.Executable()
if err != nil {
return "", err
}
exe, err = filepath.EvalSymlinks(exe)
if err != nil {
return "", err
}
return exe, nil
}