-
Notifications
You must be signed in to change notification settings - Fork 894
feat: add version checking to CLI #2643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fe44291
feat: add version checking to CLI
sreya a9b4796
stuff
sreya 19224b0
add more plumbing
sreya ff7d39e
remove some test code
sreya aa1ee2f
remove unnecessary type cast
sreya 2b52169
prevent duplicates
sreya 5435418
make gen
sreya 9a9bae1
use semver
sreya cc6baed
Revert "use semver"
sreya 6e8c3c0
use install script link
sreya fc3fbb5
Merge branch 'main' into jon/version
sreya 67966e7
some merge woes
sreya d855172
some stuff
sreya 54cb664
more stuff
sreya 51301d9
fix versions
sreya 636d5db
remove race condition
sreya d36e134
make gen
sreya b07de24
pr comments
sreya 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
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 |
---|---|---|
|
@@ -4,11 +4,13 @@ import ( | |
"fmt" | ||
"net/url" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"github.com/charmbracelet/lipgloss" | ||
"github.com/kirsle/configdir" | ||
"github.com/mattn/go-isatty" | ||
"github.com/spf13/cobra" | ||
|
@@ -40,7 +42,13 @@ const ( | |
varForceTty = "force-tty" | ||
notLoggedInMessage = "You are not logged in. Try logging in using 'coder login <url>'." | ||
|
||
envSessionToken = "CODER_SESSION_TOKEN" | ||
noVersionCheckFlag = "no-version-warning" | ||
envNoVersionCheck = "CODER_NO_VERSION_WARNING" | ||
) | ||
|
||
var ( | ||
errUnauthenticated = xerrors.New(notLoggedInMessage) | ||
envSessionToken = "CODER_SESSION_TOKEN" | ||
) | ||
|
||
func init() { | ||
|
@@ -53,12 +61,37 @@ func init() { | |
} | ||
|
||
func Root() *cobra.Command { | ||
var varSuppressVersion bool | ||
|
||
cmd := &cobra.Command{ | ||
Use: "coder", | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
Long: `Coder — A tool for provisioning self-hosted development environments. | ||
`, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
if varSuppressVersion { | ||
return nil | ||
} | ||
|
||
// Login handles checking the versions itself since it | ||
// has a handle to an unauthenticated client. | ||
if cmd.Name() == "login" { | ||
return nil | ||
} | ||
|
||
client, err := createClient(cmd) | ||
// If the client is unauthenticated we can ignore the check. | ||
// The child commands should handle an unauthenticated client. | ||
if xerrors.Is(err, errUnauthenticated) { | ||
return nil | ||
} | ||
if err != nil { | ||
return xerrors.Errorf("create client: %w", err) | ||
} | ||
return checkVersions(cmd, client) | ||
}, | ||
|
||
Example: ` Start a Coder server. | ||
` + cliui.Styles.Code.Render("$ coder server") + ` | ||
|
||
|
@@ -97,6 +130,7 @@ func Root() *cobra.Command { | |
cmd.SetUsageTemplate(usageTemplate()) | ||
|
||
cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.") | ||
cliflag.BoolVarP(cmd.PersistentFlags(), &varSuppressVersion, noVersionCheckFlag, "", envNoVersionCheck, false, "Suppress warning when client and server versions do not match.") | ||
cliflag.String(cmd.PersistentFlags(), varToken, "", envSessionToken, "", fmt.Sprintf("Specify an authentication token. For security reasons setting %s is preferred.", envSessionToken)) | ||
cliflag.String(cmd.PersistentFlags(), varAgentToken, "", "CODER_AGENT_TOKEN", "", "Specify an agent authentication token.") | ||
_ = cmd.PersistentFlags().MarkHidden(varAgentToken) | ||
|
@@ -142,7 +176,7 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) { | |
if err != nil { | ||
// If the configuration files are absent, the user is logged out | ||
if os.IsNotExist(err) { | ||
return nil, xerrors.New(notLoggedInMessage) | ||
return nil, errUnauthenticated | ||
} | ||
return nil, err | ||
} | ||
|
@@ -157,7 +191,7 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) { | |
if err != nil { | ||
// If the configuration files are absent, the user is logged out | ||
if os.IsNotExist(err) { | ||
return nil, xerrors.New(notLoggedInMessage) | ||
return nil, errUnauthenticated | ||
} | ||
return nil, err | ||
} | ||
|
@@ -331,3 +365,32 @@ func FormatCobraError(err error, cmd *cobra.Command) string { | |
helpErrMsg := fmt.Sprintf("Run '%s --help' for usage.", cmd.CommandPath()) | ||
return cliui.Styles.Error.Render(err.Error() + "\n" + helpErrMsg) | ||
} | ||
|
||
func checkVersions(cmd *cobra.Command, client *codersdk.Client) error { | ||
flag := cmd.Flag("no-version-warning") | ||
if suppress, _ := strconv.ParseBool(flag.Value.String()); suppress { | ||
return nil | ||
} | ||
|
||
clientVersion := buildinfo.Version() | ||
|
||
info, err := client.BuildInfo(cmd.Context()) | ||
if err != nil { | ||
return xerrors.Errorf("build info: %w", err) | ||
} | ||
|
||
fmtWarningText := `client/server versions do not match | ||
client version: %s | ||
server version: %s | ||
to download the appropriate version run 'curl -L https://coder.com/install.sh | sh -s -- --version %s' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like we could shorten this up a bit. What do you think about:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
` | ||
|
||
if !buildinfo.VersionsMatch(clientVersion, info.Version) { | ||
warn := cliui.Styles.Warn.Copy().Align(lipgloss.Left) | ||
// Trim the leading 'v', our install.sh script does not handle this case well. | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), warn.Render(fmtWarningText), clientVersion, info.Version, strings.TrimPrefix(info.CanonicalVersion(), "v")) | ||
_, _ = fmt.Fprintln(cmd.OutOrStdout()) | ||
} | ||
|
||
return 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
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.
Uh oh!
There was an error while loading. Please reload this page.