ktx/packages/cli/src/commands/connection-commands.ts

45 lines
1.7 KiB
TypeScript
Raw Normal View History

2026-05-13 14:21:05 +02:00
import { type Command } from '@commander-js/extra-typings';
import { type KtxCliCommandContext, resolveCommandProjectDir } from '../cli-program.js';
2026-05-10 23:51:24 +02:00
import type { KtxConnectionArgs } from '../connection.js';
2026-05-10 23:12:26 +02:00
import { profileMark } from '../startup-profile.js';
profileMark('module:commands/connection-commands');
2026-05-10 23:51:24 +02:00
async function runConnectionArgs(context: KtxCliCommandContext, args: KtxConnectionArgs): Promise<void> {
const runner = context.deps.connection ?? (await import('../connection.js')).runKtxConnection;
2026-05-10 23:12:26 +02:00
context.setExitCode(await runner(args, context.io));
}
2026-05-10 23:51:24 +02:00
export function registerConnectionCommands(program: Command, context: KtxCliCommandContext, commandName = 'connection'): void {
2026-05-10 23:12:26 +02:00
const connection = program
.command(commandName)
2026-05-13 14:21:05 +02:00
.description('List and test configured connections')
2026-05-10 23:12:26 +02:00
.showHelpAfterError()
.addHelpText(
'after',
2026-05-10 23:51:24 +02:00
'\nProject directory defaults to KTX_PROJECT_DIR when set, otherwise the nearest ktx.yaml or current working directory.\n',
2026-05-10 23:12:26 +02:00
);
connection.hook('preAction', (_thisCommand, actionCommand) => {
context.writeDebug?.(commandName, actionCommand);
});
connection
.command('list')
.description('List configured connections')
.action(async (_options: unknown, command) => {
await runConnectionArgs(context, { command: 'list', projectDir: resolveCommandProjectDir(command) });
});
connection
.command('test')
.description('Test a configured connection')
2026-05-10 23:51:24 +02:00
.argument('<connectionId>', 'KTX connection id')
2026-05-10 23:12:26 +02:00
.action(async (connectionId: string, _options: unknown, command) => {
await runConnectionArgs(context, {
command: 'test',
projectDir: resolveCommandProjectDir(command),
connectionId,
});
});
}