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): function to turn yargs output to cliArguments #32696

Merged
merged 14 commits into from
Jan 2, 2025
Merged
240 changes: 240 additions & 0 deletions packages/aws-cdk/lib/convert-to-cli-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
// -------------------------------------------------------------------------------------------
// GENERATED FROM packages/aws-cdk/lib/config.ts.
// Do not edit by hand; all changes will be overwritten at build time from the config file.
// -------------------------------------------------------------------------------------------
/* eslint-disable @stylistic/max-len */
import { CliArguments, GlobalOptions } from './cli-arguments';
import { Command } from './settings';

// @ts-ignore TS6133
export function convertToCliArgs(args: any): CliArguments {
const globalOptions: GlobalOptions = {
app: args.app,
build: args.build,
context: args.context,
plugin: args.plugin,
trace: args.trace,
strict: args.strict,
lookups: args.lookups,
ignoreErrors: args.ignoreErrors,
json: args.json,
verbose: args.verbose,
debug: args.debug,
profile: args.profile,
proxy: args.proxy,
caBundlePath: args.caBundlePath,
ec2creds: args.ec2creds,
versionReporting: args.versionReporting,
pathMetadata: args.pathMetadata,
assetMetadata: args.assetMetadata,
roleArn: args.roleArn,
staging: args.staging,
output: args.output,
notices: args.notices,
noColor: args.noColor,
ci: args.ci,
unstable: args.unstable,
};
let commandOptions;
switch (args._[0] as Command) {
case 'list':
commandOptions = {

Check warning on line 41 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L41

Added line #L41 was not covered by tests
long: args.long,
showDependencies: args.showDependencies,
};
break;

Check warning on line 45 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L45

Added line #L45 was not covered by tests

case 'synthesize':
commandOptions = {

Check warning on line 48 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L48

Added line #L48 was not covered by tests
exclusively: args.exclusively,
validation: args.validation,
quiet: args.quiet,
};
break;

Check warning on line 53 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L53

Added line #L53 was not covered by tests

case 'bootstrap':
commandOptions = {

Check warning on line 56 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L56

Added line #L56 was not covered by tests
bootstrapBucketName: args.bootstrapBucketName,
bootstrapKmsKeyId: args.bootstrapKmsKeyId,
examplePermissionsBoundary: args.examplePermissionsBoundary,
customPermissionsBoundary: args.customPermissionsBoundary,
bootstrapCustomerKey: args.bootstrapCustomerKey,
qualifier: args.qualifier,
publicAccessBlockConfiguration: args.publicAccessBlockConfiguration,
tags: args.tags,
execute: args.execute,
trust: args.trust,
trustForLookup: args.trustForLookup,
cloudformationExecutionPolicies: args.cloudformationExecutionPolicies,
force: args.force,
terminationProtection: args.terminationProtection,
showTemplate: args.showTemplate,
toolkitStackName: args.toolkitStackName,
template: args.template,
previousParameters: args.previousParameters,
};
break;

Check warning on line 76 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L76

Added line #L76 was not covered by tests

case 'gc':
commandOptions = {

Check warning on line 79 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L79

Added line #L79 was not covered by tests
action: args.action,
type: args.type,
rollbackBufferDays: args.rollbackBufferDays,
createdBufferDays: args.createdBufferDays,
confirm: args.confirm,
bootstrapStackName: args.bootstrapStackName,
};
break;

Check warning on line 87 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L87

Added line #L87 was not covered by tests

case 'deploy':
commandOptions = {
all: args.all,
buildExclude: args.buildExclude,
exclusively: args.exclusively,
requireApproval: args.requireApproval,
notificationArns: args.notificationArns,
tags: args.tags,
execute: args.execute,
changeSetName: args.changeSetName,
method: args.method,
force: args.force,
parameters: args.parameters,
outputsFile: args.outputsFile,
previousParameters: args.previousParameters,
toolkitStackName: args.toolkitStackName,
progress: args.progress,
rollback: args.rollback,
hotswap: args.hotswap,
hotswapFallback: args.hotswapFallback,
watch: args.watch,
logs: args.logs,
concurrency: args.concurrency,
assetParallelism: args.assetParallelism,
assetPrebuild: args.assetPrebuild,
ignoreNoStacks: args.ignoreNoStacks,
};
break;

case 'rollback':
commandOptions = {

Check warning on line 119 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L119

Added line #L119 was not covered by tests
all: args.all,
toolkitStackName: args.toolkitStackName,
force: args.force,
validateBootstrapVersion: args.validateBootstrapVersion,
orphan: args.orphan,
};
break;

Check warning on line 126 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L126

Added line #L126 was not covered by tests

case 'import':
commandOptions = {

Check warning on line 129 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L129

Added line #L129 was not covered by tests
execute: args.execute,
changeSetName: args.changeSetName,
toolkitStackName: args.toolkitStackName,
rollback: args.rollback,
force: args.force,
recordResourceMapping: args.recordResourceMapping,
resourceMapping: args.resourceMapping,
};
break;

Check warning on line 138 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L138

Added line #L138 was not covered by tests

case 'watch':
commandOptions = {

Check warning on line 141 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L141

Added line #L141 was not covered by tests
buildExclude: args.buildExclude,
exclusively: args.exclusively,
changeSetName: args.changeSetName,
force: args.force,
toolkitStackName: args.toolkitStackName,
progress: args.progress,
rollback: args.rollback,
hotswap: args.hotswap,
hotswapFallback: args.hotswapFallback,
logs: args.logs,
concurrency: args.concurrency,
};
break;

Check warning on line 154 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L154

Added line #L154 was not covered by tests

case 'destroy':
commandOptions = {

Check warning on line 157 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L157

Added line #L157 was not covered by tests
all: args.all,
exclusively: args.exclusively,
force: args.force,
};
break;

Check warning on line 162 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L162

Added line #L162 was not covered by tests

case 'diff':
commandOptions = {

Check warning on line 165 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L165

Added line #L165 was not covered by tests
exclusively: args.exclusively,
contextLines: args.contextLines,
template: args.template,
strict: args.strict,
securityOnly: args.securityOnly,
fail: args.fail,
processed: args.processed,
quiet: args.quiet,
changeSet: args.changeSet,
};
break;

Check warning on line 176 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L176

Added line #L176 was not covered by tests

case 'metadata':
commandOptions = {};
break;

Check warning on line 180 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L179-L180

Added lines #L179 - L180 were not covered by tests

case 'acknowledge':
commandOptions = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at our current for this command I see:

case 'ack':
return cli.acknowledge(args.ID);

Should this PR also handle positional arguments? I would expect we have a strongly typed args.acknowledge.id or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. A big part of my refactor is to make sure we don't have to use args, argv, and configuration.settings everywhere and instead just have 1 object with arguments.

Currently those positional arguments are found in index 1 and beyond on argv._: argv._: [Command, ...string]

It will be a bit awkward to figure out the right way to deal with that, which I would like to do in a separate PR.

break;

Check warning on line 184 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L183-L184

Added lines #L183 - L184 were not covered by tests

case 'notices':
commandOptions = {

Check warning on line 187 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L187

Added line #L187 was not covered by tests
unacknowledged: args.unacknowledged,
};
break;

Check warning on line 190 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L190

Added line #L190 was not covered by tests

case 'init':
commandOptions = {

Check warning on line 193 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L193

Added line #L193 was not covered by tests
language: args.language,
list: args.list,
generateOnly: args.generateOnly,
};
break;

Check warning on line 198 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L198

Added line #L198 was not covered by tests

case 'migrate':
commandOptions = {

Check warning on line 201 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L201

Added line #L201 was not covered by tests
stackName: args.stackName,
language: args.language,
account: args.account,
region: args.region,
fromPath: args.fromPath,
fromStack: args.fromStack,
outputPath: args.outputPath,
fromScan: args.fromScan,
filter: args.filter,
compress: args.compress,
};
break;

Check warning on line 213 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L213

Added line #L213 was not covered by tests

case 'context':
commandOptions = {

Check warning on line 216 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L216

Added line #L216 was not covered by tests
reset: args.reset,
force: args.force,
clear: args.clear,
};
break;

Check warning on line 221 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L221

Added line #L221 was not covered by tests

case 'docs':
commandOptions = {

Check warning on line 224 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L224

Added line #L224 was not covered by tests
browser: args.browser,
};
break;

Check warning on line 227 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L227

Added line #L227 was not covered by tests

case 'doctor':
commandOptions = {};
break;

Check warning on line 231 in packages/aws-cdk/lib/convert-to-cli-args.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/convert-to-cli-args.ts#L230-L231

Added lines #L230 - L231 were not covered by tests
}
const cliArguments: CliArguments = {
_: args._,
globalOptions,
[args._[0]]: commandOptions,
};

return cliArguments;
}
5 changes: 3 additions & 2 deletions packages/aws-cdk/scripts/cli-args-gen.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as fs from 'fs';
// eslint-disable-next-line import/no-extraneous-dependencies
import { renderYargs, renderCliType } from '@aws-cdk/cli-args-gen';
import { renderYargs, renderCliArgsType, renderCliArgsFunc } from '@aws-cdk/cli-args-gen';
import { makeConfig, YARGS_HELPERS } from '../lib/config';

