-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
185 lines (162 loc) · 4.72 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
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
177
178
179
180
181
182
183
184
185
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package install
import (
"fmt"
"io"
"net/http"
"runtime/debug"
"strings"
"sync"
"github.com/dmotylev/goproperties"
"github.com/getgauge/gauge/config"
"github.com/getgauge/gauge/logger"
"github.com/getgauge/gauge/plugin"
"github.com/getgauge/gauge/plugin/pluginInfo"
"github.com/getgauge/gauge/version"
)
type UpdateFacade struct {
wg *sync.WaitGroup
print chan bool
}
func (u *UpdateFacade) BufferUpdateDetails() {
var wg sync.WaitGroup
u.print = make(chan bool)
u.wg = &wg
u.wg.Add(1)
go printUpdateInfo(u.print, u.wg)
}
func (u *UpdateFacade) PrintUpdateBuffer() {
u.print <- true
u.wg.Wait()
}
func PrintUpdateInfoWithDetails() {
updates := checkUpdates()
if len(updates) > 0 {
for _, update := range updates {
logger.Infof(true, fmt.Sprintf("%-10s\t\t%-10s\t%s", update.Name, update.CompatibleVersion, update.Message))
}
} else {
logger.Infof(true, "No Updates available.")
}
}
func checkUpdates() []UpdateInfo {
return append(checkGaugeUpdate(), checkPluginUpdates()...)
}
func recoverPanic() {
if r := recover(); r != nil {
logger.Fatalf(true, "%v\n%s", r, string(debug.Stack()))
}
}
func printUpdateInfo(print chan bool, wg *sync.WaitGroup) {
message := make(chan string)
go func() {
defer recoverPanic()
updates := checkUpdates()
if len(updates) > 0 {
message <- "Updates are available. Run `gauge update -c` for more info."
}
}()
waitToPrint(message, print, "", wg)
}
func waitToPrint(messageChan chan string, printChan chan bool, message string, wg *sync.WaitGroup) {
select {
case <-printChan:
if message != "" {
logger.Infof(true, message)
}
wg.Done()
case message = <-messageChan:
waitToPrint(messageChan, printChan, message, wg)
}
}
func checkGaugeUpdate() []UpdateInfo {
var updateInfos []UpdateInfo
url := config.GaugeUpdateUrl()
if qp := plugin.QueryParams(); qp != "" {
url += qp
}
v, err := getLatestGaugeVersion(url)
if err != nil {
return updateInfos
}
latestVersion, err := version.ParseVersion(v)
if err != nil {
return updateInfos
}
isLatestVersion := version.CurrentGaugeVersion.IsLesserThan(latestVersion)
if isLatestVersion {
updateInfos = append(updateInfos, UpdateInfo{"Gauge", latestVersion.String(), "Download the installer from https://gauge.org/get-started/"})
}
return updateInfos
}
type UpdateInfo struct {
Name string
CompatibleVersion string
Message string
}
func checkPluginUpdates() []UpdateInfo {
var pluginsToUpdate []UpdateInfo
plugins, err := pluginInfo.GetAllInstalledPluginsWithVersion()
if err != nil {
return pluginsToUpdate
}
logger.Debugf(true, "Checking updates...")
for _, plugin := range plugins {
desc, result := getInstallDescription(plugin.Name, true)
if result.Error != nil {
continue
}
pluginsToUpdate = append(pluginsToUpdate, createPluginUpdateDetail(plugin.Version.String(), *desc)...)
}
return pluginsToUpdate
}
func createPluginUpdateDetail(currentVersion string, latestVersionDetails installDescription) []UpdateInfo {
var updateInfo []UpdateInfo
v, err := version.ParseVersion(currentVersion)
if err != nil {
return updateInfo
}
v1, err := version.ParseVersion(latestVersionDetails.Versions[0].Version)
if err != nil {
return updateInfo
}
if v.IsLesserThan(v1) {
versionDesc, err := latestVersionDetails.getLatestCompatibleVersionTo(version.CurrentGaugeVersion)
if err != nil {
return updateInfo
}
updateInfo = append(updateInfo, UpdateInfo{latestVersionDetails.Name, versionDesc.Version, fmt.Sprintf("Run 'gauge update %s'", latestVersionDetails.Name)})
}
return updateInfo
}
var getLatestGaugeVersion = func(url string) (string, error) {
res, err := http.Get(url)
if err != nil {
return "", err
}
defer res.Body.Close()
v, err := getGaugeVersionProperty(res.Body)
if err != nil {
return "", err
}
return v, nil
}
func getGaugeVersionProperty(r io.Reader) (string, error) {
properties := make(properties.Properties)
err := properties.Load(r)
if err != nil {
return "", fmt.Errorf("Failed to parse: %s", err.Error())
}
return strings.TrimSpace(properties["version"]), nil
}