@@ -31,40 +31,56 @@ var warnOutdatedVersionOnce sync.Once
3131
3232// maybeWarnOutdatedVersion prints a warning to stderr when src-cli is older
3333// than the version recommended by the Sourcegraph instance at endpointURL,
34- // which it asks using client.
34+ // which it asks using client unless a recent answer is cached .
3535//
3636// It never fails the command: an unreachable instance, an unparseable version,
3737// or an unwritable cache all result in no warning.
3838func maybeWarnOutdatedVersion (client api.Client , endpointURL * url.URL , subcommand string ) {
39- if skipVersionCheck (subcommand ) {
39+ // 'src version' asks the instance itself, and then calls
40+ // warnOutdatedVersionFromLookup with the answer.
41+ if subcommand == "version" || skipVersionCheck () {
4042 return
4143 }
42- cacheDir , err := os . UserCacheDir ( )
44+ cachePath , err := recommendedVersionCachePath ( endpointURL )
4345 if err != nil {
4446 return
4547 }
46- cachePath := filepath .Join (cacheDir , "sourcegraph" , "src-cli" , "recommended-version-" + url .QueryEscape (endpointURL .String ()))
47- if warning := outdatedVersionWarning (client , endpointURL , cachePath ); warning != "" {
48- fmt .Fprintln (os .Stderr , warning )
48+ printOutdatedVersionWarning (recommendedVersion (client , cachePath ), endpointURL )
49+ }
50+
51+ // warnOutdatedVersionFromLookup is the check for 'src version', which has just
52+ // been told the recommended version by the instance at endpointURL. The answer
53+ // is cached, so that the commands that follow warn about the same version.
54+ func warnOutdatedVersionFromLookup (recommended string , endpointURL * url.URL ) {
55+ if skipVersionCheck () {
56+ return
4957 }
58+ if cachePath , err := recommendedVersionCachePath (endpointURL ); err == nil {
59+ _ = cacheRecommendedVersion (cachePath , recommended )
60+ }
61+ printOutdatedVersionWarning (recommended , endpointURL )
62+ }
63+
64+ // skipVersionCheck reports whether the version check is disabled, or has no
65+ // version to compare.
66+ func skipVersionCheck () bool {
67+ return os .Getenv ("SRC_SKIP_VERSION_CHECK" ) != "" || version .BuildTag == version .DefaultBuildTag
5068}
5169
52- // skipVersionCheck reports whether the version check is disabled, has no
53- // version to compare, or is redundant because the command is 'src version'.
54- func skipVersionCheck (subcommand string ) bool {
55- return os .Getenv ("SRC_SKIP_VERSION_CHECK" ) != "" ||
56- version .BuildTag == version .DefaultBuildTag ||
57- subcommand == "version"
70+ func printOutdatedVersionWarning (recommended string , endpointURL * url.URL ) {
71+ if warning := outdatedVersionWarning (recommended , endpointURL ); warning != "" {
72+ fmt .Fprintln (os .Stderr , warning )
73+ }
5874}
5975
6076// outdatedVersionWarning returns the warning to print, or "" when src-cli is
61- // not known to be older than the version the instance recommends .
62- func outdatedVersionWarning (client api. Client , endpointURL * url.URL , cachePath string ) string {
77+ // not known to be older than the recommended version .
78+ func outdatedVersionWarning (recommendedVersion string , endpointURL * url.URL ) string {
6379 current , err := semver .NewVersion (version .BuildTag )
6480 if err != nil {
6581 return ""
6682 }
67- recommended , err := semver .NewVersion (recommendedVersion ( client , cachePath ) )
83+ recommended , err := semver .NewVersion (recommendedVersion )
6884 if err != nil {
6985 return ""
7086 }
@@ -77,6 +93,25 @@ func outdatedVersionWarning(client api.Client, endpointURL *url.URL, cachePath s
7793 current , recommended , endpointURL )
7894}
7995
96+ // recommendedVersionCachePath returns the file that holds the version
97+ // recommended by the instance at endpointURL.
98+ func recommendedVersionCachePath (endpointURL * url.URL ) (string , error ) {
99+ cacheDir , err := os .UserCacheDir ()
100+ if err != nil {
101+ return "" , err
102+ }
103+ return filepath .Join (cacheDir , "sourcegraph" , "src-cli" , "recommended-version-" + url .QueryEscape (endpointURL .String ())), nil
104+ }
105+
106+ // cacheRecommendedVersion records recommended, and the time it was learned, in
107+ // the file at cachePath.
108+ func cacheRecommendedVersion (cachePath , recommended string ) error {
109+ if err := os .MkdirAll (filepath .Dir (cachePath ), 0o700 ); err != nil {
110+ return err
111+ }
112+ return os .WriteFile (cachePath , []byte (recommended ), 0o600 )
113+ }
114+
80115// recommendedVersion returns the src-cli version recommended by the instance
81116// that client talks to, or "" when it is unknown. The answer is cached in the
82117// file at cachePath for versionCheckInterval, so most commands make no extra
@@ -90,7 +125,7 @@ func recommendedVersion(client api.Client, cachePath string) string {
90125 // Record the attempt before the lookup, so that an instance that does not
91126 // answer delays one command per versionCheckInterval instead of every
92127 // command. Without a writable cache there is no such bound, so do not look.
93- if os . MkdirAll ( filepath . Dir ( cachePath ), 0o700 ) != nil || os . WriteFile ( cachePath , nil , 0o600 ) != nil {
128+ if cacheRecommendedVersion ( cachePath , "" ) != nil {
94129 return ""
95130 }
96131
@@ -100,6 +135,6 @@ func recommendedVersion(client api.Client, cachePath string) string {
100135 if err != nil {
101136 return ""
102137 }
103- _ = os . WriteFile (cachePath , [] byte ( recommended ), 0o600 )
138+ _ = cacheRecommendedVersion (cachePath , recommended )
104139 return recommended
105140}
0 commit comments