Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ All notable changes to `src-cli` are documented in this file.

### Added

- `src` now prints a warning to stderr when it is older than the version recommended by your Sourcegraph instance. The recommended version is looked up at most once every 24 hours per instance. To disable the check, set the `SRC_SKIP_VERSION_CHECK` environment variable to any non-blank string. [#930](https://github.com/sourcegraph/src-cli/issues/930)

### Changed

### Fixed
Expand Down
8 changes: 7 additions & 1 deletion cmd/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,13 @@ func (c *config) apiClient(flags *api.Flags, out io.Writer) api.Client {
}
}

return api.NewClient(opts)
client := api.NewClient(opts)
// Checked here rather than before the command is dispatched, so that the
// lookup uses the command's own flags, such as -insecure-skip-verify.
warnOutdatedVersionOnce.Do(func() {
maybeWarnOutdatedVersion(client, c.endpointURL, flag.CommandLine.Arg(0))
})
return client
}

// readConfig reads the config file from the given path.
Expand Down
18 changes: 11 additions & 7 deletions cmd/src/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"

"github.com/sourcegraph/sourcegraph/lib/errors"
Expand Down Expand Up @@ -42,17 +43,19 @@ var versionCommand = clicompat.Wrap(&cli.Command{
HideVersion: true,
Action: func(ctx context.Context, c *cli.Command) error {
args := VersionArgs{
Client: cfg.apiClient(clicompat.APIFlagsFromCmd(c), os.Stdout),
ClientOnly: c.Bool("client-only"),
Client: cfg.apiClient(clicompat.APIFlagsFromCmd(c), os.Stdout),
EndpointURL: cfg.endpointURL,
ClientOnly: c.Bool("client-only"),
}
return versionHandler(args)
},
})

type VersionArgs struct {
ClientOnly bool
Client api.Client
Output io.Writer
ClientOnly bool
Client api.Client
EndpointURL *url.URL
Output io.Writer
}

func versionHandler(args VersionArgs) error {
Expand All @@ -68,9 +71,10 @@ func versionHandler(args VersionArgs) error {
if recommendedVersion == "" {
fmt.Println("Recommended version: <unknown>")
fmt.Println("This Sourcegraph instance does not support this feature.")
return nil
} else {
fmt.Printf("Recommended version: %s or later\n", recommendedVersion)
}
fmt.Printf("Recommended version: %s or later\n", recommendedVersion)
warnOutdatedVersionFromLookup(recommendedVersion, args.EndpointURL)
return nil
}

Expand Down
140 changes: 140 additions & 0 deletions cmd/src/version_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package main

import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"sync"
"time"

"github.com/Masterminds/semver"

"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/version"
)

const (
// versionCheckTimeout bounds the recommended-version lookup, which runs
// before the command the user asked for.
versionCheckTimeout = 3 * time.Second

// versionCheckInterval is how long the result of a lookup is reused before
// the instance is asked again.
versionCheckInterval = 24 * time.Hour
)

// warnOutdatedVersionOnce limits the check to once per run of src-cli, however
// many API clients the command creates.
var warnOutdatedVersionOnce sync.Once

// maybeWarnOutdatedVersion prints a warning to stderr when src-cli is older
// than the version recommended by the Sourcegraph instance at endpointURL,
// which it asks using client unless a recent answer is cached.
//
// It never fails the command: an unreachable instance, an unparseable version,
// or an unwritable cache all result in no warning.
func maybeWarnOutdatedVersion(client api.Client, endpointURL *url.URL, subcommand string) {
// 'src version' asks the instance itself, and then calls
// warnOutdatedVersionFromLookup with the answer.
if subcommand == "version" || skipVersionCheck() {
return
}
cachePath, err := recommendedVersionCachePath(endpointURL)
if err != nil {
return
}
printOutdatedVersionWarning(recommendedVersion(client, cachePath), endpointURL)
}

// warnOutdatedVersionFromLookup is the check for 'src version', which has just
// been told the recommended version by the instance at endpointURL. The answer
// is cached, so that the commands that follow warn about the same version.
func warnOutdatedVersionFromLookup(recommended string, endpointURL *url.URL) {
if skipVersionCheck() {
return
}
if cachePath, err := recommendedVersionCachePath(endpointURL); err == nil {
_ = cacheRecommendedVersion(cachePath, recommended)
}
printOutdatedVersionWarning(recommended, endpointURL)
}

// skipVersionCheck reports whether the version check is disabled, or has no
// version to compare.
func skipVersionCheck() bool {
return os.Getenv("SRC_SKIP_VERSION_CHECK") != "" || version.BuildTag == version.DefaultBuildTag
}

func printOutdatedVersionWarning(recommended string, endpointURL *url.URL) {
if warning := outdatedVersionWarning(recommended, endpointURL); warning != "" {
fmt.Fprintln(os.Stderr, warning)
}
}

// outdatedVersionWarning returns the warning to print, or "" when src-cli is
// not known to be older than the recommended version.
func outdatedVersionWarning(recommendedVersion string, endpointURL *url.URL) string {
current, err := semver.NewVersion(version.BuildTag)
if err != nil {
return ""
}
recommended, err := semver.NewVersion(recommendedVersion)
if err != nil {
return ""
}
if !current.LessThan(recommended) {
return ""
}
return fmt.Sprintf("⚠️ Warning: src-cli %s is older than %s, the version recommended by %s. "+
"To upgrade, see https://github.com/sourcegraph/src-cli#installation. "+
"To silence this warning, set SRC_SKIP_VERSION_CHECK=1.",
current, recommended, endpointURL)
}

// recommendedVersionCachePath returns the file that holds the version
// recommended by the instance at endpointURL.
func recommendedVersionCachePath(endpointURL *url.URL) (string, error) {
cacheDir, err := os.UserCacheDir()
if err != nil {
return "", err
}
return filepath.Join(cacheDir, "sourcegraph", "src-cli", "recommended-version-"+url.QueryEscape(endpointURL.String())), nil
}

// cacheRecommendedVersion records recommended, and the time it was learned, in
// the file at cachePath.
func cacheRecommendedVersion(cachePath, recommended string) error {
if err := os.MkdirAll(filepath.Dir(cachePath), 0o700); err != nil {
return err
}
return os.WriteFile(cachePath, []byte(recommended), 0o600)
}

// recommendedVersion returns the src-cli version recommended by the instance
// that client talks to, or "" when it is unknown. The answer is cached in the
// file at cachePath for versionCheckInterval, so most commands make no extra
// request.
func recommendedVersion(client api.Client, cachePath string) string {
if info, err := os.Stat(cachePath); err == nil && time.Since(info.ModTime()) < versionCheckInterval {
cached, _ := os.ReadFile(cachePath)
return string(cached)
}

// Record the attempt before the lookup, so that an instance that does not
// answer delays one command per versionCheckInterval instead of every
// command. Without a writable cache there is no such bound, so do not look.
if cacheRecommendedVersion(cachePath, "") != nil {
return ""
}

ctx, cancel := context.WithTimeout(context.Background(), versionCheckTimeout)
defer cancel()
recommended, err := getRecommendedVersion(ctx, client)
if err != nil {
return ""
}
_ = cacheRecommendedVersion(cachePath, recommended)
return recommended
}
Loading
Loading