forked from creativeprojects/go-selfupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.go
More file actions
173 lines (150 loc) · 5.02 KB
/
update.go
File metadata and controls
173 lines (150 loc) · 5.02 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
package selfupdate
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/creativeprojects/go-selfupdate/internal"
"github.com/creativeprojects/go-selfupdate/update"
)
type UpdateToOpt struct {
Rel *Release
RelExe,
CmdPath string
}
type UpdateCommandOpt struct {
RelExe,
CmdPath,
Current string
Repository Repository
}
type UpdateSelfOpt struct {
RelExe,
Current string
Repository Repository
}
// UpdateTo downloads an executable from the source provider and replace current binary with the downloaded one.
// It downloads a release asset via the source provider so this function is available for update releases on private repository.
func (up *Updater) UpdateTo(ctx context.Context, opt UpdateToOpt) error {
if opt.Rel == nil {
return ErrInvalidRelease
}
if opt.RelExe == "" {
_, opt.RelExe = filepath.Split(opt.CmdPath)
}
data, err := up.download(ctx, opt.Rel, opt.Rel.AssetID)
if err != nil {
return fmt.Errorf("failed to read asset %q: %w", opt.Rel.AssetName, err)
}
if up.validator != nil {
err = up.validate(ctx, opt.Rel, data)
if err != nil {
return err
}
}
return up.decompressAndUpdate(bytes.NewReader(data), opt.Rel.AssetURL, opt.RelExe, opt.CmdPath)
}
// UpdateCommand updates a given command binary to the latest version.
// 'opt.Current' is used to check the latest version against the current version.
func (up *Updater) UpdateCommand(ctx context.Context, opt UpdateCommandOpt) (*Release, error) {
version, err := semver.NewVersion(opt.Current)
if err != nil {
return nil, fmt.Errorf("incorrect version %q: %w", opt.Current, err)
}
if up.os == "windows" && !strings.HasSuffix(opt.CmdPath, ".exe") {
// Ensure to add '.exe' to given path on Windows
opt.CmdPath = opt.CmdPath + ".exe"
}
stat, err := os.Lstat(opt.CmdPath)
if err != nil {
return nil, fmt.Errorf("failed to stat '%s'. file may not exist: %s", opt.CmdPath, err)
}
if stat.Mode()&os.ModeSymlink != 0 {
p, err := internal.ResolvePath(opt.CmdPath)
if err != nil {
return nil, fmt.Errorf("failed to resolve symlink '%s' for executable: %s", opt.CmdPath, err)
}
opt.CmdPath = p
}
rel, ok, err := up.DetectLatest(ctx, DetectLatestOpt{opt.Repository})
if err != nil {
return nil, err
}
if !ok {
log.Print("No release detected. Current version is considered up-to-date")
return &Release{version: version}, nil
}
if version.Equal(rel.version) {
log.Printf("Current version %s is the latest. Update is not needed", version.String())
return rel, nil
}
log.Printf("Will update %s to the latest version %s", opt.CmdPath, rel.Version())
if err := up.UpdateTo(ctx, UpdateToOpt{rel, opt.RelExe, opt.CmdPath}); err != nil {
return nil, err
}
return rel, nil
}
// UpdateSelf updates the running executable itself to the latest version.
// 'current' is used to check the latest version against the current version.
func (up *Updater) UpdateSelf(ctx context.Context, opt UpdateSelfOpt) (*Release, error) {
cmdPath, err := internal.GetExecutablePath()
if err != nil {
return nil, err
}
return up.UpdateCommand(ctx, UpdateCommandOpt{opt.RelExe, cmdPath, opt.Current, opt.Repository})
}
func (up *Updater) decompressAndUpdate(src io.Reader, assetURL, relExe, cmdPath string) error {
asset, err := DecompressCommand(DecompressCommandOpt{src, assetURL, relExe, up.os, up.arch})
if err != nil {
return err
}
log.Printf("Will update %s to the latest downloaded from %s", cmdPath, assetURL)
return update.Apply(asset, update.Options{
TargetPath: cmdPath,
OldSavePath: up.oldSavePath,
})
}
// validate loads the validation file and passes it to the validator.
// The validation is successful if no error was returned
func (up *Updater) validate(ctx context.Context, rel *Release, data []byte) error {
if rel == nil {
return ErrInvalidRelease
}
// compatibility with setting rel.ValidationAssetID directly
if len(rel.ValidationChain) == 0 {
rel.ValidationChain = append(rel.ValidationChain, struct {
ValidationAssetID int64
ValidationAssetName, ValidationAssetURL string
}{
ValidationAssetID: rel.ValidationAssetID,
ValidationAssetName: "",
ValidationAssetURL: rel.ValidationAssetURL,
})
}
validationName := rel.AssetName
for _, va := range rel.ValidationChain {
validationData, err := up.download(ctx, rel, va.ValidationAssetID)
if err != nil {
return fmt.Errorf("failed reading validation data %q: %w", va.ValidationAssetName, err)
}
if err = up.validator.Validate(validationName, data, validationData); err != nil {
return fmt.Errorf("failed validating asset content %q: %w", validationName, err)
}
// Select what next to validate
validationName = va.ValidationAssetName
data = validationData
}
return nil
}
func (up *Updater) download(ctx context.Context, rel *Release, assetID int64) (data []byte, err error) {
var reader io.ReadCloser
if reader, err = up.source.DownloadReleaseAsset(ctx, rel, assetID); err == nil {
defer func() { _ = reader.Close() }()
data, err = io.ReadAll(reader)
}
return
}