-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
update.go
135 lines (114 loc) · 3.44 KB
/
update.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package actions
import (
"encoding/json"
"fmt"
"github.com/analogj/drawbridge/pkg/config"
"github.com/analogj/drawbridge/pkg/errors"
"github.com/analogj/drawbridge/pkg/utils"
"github.com/analogj/drawbridge/pkg/version"
"github.com/fatih/color"
"github.com/inconshreveable/go-update"
"io/ioutil"
"net/http"
"os"
"runtime"
"time"
)
type UpdateAction struct {
Config config.Interface
}
type GithubReleaseInfo struct {
TagName string `json:"tag_name"`
PublishedAt time.Time `json:"published_at"`
ReleaseNotesUrl string `json:"html_url"`
Assets []struct {
Url string `json:"browser_download_url"`
Name string `json:"name"`
} `json:"assets"`
}
// https://github.com/hyperhq/hypercli/blob/302a6b530148f6a777cd6b8772f706ab5e3da46b/pkg/selfupdate/selfupdate.go
// https://github.com/hyperhq/hypercli/blob/302a6b530148f6a777cd6b8772f706ab5e3da46b/hyper/hyper.go
// https://github.com/inconshreveable/go-update
//
func (e *UpdateAction) Start() error {
releaseInfo, err := e.GetLatestReleaseInfo()
if err != nil {
return err
}
//compare the current version to the destination version
currentTimestamp, err := e.currentBinaryTimestamp()
if err != nil {
return err
}
fmt.Printf("Current: %v [%v]. Available: %v [%v]\nRelease notes are available here: %v\n",
e.currentBinaryVersion(),
currentTimestamp.Format("2006-01-02"),
releaseInfo.TagName,
releaseInfo.PublishedAt.Format("2006-01-02"),
releaseInfo.ReleaseNotesUrl,
)
if releaseInfo.TagName == e.currentBinaryVersion() {
return errors.UpdateNotAvailableError("No new version found.")
}
//see if theres a binary for this OS/Arch
assetUrl := ""
requiredOsArch := fmt.Sprintf("drawbridge-%v-%v", runtime.GOOS, runtime.GOARCH)
for _, asset := range releaseInfo.Assets {
if asset.Name == requiredOsArch {
assetUrl = asset.Url
}
}
if len(assetUrl) == 0 {
return errors.UpdateBinaryOsArchMissingError(fmt.Sprintf("Cannot find a drawbridge binary for OS/Arch: %v", requiredOsArch))
}
val := utils.StdinQueryBoolean(fmt.Sprintf("Are you sure you would like to update drawbridge to %v?\nPlease confirm [yes/no]:", releaseInfo.TagName))
if !val {
color.Red("Cancelled update operation.")
return nil
}
color.Yellow("Updating Drawbridge binary. Please wait...")
releaseBinaryReq, err := http.Get(assetUrl)
if err != nil {
return err
}
defer releaseBinaryReq.Body.Close()
err = update.Apply(releaseBinaryReq.Body, update.Options{})
if err != nil {
// error handling
return err
}
color.Green("Successfully updated Drawbridge")
return nil
}
func (e *UpdateAction) GetLatestReleaseInfo() (GithubReleaseInfo, error) {
latestReleaseReq, err := http.Get("https://api.github.com/repos/AnalogJ/drawbridge/releases/latest")
if err != nil {
return GithubReleaseInfo{}, err
}
defer latestReleaseReq.Body.Close()
respBodyJson, err := ioutil.ReadAll(latestReleaseReq.Body)
if err != nil {
return GithubReleaseInfo{}, err
}
//parse json
releaseInfo := GithubReleaseInfo{}
err = json.Unmarshal(respBodyJson, &releaseInfo)
if err != nil {
return GithubReleaseInfo{}, err
}
return releaseInfo, nil
}
func (e *UpdateAction) currentBinaryTimestamp() (time.Time, error) {
execPath, err := os.Executable()
if err != nil {
return time.Time{}, err
}
info, err := os.Stat(execPath)
if err != nil {
return time.Time{}, err
}
return info.ModTime(), err
}
func (e *UpdateAction) currentBinaryVersion() string {
return fmt.Sprintf("v%v", version.VERSION)
}