Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 7fcfe98

Browse files
authored
fix: improve API version mismatch error and show after login (#206)
1 parent f492f2d commit 7fcfe98

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

coder-sdk/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// APIVersion parses the coder-version http header from an authenticated request.
99
func (c Client) APIVersion(ctx context.Context) (string, error) {
1010
const coderVersionHeaderKey = "coder-version"
11-
resp, err := c.request(ctx, http.MethodGet, "/api/private/users/"+Me, nil)
11+
resp, err := c.request(ctx, http.MethodGet, "/api", nil)
1212
if err != nil {
1313
return "", err
1414
}

internal/cmd/auth.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func newClient(ctx context.Context) (*coder.Client, error) {
5353
}
5454

5555
apiVersion, err := c.APIVersion(ctx)
56+
if apiVersion != "" && !version.VersionsMatch(apiVersion) {
57+
logVersionMismatchError(apiVersion)
58+
}
5659
if err != nil {
5760
var he *coder.HTTPError
5861
if xerrors.As(err, &he) {
@@ -63,14 +66,14 @@ func newClient(ctx context.Context) (*coder.Client, error) {
6366
return nil, err
6467
}
6568

66-
if !version.VersionsMatch(apiVersion) {
67-
clog.LogWarn(
68-
"version mismatch detected",
69-
fmt.Sprintf("coder-cli version: %s", version.Version),
70-
fmt.Sprintf("Coder API version: %s", apiVersion), clog.BlankLine,
71-
clog.Tipf("download the appropriate version here: https://github.com/cdr/coder-cli/releases"),
72-
)
73-
}
74-
7569
return c, nil
7670
}
71+
72+
func logVersionMismatchError(apiVersion string) {
73+
clog.LogWarn(
74+
"version mismatch detected",
75+
fmt.Sprintf("Coder CLI version: %s", version.Version),
76+
fmt.Sprintf("Coder API version: %s", apiVersion), clog.BlankLine,
77+
clog.Tipf("download the appropriate version here: https://github.com/cdr/coder-cli/releases"),
78+
)
79+
}

internal/cmd/login.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"cdr.dev/coder-cli/coder-sdk"
1212
"cdr.dev/coder-cli/internal/config"
1313
"cdr.dev/coder-cli/internal/loginsrv"
14+
"cdr.dev/coder-cli/internal/version"
1415
"cdr.dev/coder-cli/pkg/clog"
1516
"github.com/pkg/browser"
1617
"github.com/spf13/cobra"
@@ -64,7 +65,13 @@ func newLocalListener() (net.Listener, error) {
6465
// Not using the SDK as we want to verify the url/token pair before storing the config files.
6566
func pingAPI(ctx context.Context, envURL *url.URL, token string) error {
6667
client := &coder.Client{BaseURL: envURL, Token: token}
67-
if _, err := client.Me(ctx); err != nil {
68+
if apiVersion, err := client.APIVersion(ctx); err == nil {
69+
if apiVersion != "" && !version.VersionsMatch(apiVersion) {
70+
logVersionMismatchError(apiVersion)
71+
}
72+
}
73+
_, err := client.Me(ctx)
74+
if err != nil {
6875
return xerrors.Errorf("call api: %w", err)
6976
}
7077
return nil

0 commit comments

Comments
 (0)