-
Notifications
You must be signed in to change notification settings - Fork 14
/
latest.go
58 lines (42 loc) · 1.12 KB
/
latest.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
package mimic
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
type platform string
var (
errNoVersions = errors.New("mimic: no versions in chrome history")
PlatformWindows platform = "win"
)
type versionHistory struct {
Versions []versions `json:"versions"`
}
type versions struct {
Version string `json:"version"`
}
// GetLatestVersion returns the latest version of Chrome for the given platform.
func GetLatestVersion(pf platform) (string, error) {
res, err := http.Get(fmt.Sprintf("https://versionhistory.googleapis.com/v1/chrome/platforms/%s/channels/stable/versions", pf))
if err != nil {
return "", err
}
defer res.Body.Close()
data := versionHistory{}
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return "", err
}
if len(data.Versions) == 0 {
return "", errNoVersions
}
return data.Versions[0].Version, nil
}
// MustGetLatestVersion is like GetLatestVersion but panics on error.
func MustGetLatestVersion(pf platform) string {
version, err := GetLatestVersion(pf)
if err != nil {
panic(err)
}
return version
}