Skip to content

feat: add version checking to CLI #2643

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 18 commits into from
Jun 29, 2022
Prev Previous commit
Next Next commit
fix versions
  • Loading branch information
sreya committed Jun 28, 2022
commit 51301d92c29cf2be3d3a93da6dc8c1e5cc17f7c2
5 changes: 4 additions & 1 deletion buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package buildinfo
import (
"fmt"
"runtime/debug"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -58,7 +59,9 @@ func Version() string {
// disregarded. If it detects that either version is a developer build it
// returns true.
func VersionsMatch(v1, v2 string) bool {
if semver.Prerelease(v1) == "-devel" || semver.Prerelease(v2) == "-devel" {
// Developer versions are disregarded...hopefully they know what they are
// doing.
if strings.HasPrefix(v1, develPrefix) || strings.HasPrefix(v2, develPrefix) {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ to download the appropriate version run 'curl -L https://coder.com/install.sh |
if !buildinfo.VersionsMatch(clientVersion, info.Version) {
warn := cliui.Styles.Warn.Copy().Align(lipgloss.Left)
// Trim the leading 'v', our install.sh script does not handle this case well.
_, _ = fmt.Fprintf(cmd.OutOrStdout(), warn.Render(fmtWarningText), clientVersion, info.Version, info.TrimmedVersion()[1:])
_, _ = fmt.Fprintf(cmd.OutOrStdout(), warn.Render(fmtWarningText), clientVersion, info.Version, strings.TrimPrefix(info.CanonicalVersion(), "v"))
_, _ = fmt.Fprintln(cmd.OutOrStdout())
}

Expand Down
8 changes: 5 additions & 3 deletions codersdk/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ type BuildInfoResponse struct {
Version string `json:"version"`
}

// TrimmedVersion trims build information from the version.
// CanonicalVersion trims build information from the version.
// E.g. 'v0.7.4-devel+11573034' -> 'v0.7.4'.
func (b BuildInfoResponse) TrimmedVersion() string {
trimmed := strings.ReplaceAll(b.Version, "-devel", "+devel")
func (b BuildInfoResponse) CanonicalVersion() string {
// We do a little hack here to massage the string into a form
// that works well with semver.
trimmed := strings.ReplaceAll(b.Version, "-devel+", "+devel-")
return semver.Canonical(trimmed)
}

Expand Down