-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdatecheck.go
More file actions
176 lines (159 loc) · 4.38 KB
/
updatecheck.go
File metadata and controls
176 lines (159 loc) · 4.38 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"errors"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/bitcart/go-github-selfupdate/selfupdate"
"github.com/blang/semver"
"github.com/briandowns/spinner"
)
type Options struct {
Current semver.Version
Found bool
Latest *selfupdate.Release
updater *selfupdate.Updater
githubAPI string
slug string
}
// hoursBeforeCheck is used to configure the delay between auto-update checks
var hoursBeforeCheck = 28
func ShouldCheckForUpdates(upd *UpdateCheck) bool {
diff := time.Since(upd.LastUpdateCheck)
return diff.Hours() >= float64(hoursBeforeCheck)
}
func updatesEnabled() bool {
return Version != "dev" && Version != "docker"
}
func skipUpdateByDefault() bool {
return !updatesEnabled() || os.Getenv("CI") == "true" ||
os.Getenv("BITCART_CLI_SKIP_UPDATE_CHECK") == "true"
}
func checkForUpdates(opts *Config) error {
if opts.SkipUpdateCheck {
return nil
}
updateCheck := &UpdateCheck{
LastUpdateCheck: time.Time{},
}
updateCheck.Load()
if ShouldCheckForUpdates(updateCheck) {
log := log.New(os.Stderr, "", 0)
slug := "bitcart/bitcart-cli"
spr := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
spr.Writer = os.Stderr
spr.Suffix = " Checking for updates..."
spr.Start()
check, err := CheckForUpdates(
opts.GitHubAPI,
slug,
Version,
)
if err != nil {
spr.Stop()
return err
}
if !check.Found {
spr.Suffix = "No updates found."
time.Sleep(300 * time.Millisecond)
spr.Stop()
updateCheck.LastUpdateCheck = time.Now()
updateCheck.WriteToDisk()
return nil
}
if IsLatestVersion(check) {
spr.Suffix = "Already up-to-date."
time.Sleep(300 * time.Millisecond)
spr.Stop()
updateCheck.LastUpdateCheck = time.Now()
updateCheck.WriteToDisk()
return nil
}
spr.Stop()
log.Println(ReportVersion(check))
log.Println(HowToUpdate(check))
log.Println("") // Print a new-line after all of that
updateCheck.LastUpdateCheck = time.Now()
updateCheck.WriteToDisk()
return nil
}
return nil
}
func CheckForUpdates(githubAPI, slug, current string) (*Options, error) {
var (
err error
check *Options
)
currentVersion, err := semver.Parse(current)
if err != nil {
return nil, errors.New("Failed to parse current version: " + err.Error())
}
check = &Options{
Current: currentVersion,
githubAPI: githubAPI,
slug: slug,
}
err = checkFromSource(check)
return check, err
}
func checkFromSource(check *Options) error {
updateConfig := selfupdate.Config{}
if check.githubAPI != "https://api.github.com" {
updateConfig.EnterpriseBaseURL = check.githubAPI
}
updater, err := selfupdate.NewUpdater(updateConfig)
if err != nil {
return err
}
check.updater = updater
err = latestRelease(check)
return err
}
func latestRelease(opts *Options) error {
latest, found, err := opts.updater.DetectLatest(opts.slug)
opts.Latest = latest
opts.Found = found
if err != nil {
return errors.New(`Failed to query the GitHub API for updates.
This is most likely due to GitHub rate-limiting on unauthenticated requests.
To have the bitcart-cli make authenticated requests please:
1. Generate a token at https://github.com/settings/tokens
2. Set the token by either adding it to your ~/.gitconfig or
setting the GITHUB_TOKEN environment variable.
Instructions for generating a token can be found at:
https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
We call the GitHub releases API to look for new releases.
More information about that API can be found here: https://developer.github.com/v3/repos/releases/
` + err.Error())
}
return nil
}
func IsLatestVersion(opts *Options) bool {
if opts.Current.String() == "" || opts.Latest == nil {
return true
}
return opts.Current.GE(opts.Latest.Version)
}
func ReportVersion(opts *Options) string {
return strings.Join([]string{
fmt.Sprintf("You are running %s", opts.Current),
fmt.Sprintf("A new release is available (%s)", opts.Latest.Version),
}, "\n")
}
func HowToUpdate(opts *Options) string {
switch Version {
case "docker", "dev":
return "Automatic updates are disabled"
default:
return "You can update with `bitcart-cli update install`"
}
}
func InstallLatest(opts *Options) (string, error) {
release, err := opts.updater.UpdateSelf(opts.Current, opts.slug)
if err != nil {
return "", errors.New("Failed to install update: " + err.Error())
}
return fmt.Sprintf("Updated to %s", release.Version), nil
}