66 "fmt"
77 "io"
88 "net/http"
9- "os"
10- "path/filepath"
119 "strings"
1210 "time"
1311
@@ -35,11 +33,10 @@ func (b *BuildInfo) MarshalJSON() ([]byte, error) {
3533}
3634
3735const (
38- scwDisableCheckVersionEnv = "SCW_DISABLE_CHECK_VERSION"
39- latestGithubReleaseURL = "https://api.github.com/repos/scaleway/scaleway-cli/releases/latest"
40- latestVersionUpdateFileLocalName = "latest-cli-version"
41- latestVersionRequestTimeout = 1 * time .Second
42- userAgentPrefix = "scaleway-cli"
36+ scwDisableCheckVersionEnv = "SCW_DISABLE_CHECK_VERSION"
37+ latestGithubReleaseURL = "https://api.github.com/repos/scaleway/scaleway-cli/releases/latest"
38+ latestVersionRequestTimeout = 1 * time .Second
39+ userAgentPrefix = "scaleway-cli"
4340)
4441
4542// IsRelease returns true when the version of the CLI is an official release:
@@ -57,28 +54,11 @@ func (b *BuildInfo) GetUserAgent() string {
5754}
5855
5956func (b * BuildInfo ) checkVersion (ctx context.Context ) {
60- cmd := extractMeta (ctx ).command
61- cmdDisableCheckVersion := cmd != nil && cmd .DisableVersionCheck
62- if ! b .IsRelease () || ExtractEnv (ctx , scwDisableCheckVersionEnv ) == "true" || cmdDisableCheckVersion {
57+ if ! b .IsRelease () || ExtractEnv (ctx , scwDisableCheckVersionEnv ) == "true" {
6358 ExtractLogger (ctx ).Debug ("skipping check version" )
6459 return
6560 }
6661
67- latestVersionUpdateFilePath := getLatestVersionUpdateFilePath (ExtractCacheDir (ctx ))
68-
69- // do nothing if last refresh at during the last 24h
70- if wasFileModifiedLast24h (latestVersionUpdateFilePath ) {
71- ExtractLogger (ctx ).Debug ("version was already checked during past 24 hours" )
72- return
73- }
74-
75- // do nothing if we cannot create the file
76- err := createAndCloseFile (latestVersionUpdateFilePath )
77- if err != nil {
78- ExtractLogger (ctx ).Debug (err .Error ())
79- return
80- }
81-
8262 // pull latest version
8363 latestVersion , err := getLatestVersion (ExtractHTTPClient (ctx ))
8464 if err != nil {
@@ -87,16 +67,12 @@ func (b *BuildInfo) checkVersion(ctx context.Context) {
8767 }
8868
8969 if b .Version .LessThan (latestVersion ) {
90- ExtractLogger (ctx ).Warningf ("a new version of scw is available (%s), beware that you are currently running %s\n " , latestVersion , b .Version )
70+ ExtractLogger (ctx ).Warningf ("A new version of scw is available (%s), beware that you are currently running %s\n " , latestVersion , b .Version )
9171 } else {
9272 ExtractLogger (ctx ).Debugf ("version is up to date (%s)\n " , b .Version )
9373 }
9474}
9575
96- func getLatestVersionUpdateFilePath (cacheDir string ) string {
97- return filepath .Join (cacheDir , latestVersionUpdateFileLocalName )
98- }
99-
10076// getLatestVersion attempt to read the latest version of the remote file at latestVersionFileURL.
10177func getLatestVersion (client * http.Client ) (* version.Version , error ) {
10278 ctx , cancelTimeout := context .WithTimeout (context .Background (), latestVersionRequestTimeout )
@@ -129,29 +105,3 @@ func getLatestVersion(client *http.Client) (*version.Version, error) {
129105
130106 return version .NewSemver (strings .TrimPrefix (jsonBody .TagName , "v" ))
131107}
132-
133- // wasFileModifiedLast24h checks whether the file has been updated during last 24 hours.
134- func wasFileModifiedLast24h (path string ) bool {
135- stat , err := os .Stat (path )
136- if err != nil {
137- return false
138- }
139-
140- yesterday := time .Now ().AddDate (0 , 0 , - 1 )
141- lastUpdate := stat .ModTime ()
142- return lastUpdate .After (yesterday )
143- }
144-
145- // createAndCloseFile creates a file and closes it. It returns true on succeed, false on failure.
146- func createAndCloseFile (path string ) error {
147- err := os .MkdirAll (filepath .Dir (path ), 0700 )
148- if err != nil {
149- return fmt .Errorf ("failed creating path %s: %s" , path , err )
150- }
151- newFile , err := os .OpenFile (path , os .O_CREATE | os .O_TRUNC | os .O_RDWR , 0600 )
152- if err != nil {
153- return fmt .Errorf ("failed creating file %s: %s" , path , err )
154- }
155-
156- return newFile .Close ()
157- }
0 commit comments