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
more stuff
  • Loading branch information
sreya committed Jun 28, 2022
commit 54cb6646b0a99be26d18c99e3ae94ea1ae559e63
21 changes: 2 additions & 19 deletions buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package buildinfo
import (
"fmt"
"runtime/debug"
"strings"
"sync"
"time"

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

v1Toks := strings.Split(v1, ".")
v2Toks := strings.Split(v2, ".")

// Versions should be formatted as "<major>.<minor>.<patch>".
// We assume malformed versions are evidence of a bug and return false.
if len(v1Toks) < 3 || len(v2Toks) < 3 {
return false
}

// Slice off the patch suffix. Patch versions should be non-breaking
// changes.
v1MajorMinor := strings.Join(v1Toks[:2], ".")
v2MajorMinor := strings.Join(v2Toks[:2], ".")

return v1MajorMinor == v2MajorMinor
return semver.MajorMinor(v1) == semver.MajorMinor(v2)
}

// ExternalURL returns a URL referencing the current Coder version.
Expand Down
11 changes: 0 additions & 11 deletions buildinfo/buildinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ func TestBuildInfo(t *testing.T) {
v2: "v1.2.3",
expectMatch: true,
},
// Test that we return false if a version is malformed.
{
name: "MalformedIgnored",
v1: "v1.2.3",
v2: "v1.2",
expectMatch: false,
},
// Test that we return true if a developer version is detected.
// Developers do not need to be warned of mismatched versions.
{
Expand Down Expand Up @@ -86,10 +79,6 @@ func TestBuildInfo(t *testing.T) {
}

for _, c := range cases {
// It's very important to do this since we're running the tests
// in parallel. Otherwise you will likely get the last element
// in the list since the goroutines will likely start executing
// after the for loop has completed.
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
Expand Down
2 changes: 1 addition & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func Root() *cobra.Command {
cmd.SetUsageTemplate(usageTemplate())

cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.")
cliflag.BoolVarP(cmd.PersistentFlags(), &varSuppressVersion, "no-version-warning", "", envNoVersionCheck, false, "Suppress warning when client and server versions do not match.")
cliflag.BoolVarP(cmd.Flags(), &varSuppressVersion, "no-version-warning", "", envNoVersionCheck, false, "Suppress warning when client and server versions do not match.")
cliflag.String(cmd.PersistentFlags(), varToken, "", envSessionToken, "", fmt.Sprintf("Specify an authentication token. For security reasons setting %s is preferred.", envSessionToken))
cliflag.String(cmd.PersistentFlags(), varAgentToken, "", "CODER_AGENT_TOKEN", "", "Specify an agent authentication token.")
_ = cmd.PersistentFlags().MarkHidden(varAgentToken)
Expand Down
13 changes: 5 additions & 8 deletions codersdk/buildinfo.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package codersdk

import (
"bytes"
"context"
"encoding/json"
"net/http"
"strings"

"golang.org/x/mod/semver"
)

// BuildInfoResponse contains build information for this instance of Coder.
Expand All @@ -20,13 +22,8 @@ type BuildInfoResponse struct {
// TrimmedVersion trims build information from the version.
// E.g. 'v0.7.4-devel+11573034' -> 'v0.7.4'.
func (b BuildInfoResponse) TrimmedVersion() string {
// Linter doesn't like strings.Index...
idx := bytes.Index([]byte(b.Version), []byte("-devel"))
if idx < 0 {
return b.Version
}

return b.Version[:idx]
trimmed := strings.ReplaceAll(b.Version, "-devel", "+devel")
return semver.Canonical(trimmed)
}

// BuildInfo returns build information for this instance of Coder.
Expand Down