Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daily #275

Merged
merged 6 commits into from
Jan 29, 2016
Merged

Daily #275

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ REL_PACKAGES := $(subst $(GODIR),./,$(PACKAGES))
VERSION = $(shell cat .goxc.json | grep "PackageVersion" | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
REV = $(shell git rev-parse HEAD || echo "nogit")
TAG = $(shell git describe --tags --always || echo $(VERSION) || echo "nogit")
LDFLAGS = "-X github.com/scaleway/scaleway-cli/pkg/scwversion.GITCOMMIT=$(REV) \
-X github.com/scaleway/scaleway-cli/pkg/scwversion.VERSION=$(TAG)"
LDFLAGS = "-X github.com/scaleway/scaleway-cli/pkg/scwversion.GITCOMMIT=$(REV)"
BUILDER = scaleway-cli-builder

# Check go version
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,9 @@ $ scw inspect myserver | jq '.[0].public_ip.address'

### master (unreleased)

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

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

Expand Down
74 changes: 73 additions & 1 deletion pkg/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ package cli
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"

"github.com/Sirupsen/logrus"
flag "github.com/docker/docker/pkg/mflag"
"github.com/hashicorp/go-version"

"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/commands"
Expand All @@ -31,14 +36,14 @@ var (

// Start is the entrypoint
func Start(rawArgs []string, streams *commands.Streams) (int, error) {
checkVersion()
if streams == nil {
streams = &commands.Streams{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}

flag.CommandLine.Parse(rawArgs)

config, cfgErr := config.GetConfig()
Expand Down Expand Up @@ -159,3 +164,70 @@ func initLogging(debug bool, verbose bool, streams *commands.Streams) {
logrus.SetLevel(logrus.WarnLevel)
}
}

func checkVersion() {
if os.Getenv("SCW_NOCHECKVERSION") != "1" {
return
}
homeDir, err := config.GetHomeDir()
if err != nil {
return
}
updateFiles := []string{"/var/run/.scw-update", "/tmp/.scw-update", filepath.Join(homeDir, ".scw-update")}
updateFile := ""

callAPI := false
for _, file := range updateFiles {
if stat, err := os.Stat(file); err == nil {
updateFile = file
callAPI = stat.ModTime().Before(time.Now().AddDate(0, 0, -1))
break
}
}
if updateFile == "" {
for _, file := range updateFiles {
if scwupdate, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600); err == nil {
scwupdate.Close()
updateFile = file
callAPI = true
break
}
}
}
if callAPI {
scwupdate, err := os.OpenFile(updateFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
if err != nil {
return
}
scwupdate.Close()
req := http.Client{
Timeout: time.Duration(1 * time.Second),
}
resp, err := req.Get("https://fr-1.storage.online.net/scaleway/scaleway-cli/VERSION")
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waiting for the http query before LGTMing

return
}
if scwversion.VERSION == "" {
return
}
ver := scwversion.VERSION
if ver[0] == 'v' {
ver = string([]byte(ver)[1:])
}
actual, err1 := version.NewVersion(ver)
update, err2 := version.NewVersion(strings.Trim(string(body), "\n"))
if err1 != nil || err2 != nil {
return
}
if actual.LessThan(update) {
logrus.Infof("A new version of scw is available (%v), beware that you are currently running %v", update, actual)
}
}
}
11 changes: 11 additions & 0 deletions vendor/github.com/hashicorp/go-version/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading