-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompletion.ts
More file actions
203 lines (191 loc) · 5.93 KB
/
Copy pathcompletion.ts
File metadata and controls
203 lines (191 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* `aiw completion` — emit shell-completion scripts for the user's
* shell of choice. Hand-rolled because citty ^0.2.2 (the pinned
* version) doesn't ship a completion generator.
*
* Walks the top-level subcommand tree (one level deep — that's where
* the discoverability win lives; per-flag completion is left for a
* future iteration). The user sources the emitted script from their
* shell rc file; see the README section for the one-liner.
*
* The list of subcommands and per-subcommand verbs is sourced from
* the constant below rather than from the citty command tree at
* runtime so the script remains a pure stdout dump (no need to
* import / construct the full command graph just to print names).
* Keep this list in sync with `src/cli.ts` when adding new commands.
*/
import { defineCommand } from "citty";
import { ExitCode } from "../exit-codes.js";
import { fail } from "../output.js";
type Shell = "bash" | "zsh" | "fish";
const SHELLS: ReadonlySet<Shell> = new Set(["bash", "zsh", "fish"]);
interface CommandSpec {
readonly name: string;
readonly summary: string;
readonly subcommands?: readonly string[];
}
/**
* Mirrors the subCommands record in `src/cli.ts`. Static so the
* completion script generation stays a pure function of the
* constant + the requested shell.
*/
const COMMANDS: readonly CommandSpec[] = [
{ name: "login", summary: "Save credentials for a runtime." },
{ name: "logout", summary: "Remove a stored profile." },
{ name: "whoami", summary: "Show who the runtime thinks you are." },
{
name: "profile",
summary: "Manage stored credential profiles.",
subcommands: ["ls", "use", "rm"],
},
{
name: "workspace",
summary: "Workspaces CRUD.",
subcommands: ["list", "create", "delete"],
},
{
name: "kb",
summary: "Knowledge-base CRUD.",
subcommands: ["list", "create"],
},
{
name: "db",
summary: "Astra DB helpers.",
subcommands: ["workbench", "ingest"],
},
{ name: "doc", summary: "Documents.", subcommands: ["upload"] },
{ name: "search", summary: "Search a knowledge base." },
{ name: "agent", summary: "Agents.", subcommands: ["list"] },
{ name: "chat", summary: "Chat with an agent." },
{ name: "job", summary: "Async jobs.", subcommands: ["status"] },
{
name: "shim",
summary: "`astra` shim install helpers.",
subcommands: ["path", "install"],
},
{ name: "completion", summary: "Print shell completion script." },
{ name: "doctor", summary: "Run pre-flight diagnostics." },
{ name: "status", summary: "Short health summary." },
];
function renderBash(): string {
const verbs = COMMANDS.map((c) => c.name).join(" ");
const cases = COMMANDS.filter((c) => c.subcommands?.length)
.map(
(c) =>
` ${c.name})
COMPREPLY=( $(compgen -W "${(c.subcommands ?? []).join(" ")}" -- "$cur") )
;;`,
)
.join("\n");
return `# aiw bash completion. Source from ~/.bashrc:
# eval "$(aiw completion bash)"
_aiw() {
local cur prev words cword
_init_completion || return
if [[ \${cword} -eq 1 ]]; then
COMPREPLY=( $(compgen -W "${verbs}" -- "$cur") )
return
fi
if [[ \${cword} -eq 2 ]]; then
case "\${words[1]}" in
${cases}
esac
fi
}
complete -F _aiw aiw
`;
}
function renderZsh(): string {
const lines: string[] = [];
lines.push("#compdef aiw");
lines.push("# aiw zsh completion. Source from ~/.zshrc:");
lines.push('# eval "$(aiw completion zsh)"');
lines.push("_aiw() {");
lines.push(" local -a verbs subverbs");
lines.push(" verbs=(");
for (const c of COMMANDS) {
lines.push(` '${c.name}:${escapeZsh(c.summary)}'`);
}
lines.push(" )");
lines.push(" if (( CURRENT == 2 )); then");
lines.push(" _describe -t commands 'aiw command' verbs");
lines.push(" return");
lines.push(" fi");
lines.push(" if (( CURRENT == 3 )); then");
lines.push(' case "$words[2]" in');
for (const c of COMMANDS) {
if (!c.subcommands?.length) continue;
lines.push(` ${c.name})`);
lines.push(" subverbs=(");
for (const sub of c.subcommands) {
lines.push(` '${sub}'`);
}
lines.push(" )");
lines.push(
" _describe -t subcommands 'subcommand' subverbs",
);
lines.push(" ;;");
}
lines.push(" esac");
lines.push(" fi");
lines.push("}");
lines.push("compdef _aiw aiw");
return `${lines.join("\n")}\n`;
}
function renderFish(): string {
const out: string[] = [];
out.push("# aiw fish completion. Save to:");
out.push("# ~/.config/fish/completions/aiw.fish");
out.push("# or pipe directly: aiw completion fish | source");
out.push(
"complete -c aiw -f -n '__fish_use_subcommand' -a '" +
COMMANDS.map((c) => c.name).join(" ") +
"'",
);
for (const c of COMMANDS) {
out.push(
`complete -c aiw -f -n '__fish_use_subcommand' -a '${c.name}' -d '${escapeFish(c.summary)}'`,
);
if (!c.subcommands?.length) continue;
for (const sub of c.subcommands) {
out.push(
`complete -c aiw -f -n '__fish_seen_subcommand_from ${c.name}' -a '${sub}'`,
);
}
}
return `${out.join("\n")}\n`;
}
function escapeZsh(s: string): string {
return s.replace(/\\/g, "\\\\").replace(/'/g, "'\\''").replace(/:/g, "\\:");
}
function escapeFish(s: string): string {
return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
}
export const completionCommand = defineCommand({
meta: {
name: "completion",
description:
"Emit a shell-completion script. Source from your shell rc — see README.",
},
args: {
shell: {
type: "positional",
required: true,
description: "Target shell: bash, zsh, or fish.",
},
},
run({ args }) {
const shell = args.shell as Shell;
if (!SHELLS.has(shell)) {
fail(`Unknown shell "${args.shell}". Expected: bash, zsh, or fish.`);
process.exit(ExitCode.USAGE_ERROR);
}
const script =
shell === "bash"
? renderBash()
: shell === "zsh"
? renderZsh()
: renderFish();
process.stdout.write(script);
},
});