@@ -21,14 +21,11 @@ import (
21
21
"bytes"
22
22
"compress/gzip"
23
23
"crypto/sha256"
24
- "encoding/json"
25
24
"errors"
26
25
"fmt"
27
26
"io"
28
- "net/http"
29
27
"os"
30
28
"path/filepath"
31
- "runtime"
32
29
"strings"
33
30
34
31
"github.com/kr/binarydist"
@@ -62,10 +59,6 @@ import (
62
59
//
63
60
//
64
61
65
- const (
66
- plat = runtime .GOOS + "-" + runtime .GOARCH
67
- )
68
-
69
62
var errHashMismatch = errors .New ("new file hash mismatch after patch" )
70
63
var errDiffURLUndefined = errors .New ("DiffURL is not defined, I cannot fetch and apply patch, reverting to full bin" )
71
64
var up = update .New ()
@@ -156,16 +149,13 @@ func removeTempSuffixFromPath(path string) string {
156
149
// go updater.BackgroundRun()
157
150
// }
158
151
type Updater struct {
159
- CurrentVersion string // Currently running version.
160
- APIURL string // Base URL for API requests (json files).
161
- CmdName string // Command name is appended to the ApiURL like http://apiurl/CmdName/. This represents one binary.
162
- BinURL string // Base URL for full binary downloads.
163
- DiffURL string // Base URL for diff downloads.
164
- Dir string // Directory to store selfupdate state.
165
- Info struct {
166
- Version string
167
- Sha256 []byte
168
- }
152
+ CurrentVersion string // Currently running version.
153
+ APIURL string // Base URL for API requests (json files).
154
+ CmdName string // Command name is appended to the ApiURL like http://apiurl/CmdName/. This represents one binary.
155
+ BinURL string // Base URL for full binary downloads.
156
+ DiffURL string // Base URL for diff downloads.
157
+ Dir string // Directory to store selfupdate state.
158
+ Info * availableUpdateInfo // Information about the available update.
169
159
}
170
160
171
161
// BackgroundRun starts the update check and apply cycle.
@@ -187,18 +177,6 @@ func (u *Updater) BackgroundRun() error {
187
177
return nil
188
178
}
189
179
190
- func fetch (url string ) (io.ReadCloser , error ) {
191
- resp , err := http .Get (url )
192
- if err != nil {
193
- return nil , err
194
- }
195
- if resp .StatusCode != 200 {
196
- log .Errorf ("bad http status from %s: %v" , url , resp .Status )
197
- return nil , fmt .Errorf ("bad http status from %s: %v" , url , resp .Status )
198
- }
199
- return resp .Body , nil
200
- }
201
-
202
180
func verifySha (bin []byte , sha []byte ) bool {
203
181
h := sha256 .New ()
204
182
h .Write (bin )
@@ -261,18 +239,11 @@ func (u *Updater) fetchBin() ([]byte, error) {
261
239
}
262
240
263
241
func (u * Updater ) fetchInfo () error {
264
- r , err := fetch (u .APIURL + u .CmdName + "/" + plat + ".json" )
242
+ info , err := fetchInfo (u .APIURL , u .CmdName )
265
243
if err != nil {
266
244
return err
267
245
}
268
- defer r .Close ()
269
- err = json .NewDecoder (r ).Decode (& u .Info )
270
- if err != nil {
271
- return err
272
- }
273
- if len (u .Info .Sha256 ) != sha256 .Size {
274
- return errors .New ("bad cmd hash in info" )
275
- }
246
+ u .Info = info
276
247
return nil
277
248
}
278
249
0 commit comments