-
Notifications
You must be signed in to change notification settings - Fork 10
/
updater.go
78 lines (61 loc) · 1.66 KB
/
updater.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
package deploy
import (
"fmt"
"runtime"
"strings"
"github.com/inconshreveable/go-update"
"github.com/octokit/go-octokit/octokit"
)
const GitHubHost = "github.com"
type Updater struct {
Host string
CurrentVersion string
}
func NewUpdater() *Updater {
version := Version
return &Updater{
Host: GitHubHost,
CurrentVersion: version,
}
}
func (updater *Updater) Update() (err error) {
releaseName, version := updater.latestReleaseNameAndVersion()
if version == "" {
fmt.Println("There is no newer version of Deploy available.")
return
}
if version == updater.CurrentVersion {
fmt.Printf("You're already on the latest version: %s\n", version)
} else {
err = updater.updateTo(releaseName, version)
}
return
}
func (updater *Updater) updateTo(releaseName, version string) (err error) {
downloadURL := fmt.Sprintf("https://%s/remind101/deploy/releases/download/%s/%s_%s_deploy", updater.Host, releaseName, runtime.GOOS, runtime.GOARCH)
fmt.Printf("Downloading %s...", version)
err, _ = update.New().FromUrl(downloadURL)
if err != nil {
fmt.Printf("Update failed: %v\n", err)
}
return
}
func (updater *Updater) latestReleaseNameAndVersion() (name, version string) {
client := octokit.NewClient(nil)
url, err := octokit.ReleasesURL.Expand(octokit.M{"owner": "remind101", "repo": "deploy"})
if err != nil {
return
}
releases, result := client.Releases(url).All()
if result.HasError() {
err = fmt.Errorf("Error getting Deploy release: %s", result.Err)
return
}
if len(releases) == 0 {
err = fmt.Errorf("No Deploy release is available")
return
}
name = releases[0].TagName
version = strings.TrimPrefix(name, "v")
return
}