-
Notifications
You must be signed in to change notification settings - Fork 15
/
update.go
152 lines (132 loc) · 4.63 KB
/
update.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
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
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"
)
// 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, rel *Release, cmdPath string) error {
if rel == nil {
return ErrInvalidRelease
}
data, err := up.download(ctx, rel, rel.AssetID)
if err != nil {
return fmt.Errorf("failed to read asset %q: %w", rel.AssetName, err)
}
if up.validator != nil {
err = up.validate(ctx, rel, data)
if err != nil {
return err
}
}
return up.decompressAndUpdate(bytes.NewReader(data), rel.AssetName, rel.AssetURL, cmdPath)
}
// UpdateCommand updates a given command binary to the latest version.
// 'current' is used to check the latest version against the current version.
func (up *Updater) UpdateCommand(ctx context.Context, cmdPath string, current string, repository Repository) (*Release, error) {
version, err := semver.NewVersion(current)
if err != nil {
return nil, fmt.Errorf("incorrect version %q: %w", current, err)
}
if up.os == "windows" && !strings.HasSuffix(cmdPath, ".exe") {
// Ensure to add '.exe' to given path on Windows
cmdPath = cmdPath + ".exe"
}
stat, err := os.Lstat(cmdPath)
if err != nil {
return nil, fmt.Errorf("failed to stat '%s'. file may not exist: %s", cmdPath, err)
}
if stat.Mode()&os.ModeSymlink != 0 {
p, err := filepath.EvalSymlinks(cmdPath)
if err != nil {
return nil, fmt.Errorf("failed to resolve symlink '%s' for executable: %s", cmdPath, err)
}
cmdPath = p
}
rel, ok, err := up.DetectLatest(ctx, 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", cmdPath, rel.Version())
if err := up.UpdateTo(ctx, rel, 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, current string, repository Repository) (*Release, error) {
cmdPath, err := internal.GetExecutablePath()
if err != nil {
return nil, err
}
return up.UpdateCommand(ctx, cmdPath, current, repository)
}
func (up *Updater) decompressAndUpdate(src io.Reader, assetName, assetURL, cmdPath string) error {
_, cmd := filepath.Split(cmdPath)
asset, err := DecompressCommand(src, assetName, cmd, 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
}