-
Notifications
You must be signed in to change notification settings - Fork 24
/
check.go
72 lines (60 loc) · 1.36 KB
/
check.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
package update
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/Scalingo/cli/config"
"github.com/Scalingo/cli/io"
"gopkg.in/errgo.v1"
)
var (
lastVersionURL = "https://cli-dl.scalingo.io/version"
lastVersion = ""
gotLastVersion = make(chan struct{})
gotAnError = false
)
func init() {
go func() {
var err error
lastVersion, err = getLastVersion()
if err != nil {
config.C.Logger.Println(err)
gotAnError = true
}
close(gotLastVersion)
}()
}
func Check() error {
version := config.Version
if strings.HasSuffix(version, "dev") {
fmt.Println("\nNo update checking, dev version:", version)
return nil
}
<-gotLastVersion
if gotAnError {
return errgo.New("Update checker: connection error")
}
if version == lastVersion {
return nil
}
io.Statusf(io.BoldRed("Your Scalingo client (%s) is out-of-date: some features may not work correctly.\n"), version)
io.Infof(io.BoldRed("Please update to '%s' by reinstalling it: https://cli.scalingo.com\n"), lastVersion)
return nil
}
func getLastVersion() (string, error) {
client := http.Client{
Timeout: 4 * time.Second,
}
res, err := client.Get(lastVersionURL)
if err != nil {
return "", errgo.Mask(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", errgo.Mask(err)
}
return strings.TrimSpace(string(body)), nil
}