Skip to content

Commit 0934a84

Browse files
committed
Merge pull request #275 from QuentinPerez/daily
Daily
2 parents b8bf0f6 + 7cfde70 commit 0934a84

File tree

10 files changed

+934
-3
lines changed

10 files changed

+934
-3
lines changed

Godeps/Godeps.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ REL_PACKAGES := $(subst $(GODIR),./,$(PACKAGES))
3535
VERSION = $(shell cat .goxc.json | grep "PackageVersion" | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
3636
REV = $(shell git rev-parse HEAD || echo "nogit")
3737
TAG = $(shell git describe --tags --always || echo $(VERSION) || echo "nogit")
38-
LDFLAGS = "-X github.com/scaleway/scaleway-cli/pkg/scwversion.GITCOMMIT=$(REV) \
39-
-X github.com/scaleway/scaleway-cli/pkg/scwversion.VERSION=$(TAG)"
38+
LDFLAGS = "-X github.com/scaleway/scaleway-cli/pkg/scwversion.GITCOMMIT=$(REV)"
4039
BUILDER = scaleway-cli-builder
4140

4241
# Check go version

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,9 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11571157

11581158
### master (unreleased)
11591159

1160+
* Add `SCW_NOCHECKVERSION=1` env var to disable the check of scw version
11601161
* Add User-Agent with scw version ([#269](https://github.com/scaleway/scaleway-cli/issues/269))
1162+
* Add daily check to update scw ([#268](https://github.com/scaleway/scaleway-cli/issues/268))
11611163

11621164
View full [commits list](https://github.com/scaleway/scaleway-cli/compare/v1.7.0...master)
11631165

pkg/cli/main.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ package cli
77
import (
88
"fmt"
99
"io/ioutil"
10+
"net/http"
1011
"os"
12+
"path/filepath"
13+
"strings"
14+
"time"
1115

1216
"github.com/Sirupsen/logrus"
1317
flag "github.com/docker/docker/pkg/mflag"
18+
"github.com/hashicorp/go-version"
1419

1520
"github.com/scaleway/scaleway-cli/pkg/api"
1621
"github.com/scaleway/scaleway-cli/pkg/commands"
@@ -31,14 +36,14 @@ var (
3136

3237
// Start is the entrypoint
3338
func Start(rawArgs []string, streams *commands.Streams) (int, error) {
39+
checkVersion()
3440
if streams == nil {
3541
streams = &commands.Streams{
3642
Stdin: os.Stdin,
3743
Stdout: os.Stdout,
3844
Stderr: os.Stderr,
3945
}
4046
}
41-
4247
flag.CommandLine.Parse(rawArgs)
4348

4449
config, cfgErr := config.GetConfig()
@@ -159,3 +164,70 @@ func initLogging(debug bool, verbose bool, streams *commands.Streams) {
159164
logrus.SetLevel(logrus.WarnLevel)
160165
}
161166
}
167+
168+
func checkVersion() {
169+
if os.Getenv("SCW_NOCHECKVERSION") != "1" {
170+
return
171+
}
172+
homeDir, err := config.GetHomeDir()
173+
if err != nil {
174+
return
175+
}
176+
updateFiles := []string{"/var/run/.scw-update", "/tmp/.scw-update", filepath.Join(homeDir, ".scw-update")}
177+
updateFile := ""
178+
179+
callAPI := false
180+
for _, file := range updateFiles {
181+
if stat, err := os.Stat(file); err == nil {
182+
updateFile = file
183+
callAPI = stat.ModTime().Before(time.Now().AddDate(0, 0, -1))
184+
break
185+
}
186+
}
187+
if updateFile == "" {
188+
for _, file := range updateFiles {
189+
if scwupdate, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600); err == nil {
190+
scwupdate.Close()
191+
updateFile = file
192+
callAPI = true
193+
break
194+
}
195+
}
196+
}
197+
if callAPI {
198+
scwupdate, err := os.OpenFile(updateFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
199+
if err != nil {
200+
return
201+
}
202+
scwupdate.Close()
203+
req := http.Client{
204+
Timeout: time.Duration(1 * time.Second),
205+
}
206+
resp, err := req.Get("https://fr-1.storage.online.net/scaleway/scaleway-cli/VERSION")
207+
if resp != nil {
208+
defer resp.Body.Close()
209+
}
210+
if err != nil {
211+
return
212+
}
213+
body, err := ioutil.ReadAll(resp.Body)
214+
if err != nil {
215+
return
216+
}
217+
if scwversion.VERSION == "" {
218+
return
219+
}
220+
ver := scwversion.VERSION
221+
if ver[0] == 'v' {
222+
ver = string([]byte(ver)[1:])
223+
}
224+
actual, err1 := version.NewVersion(ver)
225+
update, err2 := version.NewVersion(strings.Trim(string(body), "\n"))
226+
if err1 != nil || err2 != nil {
227+
return
228+
}
229+
if actual.LessThan(update) {
230+
logrus.Infof("A new version of scw is available (%v), beware that you are currently running %v", update, actual)
231+
}
232+
}
233+
}

vendor/github.com/hashicorp/go-version/.travis.yml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)