From 91973e1e88402819d00aa9492c631d56ccb0b720 Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Fri, 2 Dec 2022 15:40:23 -0600 Subject: [PATCH] cli: remove redundant client creation requests (#5264) --- cli/list.go | 4 +++ cli/root.go | 78 +++++++++++++++++++++-------------------------------- 2 files changed, 35 insertions(+), 47 deletions(-) diff --git a/cli/list.go b/cli/list.go index ed75fe6e85..a198dd25b8 100644 --- a/cli/list.go +++ b/cli/list.go @@ -77,6 +77,7 @@ func list() *cobra.Command { if err != nil { return err } + filter := codersdk.WorkspaceFilter{ FilterQuery: searchQuery, } @@ -91,6 +92,7 @@ func list() *cobra.Command { } filter.Owner = myUser.Username } + res, err := client.Workspaces(cmd.Context(), filter) if err != nil { return err @@ -102,10 +104,12 @@ func list() *cobra.Command { _, _ = fmt.Fprintln(cmd.ErrOrStderr()) return nil } + userRes, err := client.Users(cmd.Context(), codersdk.UsersRequest{}) if err != nil { return err } + usersByID := map[uuid.UUID]codersdk.User{} for _, user := range userRes.Users { usersByID[user.ID] = user diff --git a/cli/root.go b/cli/root.go index c68adfff99..654009e8ce 100644 --- a/cli/root.go +++ b/cli/root.go @@ -136,53 +136,6 @@ func Root(subcommands []*cobra.Command) *cobra.Command { } return cmd.Help() }, - PersistentPreRun: func(cmd *cobra.Command, args []string) { - if cliflag.IsSetBool(cmd, varNoVersionCheck) && - cliflag.IsSetBool(cmd, varNoFeatureWarning) { - return - } - - // login handles checking the versions itself since it has a handle - // to an unauthenticated client. - // - // server is skipped for obvious reasons. - // - // agent is skipped because these checks use the global coder config - // and not the agent URL and token from the environment. - // - // gitssh is skipped because it's usually not called by users - // directly. - if cmd.Name() == "login" || cmd.Name() == "server" || cmd.Name() == "agent" || cmd.Name() == "gitssh" { - return - } - if isGitAskpass { - return - } - - client, err := CreateClient(cmd) - // If we are unable to create a client, presumably the subcommand will fail as well - // so we can bail out here. - if err != nil { - return - } - - err = checkVersions(cmd, client) - if err != nil { - // Just log the error here. We never want to fail a command - // due to a pre-run. - _, _ = fmt.Fprintf(cmd.ErrOrStderr(), - cliui.Styles.Warn.Render("check versions error: %s"), err) - _, _ = fmt.Fprintln(cmd.ErrOrStderr()) - } - - err = checkWarnings(cmd, client) - if err != nil { - // Same as above - _, _ = fmt.Fprintf(cmd.ErrOrStderr(), - cliui.Styles.Warn.Render("check entitlement warnings error: %s"), err) - _, _ = fmt.Fprintln(cmd.ErrOrStderr()) - } - }, Example: formatExamples( example{ Description: "Start a Coder server", @@ -307,6 +260,37 @@ func CreateClient(cmd *cobra.Command) (*codersdk.Client, error) { return nil, err } client.SetSessionToken(token) + + // We send these requests in parallel to minimize latency. + var ( + versionErr = make(chan error) + warningErr = make(chan error) + ) + go func() { + versionErr <- checkVersions(cmd, client) + close(versionErr) + }() + + go func() { + warningErr <- checkWarnings(cmd, client) + close(warningErr) + }() + + if err = <-versionErr; err != nil { + // Just log the error here. We never want to fail a command + // due to a pre-run. + _, _ = fmt.Fprintf(cmd.ErrOrStderr(), + cliui.Styles.Warn.Render("check versions error: %s"), err) + _, _ = fmt.Fprintln(cmd.ErrOrStderr()) + } + + if err = <-warningErr; err != nil { + // Same as above + _, _ = fmt.Fprintf(cmd.ErrOrStderr(), + cliui.Styles.Warn.Render("check entitlement warnings error: %s"), err) + _, _ = fmt.Fprintln(cmd.ErrOrStderr()) + } + return client, nil }