@@ -21,14 +21,12 @@ 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
27
"net/http"
29
28
"os"
30
29
"path/filepath"
31
- "runtime"
32
30
"strings"
33
31
34
32
"github.com/kr/binarydist"
@@ -62,10 +60,6 @@ import (
62
60
//
63
61
//
64
62
65
- const (
66
- plat = runtime .GOOS + "-" + runtime .GOARCH
67
- )
68
-
69
63
var errHashMismatch = errors .New ("new file hash mismatch after patch" )
70
64
var errDiffURLUndefined = errors .New ("DiffURL is not defined, I cannot fetch and apply patch, reverting to full bin" )
71
65
var up = update .New ()
@@ -156,16 +150,13 @@ func removeTempSuffixFromPath(path string) string {
156
150
// go updater.BackgroundRun()
157
151
// }
158
152
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
- }
153
+ CurrentVersion string // Currently running version.
154
+ APIURL string // Base URL for API requests (json files).
155
+ CmdName string // Command name is appended to the ApiURL like http://apiurl/CmdName/. This represents one binary.
156
+ BinURL string // Base URL for full binary downloads.
157
+ DiffURL string // Base URL for diff downloads.
158
+ Dir string // Directory to store selfupdate state.
159
+ Info * availableUpdateInfo // Information about the available update.
169
160
}
170
161
171
162
// BackgroundRun starts the update check and apply cycle.
@@ -187,18 +178,6 @@ func (u *Updater) BackgroundRun() error {
187
178
return nil
188
179
}
189
180
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
181
func verifySha (bin []byte , sha []byte ) bool {
203
182
h := sha256 .New ()
204
183
h .Write (bin )
@@ -261,18 +240,11 @@ func (u *Updater) fetchBin() ([]byte, error) {
261
240
}
262
241
263
242
func (u * Updater ) fetchInfo () error {
264
- r , err := fetch (u .APIURL + u .CmdName + "/" + plat + ".json" )
243
+ info , err := fetchInfo (u .APIURL , u .CmdName )
265
244
if err != nil {
266
245
return err
267
246
}
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
- }
247
+ u .Info = info
276
248
return nil
277
249
}
278
250
0 commit comments