Skip to content

Commit 7f7a5e7

Browse files
committed
feat(cli): add shell completion command
1 parent 1f03795 commit 7f7a5e7

7 files changed

Lines changed: 550 additions & 1 deletion

File tree

README.en.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ coder-studio config password set --stdin
7777
coder-studio auth status
7878
coder-studio auth ip list
7979
coder-studio help start
80+
coder-studio help completion
81+
eval "$(coder-studio completion bash)"
8082
```
8183

8284
For the detailed command reference, see `docs/development/cli.en.md`.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ coder-studio config password set --stdin
7676
coder-studio auth status
7777
coder-studio auth ip list
7878
coder-studio help start
79+
coder-studio help completion
80+
eval "$(coder-studio completion bash)"
7981
```
8082

8183
详细命令说明见:`docs/development/cli.md`

docs/development/cli.en.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ coder-studio auth ip unblock 203.0.113.12
7474
coder-studio auth ip unblock --all
7575
```
7676

77+
Shell completion:
78+
79+
```bash
80+
coder-studio completion bash
81+
coder-studio completion zsh
82+
coder-studio completion fish
83+
```
84+
7785
Version:
7886

7987
```bash
@@ -87,6 +95,7 @@ coder-studio help
8795
coder-studio help start
8896
coder-studio help config
8997
coder-studio help auth
98+
coder-studio help completion
9099
```
91100

92101
## Global Flags
@@ -105,6 +114,31 @@ Additional flags on specific commands:
105114
- `config password set --stdin`: reads the passphrase from stdin
106115
- `auth ip unblock --all`: removes all currently blocked IPs at once
107116

117+
## Shell Completion
118+
119+
The CLI can print completion scripts for `bash`, `zsh`, and `fish`. The command only writes the script to stdout.
120+
121+
Load it for the current shell session:
122+
123+
```bash
124+
eval "$(coder-studio completion bash)"
125+
source <(coder-studio completion zsh)
126+
coder-studio completion fish | source
127+
```
128+
129+
Persistent installation examples:
130+
131+
```bash
132+
coder-studio completion bash >> ~/.bashrc
133+
coder-studio completion zsh > ~/.zfunc/_coder-studio
134+
coder-studio completion fish > ~/.config/fish/completions/coder-studio.fish
135+
```
136+
137+
Notes:
138+
139+
- `completion` does not support `--json`
140+
- `coder-studio help completion` shows command usage
141+
108142
## Exit Codes
109143

110144
- `0`: success

docs/development/cli.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ coder-studio auth ip unblock 203.0.113.12
7474
coder-studio auth ip unblock --all
7575
```
7676

77+
Shell Completion:
78+
79+
```bash
80+
coder-studio completion bash
81+
coder-studio completion zsh
82+
coder-studio completion fish
83+
```
84+
7785
版本信息:
7886

7987
```bash
@@ -87,6 +95,7 @@ coder-studio help
8795
coder-studio help start
8896
coder-studio help config
8997
coder-studio help auth
98+
coder-studio help completion
9099
```
91100

92101
## 全局参数
@@ -105,6 +114,31 @@ coder-studio help auth
105114
- `config password set --stdin`:从标准输入读取口令
106115
- `auth ip unblock --all`:一次解除全部当前封禁 IP
107116

117+
## Shell Completion
118+
119+
CLI 可以输出 `bash``zsh``fish` 的补全脚本,命令本身只负责把脚本打印到标准输出。
120+
121+
即时加载:
122+
123+
```bash
124+
eval "$(coder-studio completion bash)"
125+
source <(coder-studio completion zsh)
126+
coder-studio completion fish | source
127+
```
128+
129+
持久化安装示例:
130+
131+
```bash
132+
coder-studio completion bash >> ~/.bashrc
133+
coder-studio completion zsh > ~/.zfunc/_coder-studio
134+
coder-studio completion fish > ~/.config/fish/completions/coder-studio.fish
135+
```
136+
137+
说明:
138+
139+
- `completion` 不支持 `--json`
140+
- `coder-studio help completion` 可查看命令说明
141+
108142
## 退出码
109143

