diff --git a/coder-sdk/version.go b/coder-sdk/version.go index 518c2318..5407fec0 100644 --- a/coder-sdk/version.go +++ b/coder-sdk/version.go @@ -8,7 +8,7 @@ import ( // APIVersion parses the coder-version http header from an authenticated request. func (c Client) APIVersion(ctx context.Context) (string, error) { const coderVersionHeaderKey = "coder-version" - resp, err := c.request(ctx, http.MethodGet, "/api/private/users/"+Me, nil) + resp, err := c.request(ctx, http.MethodGet, "/api", nil) if err != nil { return "", err } diff --git a/internal/cmd/auth.go b/internal/cmd/auth.go index aa204d1e..0322f4ab 100644 --- a/internal/cmd/auth.go +++ b/internal/cmd/auth.go @@ -53,6 +53,9 @@ func newClient(ctx context.Context) (*coder.Client, error) { } apiVersion, err := c.APIVersion(ctx) + if apiVersion != "" && !version.VersionsMatch(apiVersion) { + logVersionMismatchError(apiVersion) + } if err != nil { var he *coder.HTTPError if xerrors.As(err, &he) { @@ -63,14 +66,14 @@ func newClient(ctx context.Context) (*coder.Client, error) { return nil, err } - if !version.VersionsMatch(apiVersion) { - clog.LogWarn( - "version mismatch detected", - fmt.Sprintf("coder-cli version: %s", version.Version), - fmt.Sprintf("Coder API version: %s", apiVersion), clog.BlankLine, - clog.Tipf("download the appropriate version here: https://github.com/cdr/coder-cli/releases"), - ) - } - return c, nil } + +func logVersionMismatchError(apiVersion string) { + clog.LogWarn( + "version mismatch detected", + fmt.Sprintf("Coder CLI version: %s", version.Version), + fmt.Sprintf("Coder API version: %s", apiVersion), clog.BlankLine, + clog.Tipf("download the appropriate version here: https://github.com/cdr/coder-cli/releases"), + ) +} diff --git a/internal/cmd/login.go b/internal/cmd/login.go index 38a34982..3b2c8493 100644 --- a/internal/cmd/login.go +++ b/internal/cmd/login.go @@ -11,6 +11,7 @@ import ( "cdr.dev/coder-cli/coder-sdk" "cdr.dev/coder-cli/internal/config" "cdr.dev/coder-cli/internal/loginsrv" + "cdr.dev/coder-cli/internal/version" "cdr.dev/coder-cli/pkg/clog" "github.com/pkg/browser" "github.com/spf13/cobra" @@ -64,7 +65,13 @@ func newLocalListener() (net.Listener, error) { // Not using the SDK as we want to verify the url/token pair before storing the config files. func pingAPI(ctx context.Context, envURL *url.URL, token string) error { client := &coder.Client{BaseURL: envURL, Token: token} - if _, err := client.Me(ctx); err != nil { + if apiVersion, err := client.APIVersion(ctx); err == nil { + if apiVersion != "" && !version.VersionsMatch(apiVersion) { + logVersionMismatchError(apiVersion) + } + } + _, err := client.Me(ctx) + if err != nil { return xerrors.Errorf("call api: %w", err) } return nil