This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Show warning on coder-cli / Coder API version mismatch #158
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package coder | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
// 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/users/"+Me, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
version := resp.Header.Get(coderVersionHeaderKey) | ||
if version == "" { | ||
version = "unknown" | ||
} | ||
|
||
return version, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package version | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// Version is populated at compile-time with the current coder-cli version. | ||
var Version string = "unknown" | ||
|
||
// VersionMatch compares the given APIVersion to the compile-time injected coder-cli version. | ||
func VersionsMatch(apiVersion string) bool { | ||
withoutPatchRelease := strings.Split(Version, ".") | ||
if len(withoutPatchRelease) < 3 { | ||
return false | ||
} | ||
majorMinor := strings.Join(withoutPatchRelease[:len(withoutPatchRelease)-1], ".") | ||
return strings.HasPrefix(strings.TrimPrefix(apiVersion, "v"), strings.TrimPrefix(majorMinor, "v")) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package version | ||
|
||
import ( | ||
"testing" | ||
|
||
"cdr.dev/slog/sloggers/slogtest/assert" | ||
) | ||
|
||
func TestVersion(t *testing.T) { | ||
Version = "1.12.1" | ||
match := VersionsMatch("1.12.2") | ||
assert.True(t, "versions match", match) | ||
|
||
Version = "v1.14.1" | ||
match = VersionsMatch("1.15.2") | ||
assert.True(t, "versions do not match", !match) | ||
|
||
Version = "v1.15.4" | ||
match = VersionsMatch("1.15.2") | ||
assert.True(t, "versions do match", match) | ||
|
||
Version = "1.15.4" | ||
match = VersionsMatch("v1.15.2") | ||
assert.True(t, "versions do match", match) | ||
|
||
Version = "1.15.4" | ||
match = VersionsMatch("v2.15.2") | ||
assert.True(t, "versions do not match", !match) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure, this is thrown to stderr correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, all log levels are thrown in stderr.