/
verify.go
49 lines (41 loc) · 970 Bytes
/
verify.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
package updater
import (
"errors"
"os"
"os/exec"
"strings"
)
var (
// ErrVersionMisMatch is returned by verifyInstallation if
// the new downloaded version did not output the same
// version parsed to Update().
ErrVersionMisMatch = errors.New("version mismatch in updated executable")
)
// verifyInstallation verifies if the executable is installed
// correctly. The downloaded executable is run with the
// flag -version.
// Returns ErrVersionMisMatch if the versions could n ot be
// matched.
func (u *Updater) verifyInstallation() error {
latestVersion, err := u.LatestVersion()
if err != nil {
return err
}
executable, err := os.Executable()
if err != nil {
return err
}
cmd := exec.Cmd{
Path: executable,
Args: []string{executable, "-version"},
}
output, err := cmd.Output()
if err != nil {
return err
}
strOutput := string(output)
if !strings.Contains(strOutput, latestVersion) {
return ErrVersionMisMatch
}
return nil
}