Skip to content

feat: add customizable upgrade message on client/server version mismatch #11587

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 19 commits into from
Jan 30, 2024
Prev Previous commit
Next Next commit
fix version formatting
  • Loading branch information
sreya committed Jan 30, 2024
commit 7b1be4366b47ca26b1030751566204068b4a888e
11 changes: 5 additions & 6 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,14 +835,14 @@ func (r *RootCmd) checkVersions(i *clibase.Invocation, client *codersdk.Client,
}

if !buildinfo.VersionsMatch(clientVersion, serverInfo.Version) {
upgradeMessage := defaultUpgradeMessage()
upgradeMessage := defaultUpgradeMessage(strings.TrimPrefix(serverInfo.CanonicalVersion(), "v"))
if serverInfo.UpgradeMessage != "" {
upgradeMessage = serverInfo.UpgradeMessage
}

fmtWarningText := "version mismatch: client %s, server %s\n%s"
fmtWarn := pretty.Sprint(cliui.DefaultStyles.Warn, fmtWarningText)
warning := fmt.Sprintf(fmtWarn, clientVersion, strings.TrimPrefix(serverInfo.CanonicalVersion(), "v"), upgradeMessage)
warning := fmt.Sprintf(fmtWarn, clientVersion, serverInfo.Version, upgradeMessage)

_, _ = fmt.Fprint(i.Stderr, warning)
_, _ = fmt.Fprintln(i.Stderr)
Expand Down Expand Up @@ -1218,12 +1218,11 @@ func SlimUnsupported(w io.Writer, cmd string) {
os.Exit(1)
}

func defaultUpgradeMessage() string {
func defaultUpgradeMessage(serverVersion string) string {
// Our installation script doesn't work on Windows, so instead we direct the user
// to the GitHub release page to download the latest installer.
version := buildinfo.Version()
if runtime.GOOS == "windows" {
return fmt.Sprintf("download the server version from: https://github.com/coder/coder/releases/v%s", version)
return fmt.Sprintf("download the server version from: https://github.com/coder/coder/releases/v%s", serverVersion)
}
return fmt.Sprintf("download the server version with: 'curl -L https://coder.com/install.sh | sh -s -- --version %s'", version)
return fmt.Sprintf("download the server version with: 'curl -L https://coder.com/install.sh | sh -s -- --version %s'", serverVersion)
}
4 changes: 2 additions & 2 deletions cli/root_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func Test_checkVersions(t *testing.T) {
err = r.checkVersions(inv, client, "v2.0.0")
require.NoError(t, err)

fmtOutput := fmt.Sprintf("version mismatch: client v2.0.0, server 1.0.0\n%s", expectedUpgradeMessage)
fmtOutput := fmt.Sprintf("version mismatch: client v2.0.0, server v1.0.0\n%s", expectedUpgradeMessage)
expectedOutput := fmt.Sprintln(pretty.Sprint(cliui.DefaultStyles.Warn, fmtOutput))
require.Equal(t, expectedOutput, buf.String())
})
Expand Down Expand Up @@ -173,7 +173,7 @@ func Test_checkVersions(t *testing.T) {
err = r.checkVersions(inv, client, "v2.0.0")
require.NoError(t, err)

fmtOutput := fmt.Sprintf("version mismatch: client v2.0.0, server 1.0.0\n%s", defaultUpgradeMessage())
fmtOutput := fmt.Sprintf("version mismatch: client v2.0.0, server v1.0.0\n%s", defaultUpgradeMessage("1.0.0"))
expectedOutput := fmt.Sprintln(pretty.Sprint(cliui.DefaultStyles.Warn, fmtOutput))
require.Equal(t, expectedOutput, buf.String())
})
Expand Down