async function main() {
fs.writeFileSync('./lib/parse-command-line-arguments.ts', await renderYargs(await makeConfig(), YARGS_HELPERS));
fs.writeFileSync('./lib/cli-arguments.ts', await renderCliType(await makeConfig()));
fs.writeFileSync('./lib/cli-arguments.ts', await renderCliArgsType(await makeConfig()));
kaizencc marked this conversation as resolved.
Show resolved Hide resolved
fs.writeFileSync('./lib/convert-to-cli-args.ts', await renderCliArgsFunc(await makeConfig()));
}

main().then(() => {
Expand Down
113 changes: 100 additions & 13 deletions packages/aws-cdk/test/cli-arguments.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,106 @@
import { CliArguments } from '../lib/cli-arguments';
import { Command } from '../lib/settings';
import { convertToCliArgs } from '../lib/convert-to-cli-args';

// CliArguments is not being used right now, so the testing suite is rather redundant.
// This file is meant to be populated when CliArguments is used.
test('cli arguments can be used as a type', async () => {
const argv: CliArguments = {
_: [Command.DEPLOY],
test('yargs object can be converted to cli arguments', async () => {
// This input represents what would come out of yargs, i.e.
// await parseCommandLineArguments(args);
const input = {
kaizencc marked this conversation as resolved.
Show resolved Hide resolved
'_': ['deploy'],
'notification-arns': ['[]'],
'notificationArns': ['[]'],
'v': 1,
'verbose': 1,
'lookups': true,
'ignore-errors': false,
'ignoreErrors': false,
'json': false,
'j': false,
'debug': false,
'ec2creds': undefined,
'i': undefined,
'version-reporting': undefined,
'versionReporting': undefined,
'path-metadata': undefined,
'pathMetadata': undefined,
'asset-metadata': undefined,
'assetMetadata': undefined,
'role-arn': undefined,
'r': undefined,
'roleArn': undefined,
'staging': true,
'no-color': false,
'noColor': false,
'ci': false,
'unstable': [],
'all': false,
'build-exclude': [],
'E': [],
'buildExclude': [],
'force': false,
'f': false,
'parameters': [{}],
'previous-parameters': true,
'previousParameters': true,
'logs': true,
'concurrency': 1,
'asset-prebuild': true,
'assetPrebuild': true,
'ignore-no-stacks': false,
'ignoreNoStacks': false,
};

const result = convertToCliArgs(input);

expect(result).toEqual({
_: ['deploy'],
globalOptions: {
lookups: true,
app: undefined,
assetMetadata: undefined,
build: undefined,
caBundlePath: undefined,
context: undefined,
ignoreErrors: false,
noColor: false,
pathMetadata: undefined,
proxy: undefined,
roleArn: undefined,
staging: true,
strict: undefined,
verbose: 1,
versionReporting: undefined,
ci: false,
debug: false,
ec2creds: undefined,
json: false,
verbose: false,
lookups: true,
unstable: [],
notices: undefined,
output: undefined,
},
};

expect(argv._[0]).toBe('deploy');
expect(argv.globalOptions?.lookups).toBeTruthy();
deploy: {
all: false,
assetParallelism: undefined,
assetPrebuild: true,
buildExclude: [],
changeSetName: undefined,
concurrency: 1,
execute: undefined,
exclusively: undefined,
force: false,
hotswap: undefined,
hotswapFallback: undefined,
ignoreNoStacks: false,
logs: true,
method: undefined,
notificationArns: ['[]'],
outputsFile: undefined,
parameters: [{}],
previousParameters: true,
progress: undefined,
requireApproval: undefined,
rollback: undefined,
tags: undefined,
toolkitStackName: undefined,
watch: undefined,
},
});
});
7 changes: 5 additions & 2 deletions tools/@aws-cdk/cli-args-gen/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# cli-args-gen

Generates CDK CLI configurations from the source of truth in `packages/aws-cdk/lib/config.ts`.
Currently generates `yargs` config into `packages/aws-cdk/lib/parse-command-line-arguments.ts` and
strongly-typed CLI arguments interface into `packages/aws-cdk-lib/cli-arguments.ts`.
Currently generates the following files:

- `packages/aws-cdk/lib/parse-command-line-arguments.ts`: `yargs` config.
- `packages/aws-cdk-lib/cli-arguments.ts`: strongly typed `CliArguments` interface.
- `packages/aws-cdk-lib/convert-to-cli-args.ts`: converts the `any` returned by `yargs` to `CliArguments`.

## Usage

Expand Down
Loading
Loading