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
remove race condition
  • Loading branch information
sreya committed Jun 28, 2022
commit 636d5dbc694acb86c888de3d2d02d46cb9d86935
8 changes: 8 additions & 0 deletions buildinfo/buildinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func TestBuildInfo(t *testing.T) {
v2: "v1.2.3",
expectMatch: true,
},
// Our CI instance uses a "-devel" prerelease
// flag. This is not the same as a developer WIP build.
{
name: "DevelPreleaseNotIgnored",
v1: "v1.1.1-devel+123abac",
v2: "v1.2.3",
expectMatch: false,
},
{
name: "MajorMismatch",
v1: "v1.2.3",
Expand Down
12 changes: 8 additions & 4 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -41,12 +42,12 @@ const (
varForceTty = "force-tty"
notLoggedInMessage = "You are not logged in. Try logging in using 'coder login <url>'."

envNoVersionCheck = "CODER_NO_VERSION_WARNING"
noVersionCheckFlag = "no-version-warning"
envNoVersionCheck = "CODER_NO_VERSION_WARNING"
)

var (
errUnauthenticated = xerrors.New(notLoggedInMessage)
varSuppressVersion = false
envSessionToken = "CODER_SESSION_TOKEN"
)

Expand All @@ -60,6 +61,8 @@ func init() {
}

func Root() *cobra.Command {
var varSuppressVersion bool

cmd := &cobra.Command{
Use: "coder",
SilenceErrors: true,
Expand Down Expand Up @@ -127,7 +130,7 @@ func Root() *cobra.Command {
cmd.SetUsageTemplate(usageTemplate())

cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.")
cliflag.BoolVarP(cmd.Flags(), &varSuppressVersion, "no-version-warning", "", envNoVersionCheck, false, "Suppress warning when client and server versions do not match.")
cliflag.BoolVarP(cmd.PersistentFlags(), &varSuppressVersion, noVersionCheckFlag, "", 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 Expand Up @@ -364,7 +367,8 @@ func FormatCobraError(err error, cmd *cobra.Command) string {
}

func checkVersions(cmd *cobra.Command, client *codersdk.Client) error {
if varSuppressVersion {
flag := cmd.Flag("no-version-warning")
if suppress, _ := strconv.ParseBool(flag.Value.String()); suppress {
return nil
}

Expand Down