Skip to content

Commit 0531bd3

Browse files
committed
fix default value
1 parent 1749686 commit 0531bd3

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

cli/root.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,9 +835,14 @@ func (r *RootCmd) checkVersions(i *clibase.Invocation, client *codersdk.Client,
835835
}
836836

837837
if !buildinfo.VersionsMatch(clientVersion, serverInfo.Version) {
838+
upgradeMessage := defaultUpgradeMessage()
839+
if serverInfo.UpgradeMessage != "" {
840+
upgradeMessage = serverInfo.UpgradeMessage
841+
}
842+
838843
fmtWarningText := "version mismatch: client %s, server %s\n%s"
839844
fmtWarn := pretty.Sprint(cliui.DefaultStyles.Warn, fmtWarningText)
840-
warning := fmt.Sprintf(fmtWarn, clientVersion, strings.TrimPrefix(serverInfo.CanonicalVersion(), "v"), serverInfo.UpgradeMessage)
845+
warning := fmt.Sprintf(fmtWarn, clientVersion, strings.TrimPrefix(serverInfo.CanonicalVersion(), "v"), upgradeMessage)
841846

842847
_, _ = fmt.Fprint(i.Stderr, warning)
843848
_, _ = fmt.Fprintln(i.Stderr)
@@ -1212,3 +1217,13 @@ func SlimUnsupported(w io.Writer, cmd string) {
12121217
//nolint:revive
12131218
os.Exit(1)
12141219
}
1220+
1221+
func defaultUpgradeMessage() string {
1222+
// Our installation script doesn't work on Windows, so instead we direct the user
1223+
// to the GitHub release page to download the latest installer.
1224+
version := buildinfo.Version()
1225+
if runtime.GOOS == "windows" {
1226+
return fmt.Sprintf("download the server version from: https://github.com/coder/coder/releases/v%s", version)
1227+
}
1228+
return fmt.Sprintf("download the server version with: 'curl -L https://coder.com/install.sh | sh -s -- --version %s'", version)
1229+
}

cli/root_internal_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestMain(m *testing.M) {
100100
func Test_checkVersions(t *testing.T) {
101101
t.Parallel()
102102

103-
t.Run("CustomInstallMessage", func(t *testing.T) {
103+
t.Run("CustomUpgradeMessage", func(t *testing.T) {
104104
t.Parallel()
105105

106106
var (
@@ -141,4 +141,43 @@ func Test_checkVersions(t *testing.T) {
141141
expectedOutput := fmt.Sprintln(pretty.Sprint(cliui.DefaultStyles.Warn, fmtOutput))
142142
require.Equal(t, expectedOutput, buf.String())
143143
})
144+
145+
t.Run("DefaultUpgradeMessage", func(t *testing.T) {
146+
t.Parallel()
147+
148+
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
149+
httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.BuildInfoResponse{
150+
ExternalURL: buildinfo.ExternalURL(),
151+
// Provide a version that will not match
152+
Version: "v1.0.0",
153+
AgentAPIVersion: coderd.AgentAPIVersionREST,
154+
// does not matter what the url is
155+
DashboardURL: "https://example.com",
156+
WorkspaceProxy: false,
157+
UpgradeMessage: "",
158+
})
159+
}))
160+
defer srv.Close()
161+
surl, err := url.Parse(srv.URL)
162+
require.NoError(t, err)
163+
164+
client := codersdk.New(surl)
165+
166+
r := &RootCmd{}
167+
168+
cmd, err := r.Command(nil)
169+
require.NoError(t, err)
170+
171+
var buf bytes.Buffer
172+
inv := cmd.Invoke()
173+
inv.Stderr = &buf
174+
175+
err = r.checkVersions(inv, client, "v2.0.0")
176+
require.NoError(t, err)
177+
178+
fmtOutput := fmt.Sprintf("version mismatch: client v2.0.0, server 1.0.0\n%s", defaultUpgradeMessage())
179+
expectedOutput := fmt.Sprintln(pretty.Sprint(cliui.DefaultStyles.Warn, fmtOutput))
180+
require.Equal(t, expectedOutput, buf.String())
181+
})
182+
144183
}

codersdk/deployment.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"flag"
7-
"fmt"
87
"net/http"
98
"os"
109
"path/filepath"
11-
"runtime"
1210
"strconv"
1311
"strings"
1412
"time"
@@ -1791,7 +1789,6 @@ when required by your organization's security policy.`,
17911789
YAML: "cliUpgradeMessage",
17921790
Group: &deploymentGroupClient,
17931791
Value: &c.CLIUpgradeMessage,
1794-
Default: defaultUpgradeMessage(),
17951792
Hidden: false,
17961793
},
17971794
{
@@ -2305,13 +2302,3 @@ func (c *Client) SSHConfiguration(ctx context.Context) (SSHConfigResponse, error
23052302
var sshConfig SSHConfigResponse
23062303
return sshConfig, json.NewDecoder(res.Body).Decode(&sshConfig)
23072304
}
2308-
2309-
func defaultUpgradeMessage() string {
2310-
// Our installation script doesn't work on Windows, so instead we direct the user
2311-
// to the GitHub release page to download the latest installer.
2312-
version := buildinfo.Version()
2313-
if runtime.GOOS == "windows" {
2314-
return fmt.Sprintf("download the server version from: https://github.com/coder/coder/releases/v%s", version)
2315-
}
2316-
return fmt.Sprintf("download the server version with: 'curl -L https://coder.com/install.sh | sh -s -- --version %s'", version)
2317-
}

0 commit comments

Comments
 (0)