@@ -21,14 +21,11 @@ import (
2121 "bytes"
2222 "compress/gzip"
2323 "crypto/sha256"
24- "encoding/json"
2524 "errors"
2625 "fmt"
2726 "io"
28- "net/http"
2927 "os"
3028 "path/filepath"
31- "runtime"
3229 "strings"
3330
3431 "github.com/kr/binarydist"
@@ -62,10 +59,6 @@ import (
6259//
6360//
6461
65- const (
66- plat = runtime .GOOS + "-" + runtime .GOARCH
67- )
68-
6962var errHashMismatch = errors .New ("new file hash mismatch after patch" )
7063var errDiffURLUndefined = errors .New ("DiffURL is not defined, I cannot fetch and apply patch, reverting to full bin" )
7164var up = update .New ()
@@ -156,16 +149,13 @@ func removeTempSuffixFromPath(path string) string {
156149// go updater.BackgroundRun()
157150// }
158151type 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.
169159}
170160
171161// BackgroundRun starts the update check and apply cycle.
@@ -187,18 +177,6 @@ func (u *Updater) BackgroundRun() error {
187177 return nil
188178}
189179
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-
202180func verifySha (bin []byte , sha []byte ) bool {
203181 h := sha256 .New ()
204182 h .Write (bin )
@@ -261,18 +239,11 @@ func (u *Updater) fetchBin() ([]byte, error) {
261239}
262240
263241func (u * Updater ) fetchInfo () error {
264- r , err := fetch (u .APIURL + u .CmdName + "/" + plat + ".json" )
242+ info , err := fetchInfo (u .APIURL , u .CmdName )
265243 if err != nil {
266244 return err
267245 }
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
276247 return nil
277248}
278249
0 commit comments