mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore(cli): replace lipgloss with coder/pretty (#9564)
This change will improve over CLI performance and "snappiness" as well as substantially reduce our test times. Preliminary benchmarks show `coder server --help` times cut from 300ms to 120ms on my dogfood instance. The inefficiency of lipgloss disproportionately impacts our system, as all help text for every command is generated whenever any command is invoked. The `pretty` API could clean up a lot of the code (e.g., by replacing complex string concatenations with Printf), but this commit is too expansive as is so that work will be done in a follow up.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/coder/coder/v2/cli/clibase"
|
||||
"github.com/coder/coder/v2/cli/cliui"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
"github.com/coder/pretty"
|
||||
)
|
||||
|
||||
func (r *RootCmd) groupCreate() *clibase.Cmd {
|
||||
@@ -42,7 +43,7 @@ func (r *RootCmd) groupCreate() *clibase.Cmd {
|
||||
return xerrors.Errorf("create group: %w", err)
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintf(inv.Stdout, "Successfully created group %s!\n", cliui.DefaultStyles.Keyword.Render(group.Name))
|
||||
_, _ = fmt.Fprintf(inv.Stdout, "Successfully created group %s!\n", pretty.Sprint(cliui.DefaultStyles.Keyword, group.Name))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/pretty"
|
||||
|
||||
"github.com/coder/coder/v2/cli/clitest"
|
||||
"github.com/coder/coder/v2/cli/cliui"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
@@ -43,6 +45,6 @@ func TestCreateGroup(t *testing.T) {
|
||||
err := inv.Run()
|
||||
require.NoError(t, err)
|
||||
|
||||
pty.ExpectMatch(fmt.Sprintf("Successfully created group %s!", cliui.DefaultStyles.Keyword.Render(groupName)))
|
||||
pty.ExpectMatch(fmt.Sprintf("Successfully created group %s!", pretty.Sprint(cliui.DefaultStyles.Keyword, groupName)))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/coder/coder/v2/cli/clibase"
|
||||
"github.com/coder/coder/v2/cli/cliui"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
"github.com/coder/pretty"
|
||||
)
|
||||
|
||||
func (r *RootCmd) groupDelete() *clibase.Cmd {
|
||||
@@ -41,7 +42,7 @@ func (r *RootCmd) groupDelete() *clibase.Cmd {
|
||||
return xerrors.Errorf("delete group: %w", err)
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintf(inv.Stdout, "Successfully deleted group %s!\n", cliui.DefaultStyles.Keyword.Render(group.Name))
|
||||
_, _ = fmt.Fprintf(inv.Stdout, "Successfully deleted group %s!\n", pretty.Sprint(cliui.DefaultStyles.Keyword, group.Name))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/pretty"
|
||||
|
||||
"github.com/coder/coder/v2/cli/clitest"
|
||||
"github.com/coder/coder/v2/cli/cliui"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
@@ -45,7 +47,7 @@ func TestGroupDelete(t *testing.T) {
|
||||
err = inv.Run()
|
||||
require.NoError(t, err)
|
||||
|
||||
pty.ExpectMatch(fmt.Sprintf("Successfully deleted group %s", cliui.DefaultStyles.Keyword.Render(group.Name)))
|
||||
pty.ExpectMatch(fmt.Sprintf("Successfully deleted group %s", pretty.Sprint(cliui.DefaultStyles.Keyword, group.Name)))
|
||||
})
|
||||
|
||||
t.Run("NoArg", func(t *testing.T) {
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/pretty"
|
||||
|
||||
agpl "github.com/coder/coder/v2/cli"
|
||||
"github.com/coder/coder/v2/cli/clibase"
|
||||
"github.com/coder/coder/v2/cli/cliui"
|
||||
@@ -77,7 +79,7 @@ func (r *RootCmd) groupEdit() *clibase.Cmd {
|
||||
return xerrors.Errorf("patch group: %w", err)
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintf(inv.Stdout, "Successfully patched group %s!\n", cliui.DefaultStyles.Keyword.Render(group.Name))
|
||||
_, _ = fmt.Fprintf(inv.Stdout, "Successfully patched group %s!\n", pretty.Sprint(cliui.DefaultStyles.Keyword, group.Name))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/pretty"
|
||||
|
||||
"github.com/coder/coder/v2/cli/clitest"
|
||||
"github.com/coder/coder/v2/cli/cliui"
|
||||
"github.com/coder/coder/v2/coderd/coderdtest"
|
||||
@@ -65,7 +67,7 @@ func TestGroupEdit(t *testing.T) {
|
||||
err = inv.Run()
|
||||
require.NoError(t, err)
|
||||
|
||||
pty.ExpectMatch(fmt.Sprintf("Successfully patched group %s", cliui.DefaultStyles.Keyword.Render(expectedName)))
|
||||
pty.ExpectMatch(fmt.Sprintf("Successfully patched group %s", pretty.Sprint(cliui.DefaultStyles.Keyword, expectedName)))
|
||||
})
|
||||
|
||||
t.Run("InvalidUserInput", func(t *testing.T) {
|
||||
|
||||
@@ -147,7 +147,7 @@ func (r *RootCmd) provisionerDaemonStart() *clibase.Cmd {
|
||||
select {
|
||||
case <-notifyCtx.Done():
|
||||
exitErr = notifyCtx.Err()
|
||||
_, _ = fmt.Fprintln(inv.Stdout, cliui.DefaultStyles.Bold.Render(
|
||||
_, _ = fmt.Fprintln(inv.Stdout, cliui.Bold(
|
||||
"Interrupt caught, gracefully exiting. Use ctrl+\\ to force quit",
|
||||
))
|
||||
case exitErr = <-errCh:
|
||||
|
||||
@@ -305,7 +305,7 @@ func (*RootCmd) proxyServer() *clibase.Cmd {
|
||||
case exitErr = <-errCh:
|
||||
case <-notifyCtx.Done():
|
||||
exitErr = notifyCtx.Err()
|
||||
_, _ = fmt.Fprintln(inv.Stdout, cliui.DefaultStyles.Bold.Render(
|
||||
_, _ = fmt.Fprintln(inv.Stdout, cliui.Bold(
|
||||
"Interrupt caught, gracefully exiting. Use ctrl+\\ to force quit",
|
||||
))
|
||||
}
|
||||
|
||||
+4
-4
@@ -1,13 +1,13 @@
|
||||
Usage: coder [global-flags] <subcommand>
|
||||
|
||||
Coder v0.0.0-devel — A tool for provisioning self-hosted development environments with Terraform.
|
||||
- Start a Coder server:
|
||||
- Start a Coder server:
|
||||
|
||||
[40m [0m[91;40m$ coder server[0m[40m [0m
|
||||
$ coder server
|
||||
|
||||
- Get started by creating a template from an example:
|
||||
- Get started by creating a template from an example:
|
||||
|
||||
[40m [0m[91;40m$ coder templates init[0m[40m [0m
|
||||
$ coder templates init
|
||||
|
||||
[1mSubcommands[0m
|
||||
features List Enterprise features
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"github.com/fatih/color"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/pretty"
|
||||
|
||||
"github.com/coder/coder/v2/cli/clibase"
|
||||
"github.com/coder/coder/v2/cli/cliui"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
@@ -206,7 +208,7 @@ func (r *RootCmd) deleteProxy() *clibase.Cmd {
|
||||
|
||||
// Confirm deletion of the template.
|
||||
_, err = cliui.Prompt(inv, cliui.PromptOptions{
|
||||
Text: fmt.Sprintf("Delete this workspace proxy: %s?", cliui.DefaultStyles.Code.Render(wsproxy.DisplayName)),
|
||||
Text: fmt.Sprintf("Delete this workspace proxy: %s?", pretty.Sprint(cliui.DefaultStyles.Code, wsproxy.DisplayName)),
|
||||
IsConfirm: true,
|
||||
Default: cliui.ConfirmNo,
|
||||
})
|
||||
@@ -428,14 +430,14 @@ func newUpdateProxyResponseFormatter() *updateProxyResponseFormatter {
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Workspace Proxy %[1]q updated successfully.\n"+
|
||||
cliui.DefaultStyles.Placeholder.Render("—————————————————————————————————————————————————")+"\n"+
|
||||
pretty.Sprint(cliui.DefaultStyles.Placeholder, "—————————————————————————————————————————————————")+"\n"+
|
||||
"Save this authentication token, it will not be shown again.\n"+
|
||||
"Token: %[2]s\n"+
|
||||
"\n"+
|
||||
"Start the proxy by running:\n"+
|
||||
cliui.DefaultStyles.Code.Render("CODER_PROXY_SESSION_TOKEN=%[2]s coder wsproxy server --primary-access-url %[3]s --http-address=0.0.0.0:3001")+
|
||||
cliui.Code("CODER_PROXY_SESSION_TOKEN=%[2]s coder wsproxy server --primary-access-url %[3]s --http-address=0.0.0.0:3001")+
|
||||
// This is required to turn off the code style. Otherwise it appears in the code block until the end of the line.
|
||||
cliui.DefaultStyles.Placeholder.Render(""),
|
||||
pretty.Sprint(cliui.DefaultStyles.Placeholder, ""),
|
||||
response.Proxy.Name, response.ProxyToken, up.primaryAccessURL), nil
|
||||
}),
|
||||
cliui.JSONFormat(),
|
||||
|
||||
Reference in New Issue
Block a user