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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 9 deletions
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

Lines changed: 8 additions & 1 deletion
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy