-
Notifications
You must be signed in to change notification settings - Fork 156
feat(core): print a warning when new release is available #665
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,20 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
| "net/http" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/hashicorp/go-version" | ||
| "github.com/scaleway/scaleway-sdk-go/logger" | ||
| "github.com/scaleway/scaleway-sdk-go/scw" | ||
| ) | ||
|
|
||
| type BuildInfo struct { | ||
| Version string | ||
| Version *version.Version | ||
| BuildDate string | ||
| GoVersion string | ||
| GitBranch string | ||
|
|
@@ -14,9 +23,100 @@ type BuildInfo struct { | |
| GoOS string | ||
| } | ||
|
|
||
| const ( | ||
| scwDisableCheckVersionEnv = "SCW_DISABLE_CHECK_VERSION" | ||
kindermoumoute marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to document it somewhere |
||
| latestVersionFileURL = "https://scw-devtools.s3.nl-ams.scw.cloud/scw-cli-v2-version" | ||
| latestVersionUpdateFileLocalName = "latest-cli-version" | ||
| latestVersionRequestTimeout = 1 * time.Second | ||
| ) | ||
|
|
||
| // IsRelease returns true when the version of the CLI is an official release: | ||
| // - version must be non-empty (exclude tests) | ||
| // - version must not contain label (e.g. '+dev') | ||
| // - version must not contain metadata (e.g. '+dev') | ||
| func (b *BuildInfo) IsRelease() bool { | ||
| return b.Version != "" && !strings.Contains(b.Version, "+") | ||
| return b.Version != nil && b.Version.Metadata() == "" | ||
| } | ||
|
|
||
| func (b *BuildInfo) checkVersion() { | ||
| if !b.IsRelease() || os.Getenv(scwDisableCheckVersionEnv) == "true" { | ||
| logger.Debugf("skipping check version") | ||
| return | ||
| } | ||
|
|
||
| latestVersionUpdateFilePath := getLatestVersionUpdateFilePath() | ||
|
|
||
| // do nothing if last refresh at during the last 24h | ||
| if wasFileModifiedLast24h(latestVersionUpdateFilePath) { | ||
| logger.Debugf("version was already checked during past 24 hours") | ||
| return | ||
| } | ||
|
|
||
| // do nothing if we cannot create the file | ||
| if !createAndCloseFile(latestVersionUpdateFilePath) { | ||
| return | ||
| } | ||
|
|
||
| // pull latest version | ||
| latestVersion, err := getLatestVersion() | ||
| if err != nil { | ||
| logger.Debugf("failed to retrieve latest version: %s", err) | ||
| return | ||
| } | ||
|
|
||
| if b.Version.LessThan(latestVersion) { | ||
| logger.Infof("a new version of scw is available (%s), beware that you are currently running %v", latestVersion, b.Version) | ||
| } else { | ||
| logger.Debugf("version is up to date (%s)", b.Version) | ||
| } | ||
| } | ||
|
|
||
| func getLatestVersionUpdateFilePath() string { | ||
| return filepath.Join(scw.GetCacheDirectory(), latestVersionUpdateFileLocalName) | ||
| } | ||
|
|
||
| // getLatestVersion attempt to read the latest version of the remote file at latestVersionFileURL. | ||
| func getLatestVersion() (*version.Version, error) { | ||
| resp, err := (&http.Client{ | ||
| Timeout: latestVersionRequestTimeout, | ||
| }).Get(latestVersionFileURL) | ||
| if resp != nil { | ||
| defer resp.Body.Close() | ||
| } | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| body, err := ioutil.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return version.NewSemver(strings.Trim(string(body), "\n")) | ||
| } | ||
|
|
||
| // wasFileModifiedLast24h checks whether the file has been updated during last 24 hours. | ||
| func wasFileModifiedLast24h(path string) bool { | ||
| stat, err := os.Stat(path) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| yesterday := time.Now().AddDate(0, 0, -1) | ||
| lastUpdate := stat.ModTime() | ||
| return lastUpdate.After(yesterday) | ||
| } | ||
|
|
||
| // createAndCloseFile creates a file and closes it. It returns true on succeed, false on failure. | ||
| func createAndCloseFile(path string) bool { | ||
| err := os.MkdirAll(filepath.Dir(path), 0700) | ||
| if err != nil { | ||
| logger.Debugf("failed creating path %s: %s", path, err) | ||
| } | ||
| newFile, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600) | ||
| if err != nil { | ||
| logger.Debugf("failed creating file %s: %s", path, err) | ||
| return false | ||
| } | ||
|
|
||
| newFile.Close() | ||
| return true | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/go-version" | ||
| "github.com/scaleway/scaleway-cli/internal/args" | ||
| ) | ||
|
|
||
| var fakeCommand = &Command{ | ||
| Namespace: "plop", | ||
| DisableTelemetry: true, | ||
| ArgsType: reflect.TypeOf(args.RawArgs{}), | ||
| Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) { | ||
| return &SuccessResult{}, nil | ||
| }, | ||
| } | ||
|
|
||
| func deleteLatestVersionUpdateFile(*BeforeFuncCtx) error { | ||
| os.Remove(getLatestVersionUpdateFilePath()) | ||
| return nil | ||
| } | ||
|
|
||
| func Test_CheckVersion(t *testing.T) { | ||
| t.Run("Outdated version", Test(&TestConfig{ | ||
| Commands: NewCommands(fakeCommand), | ||
| BuildInfo: BuildInfo{ | ||
| Version: version.Must(version.NewSemver("v1.20")), | ||
| }, | ||
| BeforeFunc: deleteLatestVersionUpdateFile, | ||
| Cmd: "scw plop", | ||
| Check: TestCheckCombine( | ||
| TestCheckStderrGolden(), | ||
| ), | ||
| })) | ||
|
|
||
| t.Run("Up to date version", Test(&TestConfig{ | ||
| Commands: NewCommands(fakeCommand), | ||
| BuildInfo: BuildInfo{ | ||
| Version: version.Must(version.NewSemver("v99.99")), | ||
| }, | ||
| BeforeFunc: deleteLatestVersionUpdateFile, | ||
| Cmd: "scw plop -D", | ||
| Check: TestCheckCombine( | ||
| TestCheckStderrGolden(), | ||
| ), | ||
| })) | ||
|
|
||
| t.Run("Already checked", Test(&TestConfig{ | ||
| Commands: NewCommands(fakeCommand), | ||
| BuildInfo: BuildInfo{ | ||
| Version: version.Must(version.NewSemver("v1.0")), | ||
| }, | ||
| BeforeFunc: func(ctx *BeforeFuncCtx) error { | ||
| if createAndCloseFile(getLatestVersionUpdateFilePath()) { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("failed to create latestVersionUpdateFile") | ||
| }, | ||
| Cmd: "scw plop -D", | ||
| Check: TestCheckCombine( | ||
| TestCheckStderrGolden(), | ||
| ), | ||
| })) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
internal/core/testdata/test-check-version-already-checked.stderr.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| DEBUG: 2019/12/09 16:04:07 marshalling type '*core.SuccessResult' | ||
| DEBUG: 2019/12/09 16:04:07 version was already checked during past 24 hours | ||
| DEBUG: 2019/12/09 16:04:07 skipping telemetry report |
1 change: 1 addition & 0 deletions
1
internal/core/testdata/test-check-version-outdated-version.stderr.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| INFO: 2019/12/09 16:04:07 a new version of scw is available (2.0.0-alpha1), beware that you are currently running 1.20.0 |
3 changes: 3 additions & 0 deletions
3
internal/core/testdata/test-check-version-up-to-date-version.stderr.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| DEBUG: 2019/12/09 16:04:07 marshalling type '*core.SuccessResult' | ||
| DEBUG: 2019/12/09 16:04:07 version is up to date (99.99.0) | ||
| DEBUG: 2019/12/09 16:04:07 skipping telemetry report |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add a newline