Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): aliased commands can be converted to cli args #32806

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/aws-cdk/lib/convert-to-cli-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function convertToCliArgs(args: any): CliArguments {
let commandOptions;
switch (args._[0] as Command) {
case 'list':
case 'ls':
commandOptions = {
long: args.long,
showDependencies: args.showDependencies,
Expand All @@ -46,6 +47,7 @@ export function convertToCliArgs(args: any): CliArguments {
break;

case 'synthesize':
case 'synth':
commandOptions = {
exclusively: args.exclusively,
validation: args.validation,
Expand Down Expand Up @@ -193,6 +195,7 @@ export function convertToCliArgs(args: any): CliArguments {
break;

case 'acknowledge':
case 'ack':
commandOptions = {
ID: args.ID,
};
Expand Down Expand Up @@ -237,6 +240,7 @@ export function convertToCliArgs(args: any): CliArguments {
break;

case 'docs':
case 'doc':
commandOptions = {
browser: args.browser,
};
Expand Down
2 changes: 2 additions & 0 deletions packages/aws-cdk/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ export enum Command {
ROLLBACK = 'rollback',
IMPORT = 'import',
ACKNOWLEDGE = 'acknowledge',
ACK = 'ack',
NOTICES = 'notices',
MIGRATE = 'migrate',
CONTEXT = 'context',
DOCS = 'docs',
DOC = 'doc',
DOCTOR = 'doctor',
}

Expand Down
9 changes: 8 additions & 1 deletion tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ function buildCommandSwitch(config: CliConfig): string {
const commandSwitchExprs = ['let commandOptions;', 'switch (args._[0] as Command) {'];
for (const commandName of Object.keys(config.commands)) {
commandSwitchExprs.push(
`case '${commandName}':`,
// All aliases of the command should map to the same switch branch
// This ensures that we store options of the command regardless of what alias is specified
...buildAliases(commandName, config.commands[commandName].aliases),
'commandOptions = {',
...buildCommandOptions(config.commands[commandName]),
...(config.commands[commandName].arg ? [buildPositionalArguments(config.commands[commandName].arg)] : []),
Expand All @@ -76,6 +78,11 @@ function buildCommandSwitch(config: CliConfig): string {
return commandSwitchExprs.join('\n');
}

function buildAliases(commandName: string, aliases: string[] = []): string[] {
const cases = [commandName, ...aliases];
return cases.map((c) => `case '${c}':`);
}

function buildCommandOptions(options: CliAction): string[] {
const commandOptions: string[] = [];
for (const optionName of Object.keys(options.options ?? {})) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('render', () => {
variadic: true,
},
description: 'Deploy a stack',
aliases: ['d'],
options: {
all: {
type: 'boolean',
Expand Down Expand Up @@ -62,6 +63,7 @@ describe('render', () => {
let commandOptions;
switch (args._[0] as Command) {
case 'deploy':
case 'd':
commandOptions = {
all: args.all,
STACKS: args.STACKS,
Expand Down
Loading