Skip to content

Commit 4a40d41

Browse files
feat: make 'src version' warn too, and refresh the cached version
'src version' was the one command that never warned. It now prints the warning from the answer it just got from the instance, and caches that answer, so the commands that follow warn about the same version. It still makes a single request, and 'src version -client-only' still makes none: the check that other commands get from config.apiClient keeps skipping 'src version'. Amp-Thread-ID: https://ampcode.com/threads/T-01a1c29c-9f51-7761-a4ba-4d4f46dcd4ef Co-authored-by: Amp <amp@ampcode.com>
1 parent 71363c8 commit 4a40d41

3 files changed

Lines changed: 183 additions & 63 deletions

File tree

‎cmd/src/version.go‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"net/url"
910
"os"
1011

1112
"github.com/sourcegraph/sourcegraph/lib/errors"
@@ -42,17 +43,19 @@ var versionCommand = clicompat.Wrap(&cli.Command{
4243
HideVersion: true,
4344
Action: func(ctx context.Context, c *cli.Command) error {
4445
args := VersionArgs{
45-
Client: cfg.apiClient(clicompat.APIFlagsFromCmd(c), os.Stdout),
46-
ClientOnly: c.Bool("client-only"),
46+
Client: cfg.apiClient(clicompat.APIFlagsFromCmd(c), os.Stdout),
47+
EndpointURL: cfg.endpointURL,
48+
ClientOnly: c.Bool("client-only"),
4749
}
4850
return versionHandler(args)
4951
},
5052
})
5153

5254
type VersionArgs struct {
53-
ClientOnly bool
54-
Client api.Client
55-
Output io.Writer
55+
ClientOnly bool
56+
Client api.Client
57+
EndpointURL *url.URL
58+
Output io.Writer
5659
}
5760

5861
func versionHandler(args VersionArgs) error {
@@ -68,9 +71,10 @@ func versionHandler(args VersionArgs) error {
6871
if recommendedVersion == "" {
6972
fmt.Println("Recommended version: <unknown>")
7073
fmt.Println("This Sourcegraph instance does not support this feature.")
71-
return nil
74+
} else {
75+
fmt.Printf("Recommended version: %s or later\n", recommendedVersion)
7276
}
73-
fmt.Printf("Recommended version: %s or later\n", recommendedVersion)
77+
warnOutdatedVersionFromLookup(recommendedVersion, args.EndpointURL)
7478
return nil
7579
}
7680

‎cmd/src/version_check.go‎

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3838
func 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

Comments
 (0)