Skip to content

Commit 0aafda1

Browse files
committed
Factored fetchInfo function to retrieve information about the latest update
1 parent f36e389 commit 0aafda1

File tree

2 files changed

+59
-38
lines changed

2 files changed

+59
-38
lines changed

updater/updater.go

+50
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515

1616
package updater
1717

18+
import (
19+
"crypto/sha256"
20+
"encoding/json"
21+
"errors"
22+
"fmt"
23+
"io"
24+
"net/http"
25+
"runtime"
26+
27+
log "github.com/sirupsen/logrus"
28+
)
29+
1830
// Start checks if an update has been downloaded and if so returns the path to the
1931
// binary to be executed to perform the update. If no update has been downloaded
2032
// it returns an empty string.
@@ -27,3 +39,41 @@ func Start(src string) string {
2739
func CheckForUpdates(currentVersion string, updateAPIURL, updateBinURL string, cmdName string) (string, error) {
2840
return checkForUpdates(currentVersion, updateAPIURL, updateBinURL, cmdName)
2941
}
42+
43+
const (
44+
plat = runtime.GOOS + "-" + runtime.GOARCH
45+
)
46+
47+
func fetchInfo(updateAPIURL string, cmdName string) (*availableUpdateInfo, error) {
48+
r, err := fetch(updateAPIURL + cmdName + "/" + plat + ".json")
49+
if err != nil {
50+
return nil, err
51+
}
52+
defer r.Close()
53+
54+
var res availableUpdateInfo
55+
if err := json.NewDecoder(r).Decode(&res); err != nil {
56+
return nil, err
57+
}
58+
if len(res.Sha256) != sha256.Size {
59+
return nil, errors.New("bad cmd hash in info")
60+
}
61+
return &res, nil
62+
}
63+
64+
type availableUpdateInfo struct {
65+
Version string
66+
Sha256 []byte
67+
}
68+
69+
func fetch(url string) (io.ReadCloser, error) {
70+
resp, err := http.Get(url)
71+
if err != nil {
72+
return nil, err
73+
}
74+
if resp.StatusCode != 200 {
75+
log.Errorf("bad http status from %s: %v", url, resp.Status)
76+
return nil, fmt.Errorf("bad http status from %s: %v", url, resp.Status)
77+
}
78+
return resp.Body, nil
79+
}

updater/updater_default.go

+9-38
Original file line numberDiff line numberDiff line change
@@ -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-
6962
var errHashMismatch = errors.New("new file hash mismatch after patch")
7063
var errDiffURLUndefined = errors.New("DiffURL is not defined, I cannot fetch and apply patch, reverting to full bin")
7164
var up = update.New()
@@ -156,16 +149,13 @@ func removeTempSuffixFromPath(path string) string {
156149
// go updater.BackgroundRun()
157150
// }
158151
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.
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-
202180
func 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

263241
func (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

Comments
 (0)