Thanks to visit codestin.com
Credit goes to github.com

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

Improve version warning log #206

Merged
merged 3 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coder-sdk/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 12 additions & 9 deletions internal/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"),
)
}
9 changes: 8 additions & 1 deletion internal/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down