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

Skip to content

Commit e361f11

Browse files
authored
feat(cli): colorize help page (#9589)
1 parent 11404af commit e361f11

File tree

86 files changed

+1240
-936
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1240
-936
lines changed

cli/cliui/cliui.go

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ var (
5151
Blue = color.Color("#5000ff")
5252
)
5353

54+
// Color returns a color for the given string.
55+
func Color(s string) termenv.Color {
56+
return color.Color(s)
57+
}
58+
5459
func isTerm() bool {
5560
return color != termenv.Ascii
5661
}

cli/help.go

+24-12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
"golang.org/x/crypto/ssh/terminal"
1818
"golang.org/x/xerrors"
1919

20+
"github.com/coder/coder/v2/buildinfo"
2021
"github.com/coder/coder/v2/cli/clibase"
2122
"github.com/coder/coder/v2/cli/cliui"
23+
"github.com/coder/pretty"
2224
)
2325

2426
//go:embed help.tpl
@@ -47,12 +49,28 @@ func wrapTTY(s string) string {
4749
var usageTemplate = template.Must(
4850
template.New("usage").Funcs(
4951
template.FuncMap{
52+
"version": func() string {
53+
return buildinfo.Version()
54+
},
5055
"wrapTTY": func(s string) string {
5156
return wrapTTY(s)
5257
},
5358
"trimNewline": func(s string) string {
5459
return strings.TrimSuffix(s, "\n")
5560
},
61+
"keyword": func(s string) string {
62+
return pretty.Sprint(
63+
pretty.FgColor(cliui.Color("#0173ff")),
64+
s,
65+
)
66+
},
67+
"prettyHeader": func(s string) string {
68+
return pretty.Sprint(
69+
pretty.FgColor(
70+
cliui.Color("#ffb500"),
71+
), strings.ToUpper(s), ":",
72+
)
73+
},
5674
"typeHelper": func(opt *clibase.Option) string {
5775
switch v := opt.Value.(type) {
5876
case *clibase.Enum:
@@ -71,13 +89,15 @@ var usageTemplate = template.Must(
7189

7290
body = wordwrap.WrapString(body, uint(twidth-len(spacing)))
7391

92+
sc := bufio.NewScanner(strings.NewReader(body))
93+
7494
var sb strings.Builder
75-
for _, line := range strings.Split(body, "\n") {
95+
for sc.Scan() {
7696
// Remove existing indent, if any.
77-
line = strings.TrimSpace(line)
97+
// line = strings.TrimSpace(line)
7898
// Use spaces so we can easily calculate wrapping.
7999
_, _ = sb.WriteString(spacing)
80-
_, _ = sb.WriteString(line)
100+
_, _ = sb.Write(sc.Bytes())
81101
_, _ = sb.WriteString("\n")
82102
}
83103
return sb.String()
@@ -126,9 +146,7 @@ var usageTemplate = template.Must(
126146
"flagName": func(opt clibase.Option) string {
127147
return opt.Flag
128148
},
129-
"prettyHeader": func(s string) string {
130-
return cliui.Bold(s)
131-
},
149+
132150
"isEnterprise": func(opt clibase.Option) bool {
133151
return opt.Annotations.IsSet("enterprise")
134152
},
@@ -160,12 +178,6 @@ var usageTemplate = template.Must(
160178
}
161179
return sb.String()
162180
},
163-
"formatLong": func(long string) string {
164-
// We intentionally don't wrap here because it would misformat
165-
// examples, where the new line would start without the prior
166-
// line's indentation.
167-
return strings.TrimSpace(long)
168-
},
169181
"formatGroupDescription": func(s string) string {
170182
s = strings.ReplaceAll(s, "\n", "")
171183
s = s + "\n"

cli/help.tpl

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
{{- /* Heavily inspired by the Go toolchain formatting. */ -}}
2-
Usage: {{.FullUsage}}
1+
{{- /* Heavily inspired by the Go toolchain and fd */ -}}
2+
coder {{version}}
3+
4+
{{prettyHeader "Usage"}}
5+
{{indent .FullUsage 2}}
36

47

58
{{ with .Short }}
6-
{{- wrapTTY . }}
9+
{{- indent . 2 | wrapTTY }}
710
{{"\n"}}
811
{{- end}}
912

1013
{{ with .Aliases }}
11-
{{ "\n" }}
12-
{{ "Aliases:"}} {{ joinStrings .}}
13-
{{ "\n" }}
14+
{{" Aliases: "}} {{- joinStrings .}}
1415
{{- end }}
1516

1617
{{- with .Long}}
17-
{{- formatLong . }}
18+
{{"\n"}}
19+
{{- indent . 2}}
1820
{{ "\n" }}
1921
{{- end }}
2022
{{ with visibleChildren . }}
@@ -34,11 +36,11 @@ Usage: {{.FullUsage}}
3436
{{- else }}
3537
{{- end }}
3638
{{- range $index, $option := $group.Options }}
37-
{{- if not (eq $option.FlagShorthand "") }}{{- print "\n -" $option.FlagShorthand ", " -}}
39+
{{- if not (eq $option.FlagShorthand "") }}{{- print "\n "}} {{ keyword "-"}}{{keyword $option.FlagShorthand }}{{", "}}
3840
{{- else }}{{- print "\n " -}}
3941
{{- end }}
40-
{{- with flagName $option }}--{{ . }}{{ end }} {{- with typeHelper $option }} {{ . }}{{ end }}
41-
{{- with envName $option }}, ${{ . }}{{ end }}
42+
{{- with flagName $option }}{{keyword "--"}}{{ keyword . }}{{ end }} {{- with typeHelper $option }} {{ . }}{{ end }}
43+
{{- with envName $option }}, {{ print "$" . | keyword }}{{ end }}
4244
{{- with $option.Default }} (default: {{ . }}){{ end }}
4345
{{- with $option.Description }}
4446
{{- $desc := $option.Description }}
@@ -47,7 +49,7 @@ Usage: {{.FullUsage}}
4749
{{- end -}}
4850
{{- end }}
4951
{{- end }}
50-
---
52+
———
5153
{{- if .Parent }}
5254
Run `coder --help` for a list of global options.
5355
{{- else }}

cli/testdata/coder_--help.golden

+28-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
Usage: coder [global-flags] <subcommand>
2-
3-
Coder v0.0.0-devel — A tool for provisioning self-hosted development environments with Terraform.
4-
- Start a Coder server:
5-
6-
$ coder server
7-
8-
- Get started by creating a template from an example:
9-
10-
$ coder templates init
11-
12-
Subcommands
1+
coder v0.0.0-devel
2+
3+
USAGE:
4+
coder [global-flags] <subcommand>
5+
6+
Coder v0.0.0-devel — A tool for provisioning self-hosted development
7+
environments with Terraform.
8+
- Start a Coder server:
9+
10+
$ coder server
11+
12+
- Get started by creating a template from an example:
13+
14+
$ coder templates init
15+
16+
SUBCOMMANDS:
1317
config-ssh Add an SSH Host entry for your workspaces "ssh
1418
coder.workspace"
1519
create Create a workspace
@@ -45,43 +49,43 @@ Coder v0.0.0-devel — A tool for provisioning self-hosted development environme
4549
users Manage users
4650
version Show coder version
4751

48-
Global Options
52+
GLOBAL OPTIONS:
4953
Global options are applied to all commands. They can be set using environment
5054
variables or flags.
5155

52-
--debug-options bool
56+
--debug-options bool
5357
Print all options, how they're set, then exit.
5458

55-
--disable-direct-connections bool, $CODER_DISABLE_DIRECT_CONNECTIONS
59+
--disable-direct-connections bool, $CODER_DISABLE_DIRECT_CONNECTIONS
5660
Disable direct (P2P) connections to workspaces.
5761

58-
--global-config string, $CODER_CONFIG_DIR (default: ~/.config/coderv2)
62+
--global-config string, $CODER_CONFIG_DIR (default: ~/.config/coderv2)
5963
Path to the global `coder` config directory.
6064

61-
--header string-array, $CODER_HEADER
65+
--header string-array, $CODER_HEADER
6266
Additional HTTP headers added to all requests. Provide as key=value.
6367
Can be specified multiple times.
6468

65-
--header-command string, $CODER_HEADER_COMMAND
69+
--header-command string, $CODER_HEADER_COMMAND
6670
An external command that outputs additional HTTP headers added to all
6771
requests. The command must output each header as `key=value` on its
6872
own line.
6973

70-
--no-feature-warning bool, $CODER_NO_FEATURE_WARNING
74+
--no-feature-warning bool, $CODER_NO_FEATURE_WARNING
7175
Suppress warnings about unlicensed features.
7276

73-
--no-version-warning bool, $CODER_NO_VERSION_WARNING
77+
--no-version-warning bool, $CODER_NO_VERSION_WARNING
7478
Suppress warning when client and server versions do not match.
7579

76-
--token string, $CODER_SESSION_TOKEN
80+
--token string, $CODER_SESSION_TOKEN
7781
Specify an authentication token. For security reasons setting
7882
CODER_SESSION_TOKEN is preferred.
7983

80-
--url url, $CODER_URL
84+
--url url, $CODER_URL
8185
URL to a deployment.
8286

83-
-v, --verbose bool, $CODER_VERBOSE
87+
-v, --verbose bool, $CODER_VERBOSE
8488
Enable verbose output.
8589

86-
---
90+
———
8791
Report bugs and request features at https://github.com/coder/coder/issues/new
+18-15
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
Usage: coder agent [flags]
1+
coder v0.0.0-devel
22

3-
Starts the Coder workspace agent.
3+
USAGE:
4+
coder agent [flags]
45

5-
Options
6-
--log-human string, $CODER_AGENT_LOGGING_HUMAN (default: /dev/stderr)
6+
Starts the Coder workspace agent.
7+
8+
OPTIONS:
9+
--log-human string, $CODER_AGENT_LOGGING_HUMAN (default: /dev/stderr)
710
Output human-readable logs to a given file.
811

9-
--log-json string, $CODER_AGENT_LOGGING_JSON
12+
--log-json string, $CODER_AGENT_LOGGING_JSON
1013
Output JSON logs to a given file.
1114

12-
--log-stackdriver string, $CODER_AGENT_LOGGING_STACKDRIVER
15+
--log-stackdriver string, $CODER_AGENT_LOGGING_STACKDRIVER
1316
Output Stackdriver compatible logs to a given file.
1417

15-
--auth string, $CODER_AGENT_AUTH (default: token)
18+
--auth string, $CODER_AGENT_AUTH (default: token)
1619
Specify the authentication type to use for the agent.
1720

18-
--debug-address string, $CODER_AGENT_DEBUG_ADDRESS (default: 127.0.0.1:2113)
21+
--debug-address string, $CODER_AGENT_DEBUG_ADDRESS (default: 127.0.0.1:2113)
1922
The bind address to serve a debug HTTP server.
2023

21-
--log-dir string, $CODER_AGENT_LOG_DIR (default: /tmp)
24+
--log-dir string, $CODER_AGENT_LOG_DIR (default: /tmp)
2225
Specify the location for the agent log files.
2326

24-
--no-reap bool
27+
--no-reap bool
2528
Do not start a process reaper.
2629

27-
--pprof-address string, $CODER_AGENT_PPROF_ADDRESS (default: 127.0.0.1:6060)
30+
--pprof-address string, $CODER_AGENT_PPROF_ADDRESS (default: 127.0.0.1:6060)
2831
The address to serve pprof.
2932

30-
--prometheus-address string, $CODER_AGENT_PROMETHEUS_ADDRESS (default: 127.0.0.1:2112)
33+
--prometheus-address string, $CODER_AGENT_PROMETHEUS_ADDRESS (default: 127.0.0.1:2112)
3134
The bind address to serve Prometheus metrics.
3235

33-
--ssh-max-timeout duration, $CODER_AGENT_SSH_MAX_TIMEOUT (default: 72h)
36+
--ssh-max-timeout duration, $CODER_AGENT_SSH_MAX_TIMEOUT (default: 72h)
3437
Specify the max timeout for a SSH connection, it is advisable to set
3538
it to a minimum of 60s, but no more than 72h.
3639

37-
--tailnet-listen-port int, $CODER_AGENT_TAILNET_LISTEN_PORT (default: 0)
40+
--tailnet-listen-port int, $CODER_AGENT_TAILNET_LISTEN_PORT (default: 0)
3841
Specify a static port for Tailscale to use for listening.
3942

40-
---
43+
———
4144
Run `coder --help` for a list of global options.
+24-20
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,55 @@
1-
Usage: coder config-ssh [flags]
1+
coder v0.0.0-devel
22

3-
Add an SSH Host entry for your workspaces "ssh coder.workspace"
3+
USAGE:
4+
coder config-ssh [flags]
45

5-
- You can use -o (or --ssh-option) so set SSH options to be used for all your
6-
workspaces:
6+
Add an SSH Host entry for your workspaces "ssh coder.workspace"
77

8-
$ coder config-ssh -o ForwardAgent=yes
8+
- You can use -o (or --ssh-option) so set SSH options to be used for all
9+
your
10+
workspaces:
11+
12+
$ coder config-ssh -o ForwardAgent=yes
13+
14+
- You can use --dry-run (or -n) to see the changes that would be made:
15+
16+
$ coder config-ssh --dry-run
917

10-
- You can use --dry-run (or -n) to see the changes that would be made:
11-
12-
$ coder config-ssh --dry-run
13-
14-
Options
15-
--coder-binary-path string, $CODER_SSH_CONFIG_BINARY_PATH
18+
OPTIONS:
19+
--coder-binary-path string, $CODER_SSH_CONFIG_BINARY_PATH
1620
Optionally specify the absolute path to the coder binary used in
1721
ProxyCommand. By default, the binary invoking this command ('config
1822
ssh') is used.
1923

20-
-n, --dry-run bool, $CODER_SSH_DRY_RUN
24+
-n, --dry-run bool, $CODER_SSH_DRY_RUN
2125
Perform a trial run with no changes made, showing a diff at the end.
2226

23-
--force-unix-filepaths bool, $CODER_CONFIGSSH_UNIX_FILEPATHS
27+
--force-unix-filepaths bool, $CODER_CONFIGSSH_UNIX_FILEPATHS
2428
By default, 'config-ssh' uses the os path separator when writing the
2529
ssh config. This might be an issue in Windows machine that use a
2630
unix-like shell. This flag forces the use of unix file paths (the
2731
forward slash '/').
2832

29-
--ssh-config-file string, $CODER_SSH_CONFIG_FILE (default: ~/.ssh/config)
33+
--ssh-config-file string, $CODER_SSH_CONFIG_FILE (default: ~/.ssh/config)
3034
Specifies the path to an SSH config.
3135

32-
--ssh-host-prefix string, $CODER_CONFIGSSH_SSH_HOST_PREFIX
36+
--ssh-host-prefix string, $CODER_CONFIGSSH_SSH_HOST_PREFIX
3337
Override the default host prefix.
3438

35-
-o, --ssh-option string-array, $CODER_SSH_CONFIG_OPTS
39+
-o, --ssh-option string-array, $CODER_SSH_CONFIG_OPTS
3640
Specifies additional SSH options to embed in each host stanza.
3741

38-
--use-previous-options bool, $CODER_SSH_USE_PREVIOUS_OPTIONS
42+
--use-previous-options bool, $CODER_SSH_USE_PREVIOUS_OPTIONS
3943
Specifies whether or not to keep options from previous run of
4044
config-ssh.
4145

42-
--wait yes|no|auto, $CODER_CONFIGSSH_WAIT (default: auto)
46+
--wait yes|no|auto, $CODER_CONFIGSSH_WAIT (default: auto)
4347
Specifies whether or not to wait for the startup script to finish
4448
executing. Auto means that the agent startup script behavior
4549
configured in the workspace template is used.
4650

47-
-y, --yes bool
51+
-y, --yes bool
4852
Bypass prompts.
4953

50-
---
54+
———
5155
Run `coder --help` for a list of global options.

0 commit comments

Comments
 (0)