110144
- `0`:执行成功

packages/coder-studio/lib/cli.mjs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs/promises';
2+
import { generateCompletionScript, SUPPORTED_COMPLETION_SHELLS } from './completion.mjs';
23
import { resolveLogPath } from './config.mjs';
34
import {
45
buildConfigPathsReport,
@@ -106,6 +107,7 @@ Usage:
106107
coder-studio doctor [--json]
107108
coder-studio config <subcommand>
108109
coder-studio auth <subcommand>
110+
coder-studio completion <bash|zsh|fish>
109111
coder-studio --version
110112
111113
Global Flags:
@@ -121,12 +123,14 @@ Exit Codes:
121123
122124
Examples:
123125
coder-studio help start
126+
coder-studio help completion
124127
coder-studio start
125128
coder-studio config show --json
126129
coder-studio config root set /srv/coder-studio/workspaces
127130
coder-studio auth ip list
131+
eval "$(coder-studio completion bash)"
128132
129-
Run \`coder-studio config --help\` or \`coder-studio auth --help\` for detailed usage.
133+
Run \`coder-studio config --help\`, \`coder-studio auth --help\`, or \`coder-studio help completion\` for detailed usage.
130134
`);
131135
}
132136

@@ -291,6 +295,22 @@ Examples:
291295
`);
292296
}
293297

298+
function printCompletionHelp() {
299+
console.log(`coder-studio completion
300+
301+
Usage:
302+
coder-studio completion <bash|zsh|fish>
303+
304+
Description:
305+
Print a shell completion script to stdout. Evaluate or source the output in your shell profile.
306+
307+
Examples:
308+
eval "$(coder-studio completion bash)"
309+
source <(coder-studio completion zsh)
310+
coder-studio completion fish | source
311+
`);
312+
}
313+
294314
function printHelpTopic(topic) {
295315
switch (topic) {
296316
case undefined:
@@ -326,6 +346,9 @@ function printHelpTopic(topic) {
326346
case 'auth':
327347
printAuthHelp();
328348
return EXIT_SUCCESS;
349+
case 'completion':
350+
printCompletionHelp();
351+
return EXIT_SUCCESS;
329352
default:
330353
throw usageError(`unsupported help topic: ${topic}`, 'main');
331354
}
@@ -884,6 +907,8 @@ function printCliError(error, flags) {
884907
console.error('hint: run `coder-studio config --help`');
885908
} else if (normalized.helpTopic === 'auth') {
886909
console.error('hint: run `coder-studio auth --help`');
910+
} else if (normalized.helpTopic === 'completion') {
911+
console.error('hint: run `coder-studio help completion`');
887912
} else if (normalized.helpTopic === 'main') {
888913
console.error('hint: run `coder-studio help`');
889914
}
@@ -907,6 +932,27 @@ export async function runCli(argv = process.argv.slice(2)) {
907932
return printHelpTopic(command);
908933
}
909934

935+
if (command === 'completion') {
936+
if (flags.json) {
937+
throw usageError('completion does not support --json', 'completion');
938+
}
939+
940+
const [shell, ...rest] = positionals;
941+
if (!shell) {
942+
printCompletionHelp();
943+
return EXIT_SUCCESS;
944+
}
945+
if (rest.length > 0) {
946+
throw usageError('completion accepts exactly one <shell> argument', 'completion');
947+
}
948+
if (!SUPPORTED_COMPLETION_SHELLS.includes(shell)) {
949+
throw usageError(`unsupported completion shell: ${shell}`, 'completion');
950+
}
951+
952+
process.stdout.write(generateCompletionScript(shell));
953+
return EXIT_SUCCESS;
954+
}
955+
910956
if (command === 'config') {
911957
const context = await resolveCommandContext(flags);
912958
return await handleConfigCommand(positionals, flags, context);

0 commit comments

Comments
 (0)