This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
/
version.go
101 lines (85 loc) · 2.29 KB
/
version.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package config
import (
"encoding/json"
"io/ioutil"
"net/http"
"runtime"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// The version variables will be set during build time, see build/build.sh
var (
BuildStamp string
Branch string
GitHash string
Version string
DefaultAOVersion = AOVersion{
Version: Version,
BuildStamp: BuildStamp,
Branch: Branch,
GitHash: GitHash,
}
)
const (
aoDownloadPath = "/assets/ao"
aoDownloadPathMacOs = "/assets/macos/ao"
aoDownloadPathWindows = "/assets/windows/ao.exe"
aoCurrentVersionPath = "/assets/version.json"
)
type AOVersion struct {
Version string `json:"version"`
Branch string `json:"branch"`
GitHash string `json:"gitHash"`
BuildStamp string `json:"buildStamp"`
}
func (v *AOVersion) IsNewVersion() bool {
// No new version if current version is dirty
if strings.Contains(Version, "-dirty") {
return false
}
// TODO: Should do better check then this
return v.Version != Version
}
func GetCurrentVersionFromServer(url string) (*AOVersion, error) {
data, err := fetchFromUpdateServer(url, aoCurrentVersionPath, "application/json")
if err != nil {
return nil, err
}
var aoVersion AOVersion
err = json.Unmarshal(data, &aoVersion)
if err != nil {
return nil, err
}
return &aoVersion, nil
}
func GetNewAOClient(url string) ([]byte, error) {
var downloadPath string
downloadPath = aoDownloadPath
if runtime.GOOS == "darwin" {
downloadPath = aoDownloadPathMacOs
}
if runtime.GOOS == "windows" {
downloadPath = aoDownloadPathWindows
}
return fetchFromUpdateServer(url, downloadPath, "application/octet-stream")
}
func fetchFromUpdateServer(url, endpoint, contentType string) ([]byte, error) {
logrus.WithField("url", url).WithField("endpoint", endpoint).Info("Request")
req, err := http.NewRequest(http.MethodGet, url+endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
logrus.WithField("url", url).WithField("status", res.StatusCode).Info("Response")
if res.StatusCode != http.StatusOK {
return nil, errors.New("Cannot reach update server: " + res.Status)
}
defer res.Body.Close()
file, err := ioutil.ReadAll(res.Body)
return file, err
}