forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_others.go
92 lines (84 loc) · 2.05 KB
/
update_others.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
// +build !windows
package action
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"syscall"
"golang.org/x/sys/unix"
"github.com/justwatchcom/gopass/utils/out"
"github.com/pkg/errors"
)
func (s *Action) updateGopass(ctx context.Context, version string, url string) error {
exe, err := s.executable(ctx)
if err != nil {
return err
}
if !strings.HasPrefix(url, "https") {
return errors.Errorf("refusing non-https URL %s", url)
}
td, err := ioutil.TempDir("", "gopass-")
if err != nil {
return err
}
defer func() {
_ = os.RemoveAll(td)
}()
out.Debug(ctx, "Tempdir: %s", td)
archive := filepath.Join(td, path.Base(url))
if err := s.tryDownload(ctx, archive, url); err != nil {
return err
}
binDst := exe + "_new"
_ = os.Remove(binDst)
if err := s.extract(ctx, archive, binDst); err != nil {
return err
}
// launch rename script and exit
out.Yellow(ctx, "Downloaded update. Exiting to install in place (%s) ...", exe)
cmd := exec.Command("/bin/bash", "-c", fmt.Sprintf(`sleep 1; echo ""; echo -n "Please wait ... "; sleep 2; mv "%s" "%s" && echo OK`, binDst, exe))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
return cmd.Start()
}
func (s *Action) isUpdateable(ctx context.Context) error {
fn, err := s.executable(ctx)
if err != nil {
return err
}
out.Debug(ctx, "isUpdateable - File: %s", fn)
// check if file is in GOPATH
if gp := os.Getenv("GOPATH"); strings.HasPrefix(fn, gp) {
return fmt.Errorf("use go get -u to update binary in GOPATH")
}
// check file
fi, err := os.Stat(fn)
if err != nil {
return err
}
if !fi.Mode().IsRegular() {
return fmt.Errorf("not a regular file")
}
if err := unix.Access(fn, unix.W_OK); err != nil {
return err
}
// check dir
fdir := filepath.Dir(fn)
return unix.Access(fdir, unix.W_OK)
}
func (s *Action) executable(ctx context.Context) (string, error) {
path, err := os.Executable()
if err != nil {
return path, err
}
path, err = filepath.EvalSymlinks(path)
return path, err
}