11import fs from 'node:fs/promises' ;
2- import { generateCompletionScript , SUPPORTED_COMPLETION_SHELLS } from './completion.mjs' ;
2+ import { generateCompletionScript , installCompletionScript , SUPPORTED_COMPLETION_SHELLS } from './completion.mjs' ;
33import { resolveLogPath } from './config.mjs' ;
44import {
55 buildConfigPathsReport ,
@@ -108,6 +108,7 @@ Usage:
108108 coder-studio config <subcommand>
109109 coder-studio auth <subcommand>
110110 coder-studio completion <bash|zsh|fish>
111+ coder-studio completion install <bash|zsh|fish> [--json]
111112 coder-studio --version
112113
113114Global Flags:
@@ -129,6 +130,7 @@ Examples:
129130 coder-studio config root set /srv/coder-studio/workspaces
130131 coder-studio auth ip list
131132 eval "$(coder-studio completion bash)"
133+ coder-studio completion install bash
132134
133135Run \`coder-studio config --help\`, \`coder-studio auth --help\`, or \`coder-studio help completion\` for detailed usage.
134136` ) ;
@@ -300,17 +302,33 @@ function printCompletionHelp() {
300302
301303Usage:
302304 coder-studio completion <bash|zsh|fish>
305+ coder-studio completion install <bash|zsh|fish> [--json]
303306
304307Description:
305- Print a shell completion script to stdout. Evaluate or source the output in your shell profile .
308+ Print or install shell completion scripts .
306309
307310Examples:
308311 eval "$(coder-studio completion bash)"
309312 source <(coder-studio completion zsh)
310313 coder-studio completion fish | source
314+ coder-studio completion install bash
315+ coder-studio completion install zsh --json
311316` ) ;
312317}
313318
319+ function printCompletionInstall ( result ) {
320+ console . log ( `installed: ${ result . shell } ` ) ;
321+ console . log ( `scriptPath: ${ result . scriptPath } ` ) ;
322+ if ( result . profilePath ) {
323+ console . log ( `profilePath: ${ result . profilePath } ` ) ;
324+ console . log ( `profileUpdated: ${ result . profileUpdated ? 'yes' : 'no' } ` ) ;
325+ } else {
326+ console . log ( 'profilePath: n/a' ) ;
327+ console . log ( 'profileUpdated: no' ) ;
328+ }
329+ console . log ( `activationCommand: ${ result . activationCommand } ` ) ;
330+ }
331+
314332function printHelpTopic ( topic ) {
315333 switch ( topic ) {
316334 case undefined :
@@ -933,23 +951,41 @@ export async function runCli(argv = process.argv.slice(2)) {
933951 }
934952
935953 if ( command === 'completion' ) {
936- if ( flags . json ) {
937- throw usageError ( 'completion does not support --json' , 'completion' ) ;
954+ const [ modeOrShell , maybeShell , ...rest ] = positionals ;
955+ if ( ! modeOrShell ) {
956+ printCompletionHelp ( ) ;
957+ return EXIT_SUCCESS ;
938958 }
939959
940- const [ shell , ...rest ] = positionals ;
941- if ( ! shell ) {
942- printCompletionHelp ( ) ;
960+ if ( modeOrShell === 'install' ) {
961+ const shell = maybeShell ;
962+ if ( ! shell ) {
963+ throw usageError ( 'completion install requires <bash|zsh|fish>' , 'completion' ) ;
964+ }
965+ if ( rest . length > 0 ) {
966+ throw usageError ( 'completion install accepts exactly one <shell> argument' , 'completion' ) ;
967+ }
968+ if ( ! SUPPORTED_COMPLETION_SHELLS . includes ( shell ) ) {
969+ throw usageError ( `unsupported completion shell: ${ shell } ` , 'completion' ) ;
970+ }
971+
972+ const result = await installCompletionScript ( shell ) ;
973+ if ( flags . json ) printJson ( result ) ;
974+ else printCompletionInstall ( result ) ;
943975 return EXIT_SUCCESS ;
944976 }
945- if ( rest . length > 0 ) {
977+
978+ if ( flags . json ) {
979+ throw usageError ( 'completion does not support --json' , 'completion' ) ;
980+ }
981+ if ( maybeShell || rest . length > 0 ) {
946982 throw usageError ( 'completion accepts exactly one <shell> argument' , 'completion' ) ;
947983 }
948- if ( ! SUPPORTED_COMPLETION_SHELLS . includes ( shell ) ) {
949- throw usageError ( `unsupported completion shell: ${ shell } ` , 'completion' ) ;
984+ if ( ! SUPPORTED_COMPLETION_SHELLS . includes ( modeOrShell ) ) {
985+ throw usageError ( `unsupported completion shell: ${ modeOrShell } ` , 'completion' ) ;
950986 }
951987
952- process . stdout . write ( generateCompletionScript ( shell ) ) ;
988+ process . stdout . write ( generateCompletionScript ( modeOrShell ) ) ;
953989 return EXIT_SUCCESS ;
954990 }
955991
0 commit comments