Skip to content

Commit 2acab46

Browse files
committed
mrgrain's changes
1 parent b670116 commit 2acab46

File tree

8 files changed

+66
-46
lines changed

8 files changed

+66
-46
lines changed

packages/aws-cdk/lib/cli/cli-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export async function makeConfig(): Promise<CliConfig> {
4444
'ci': { type: 'boolean', desc: 'Force CI detection. If CI=true then logs will be sent to stdout instead of stderr', default: YARGS_HELPERS.isCI() },
4545
'unstable': { type: 'array', desc: 'Opt in to unstable features. The flag indicates that the scope and API of a feature might still change. Otherwise the feature is generally production ready and fully supported. Can be specified multiple times.', default: [] },
4646
'telemetry-file': { type: 'string', desc: 'Send telemetry data to a local file.', default: undefined },
47-
'yes': { type: 'boolean', alias: 'y', desc: 'Skip interactive prompts. If yes=true then the operation will proceed without confirmation.', default: false },
47+
'yes': { type: 'boolean', alias: 'y', desc: 'Automatically answer interactive prompts with the recommended response. This includes confirming actions.', default: false },
4848
},
4949
commands: {
5050
'list': {

packages/aws-cdk/lib/cli/cli-type-registry.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
"yes": {
132132
"type": "boolean",
133133
"alias": "y",
134-
"desc": "Skip interactive prompts. If yes=true then the operation will proceed without confirmation.",
134+
"desc": "Automatically answer interactive prompts with the recommended response. This includes confirming actions.",
135135
"default": false
136136
}
137137
},

packages/aws-cdk/lib/cli/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
7070
isCI: Boolean(argv.ci),
7171
currentAction: cmd,
7272
stackProgress: argv.progress,
73-
nonInteractive: argv.yes,
73+
autoRespond: argv.yes,
7474
}, true);
7575
const ioHelper = asIoHelper(ioHost, ioHost.currentAction as any);
7676

packages/aws-cdk/lib/cli/io-host/cli-io-host.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ export interface CliIoHostProps {
8383
readonly stackProgress?: StackActivityProgress;
8484

8585
/**
86-
* Whether the CLI is running in non-interactive mode.
87-
* When true, operation will proceed without confirmation.
86+
* Whether the CLI should attempt to automatically respond to prompts.
87+
*
88+
* When true, operation will usually proceed without interactive confirmation.
89+
* Confirmations are responded to with yes. Other prompts will respond with the default value.
8890
*
8991
* @default false
9092
*/
91-
readonly nonInteractive?: boolean;
93+
readonly autoRespond?: boolean;
9294
}
9395

9496
/**
@@ -168,7 +170,7 @@ export class CliIoHost implements IIoHost {
168170
private corkedCounter = 0;
169171
private readonly corkedLoggingBuffer: IoMessage<unknown>[] = [];
170172

171-
private readonly nonInteractive: boolean;
173+
private readonly autoRespond: boolean;
172174

173175
public telemetry?: TelemetrySession;
174176

@@ -180,7 +182,7 @@ export class CliIoHost implements IIoHost {
180182
this.requireDeployApproval = props.requireDeployApproval ?? RequireApproval.BROADENING;
181183

182184
this.stackProgress = props.stackProgress ?? StackActivityProgress.BAR;
183-
this.nonInteractive = props.nonInteractive ?? false;
185+
this.autoRespond = props.autoRespond ?? false;
184186
}
185187

186188
public async startTelemetry(args: any, context: Context, _proxyAgent?: Agent) {
@@ -424,6 +426,29 @@ export class CliIoHost implements IIoHost {
424426
const concurrency = data.concurrency ?? 0;
425427
const responseDescription = data.responseDescription;
426428

429+
// Special approval prompt
430+
// Determine if the message needs approval. If it does, continue (it is a basic confirmation prompt)
431+
// If it does not, return success (true). We only check messages with codes that we are aware
432+
// are requires approval codes.
433+
if (this.skipApprovalStep(msg)) {
434+
return true;
435+
}
436+
437+
// In --yes mode, respond for the user if we can
438+
if (this.autoRespond) {
439+
// respond with yes to all confirmations
440+
if (isConfirmationPrompt(msg)) {
441+
// @TODO print message and confirmation
442+
return true;
443+
}
444+
445+
// respond with the default for all other messages
446+
if (msg.defaultResponse) {
447+
// @TODO print message and response
448+
return msg.defaultResponse;
449+
}
450+
}
451+
427452
// only talk to user if STDIN is a terminal (otherwise, fail)
428453
if (!this.isTTY) {
429454
throw new ToolkitError(`${motivation}, but terminal (TTY) is not attached so we are unable to get a confirmation from the user`);
@@ -434,19 +459,6 @@ export class CliIoHost implements IIoHost {
434459
throw new ToolkitError(`${motivation}, but concurrency is greater than 1 so we are unable to get a confirmation from the user`);
435460
}
436461

437-
// In non-interactive mode, always return success (true).
438-
if (this.nonInteractive) {
439-
return true;
440-
}
441-
442-
// Special approval prompt
443-
// Determine if the message needs approval. If it does, continue (it is a basic confirmation prompt)
444-
// If it does not, return success (true). We only check messages with codes that we are aware
445-
// are requires approval codes.
446-
if (this.skipApprovalStep(msg)) {
447-
return true;
448-
}
449-
450462
// Basic confirmation prompt
451463
// We treat all requests with a boolean response as confirmation prompts
452464
if (isConfirmationPrompt(msg)) {

packages/aws-cdk/lib/cli/parse-command-line-arguments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export function parseCommandLineArguments(args: Array<string>): any {
165165
default: false,
166166
type: 'boolean',
167167
alias: 'y',
168-
desc: 'Skip interactive prompts. If yes=true then the operation will proceed without confirmation.',
168+
desc: 'Automatically answer interactive prompts with the recommended response. This includes confirming actions.',
169169
})
170170
.command(['list [STACKS..]', 'ls [STACKS..]'], 'Lists all stacks in the app', (yargs: Argv) =>
171171
yargs

packages/aws-cdk/lib/cli/user-input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export interface GlobalOptions {
329329
readonly telemetryFile?: string;
330330

331331
/**
332-
* Skip interactive prompts. If yes=true then the operation will proceed without confirmation.
332+
* Automatically answer interactive prompts with the recommended response. This includes confirming actions.
333333
*
334334
* @default - false
335335
*/

packages/aws-cdk/test/cli/cli.test.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ jest.mock('../../lib/cli/parse-command-line-arguments', () => ({
5959
if (args.includes('--role-arn')) {
6060
result = { ...result, roleArn: 'arn:aws:iam::123456789012:role/TestRole' };
6161
}
62+
} else if (args.includes('deploy')) {
63+
result = {
64+
...result,
65+
_: ['deploy'],
66+
parameters: [],
67+
};
6268
}
6369

6470
// Handle notices flags
@@ -486,22 +492,24 @@ describe('gc command tests', () => {
486492
});
487493
});
488494

489-
test('when --yes option is provided, CliIoHost is in non-interactive mode', async () => {
490-
// GIVEN
491-
const migrateSpy = jest.spyOn(cdkToolkitModule.CdkToolkit.prototype, 'deploy').mockResolvedValue();
492-
const execSpy = jest.spyOn(CliIoHost, 'instance');
495+
describe('--yes', () => {
496+
test('when --yes option is provided, CliIoHost is using autoRespond', async () => {
497+
// GIVEN
498+
const migrateSpy = jest.spyOn(cdkToolkitModule.CdkToolkit.prototype, 'deploy').mockResolvedValue();
499+
const execSpy = jest.spyOn(CliIoHost, 'instance');
493500

494-
// WHEN
495-
await exec(['deploy', '--yes']);
501+
// WHEN
502+
await exec(['deploy', '--yes']);
496503

497-
// THEN
498-
expect(execSpy).toHaveBeenCalledWith(
499-
expect.objectContaining({
500-
nonInteractive: true,
501-
}),
502-
true,
503-
);
504+
// THEN
505+
expect(execSpy).toHaveBeenCalledWith(
506+
expect.objectContaining({
507+
autoRespond: true,
508+
}),
509+
true,
510+
);
504511

505-
migrateSpy.mockRestore();
506-
execSpy.mockRestore();
512+
migrateSpy.mockRestore();
513+
execSpy.mockRestore();
514+
});
507515
});

packages/aws-cdk/test/cli/io-host/cli-io-host.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,17 +494,17 @@ describe('CliIoHost', () => {
494494
});
495495
});
496496

497-
describe('non-interactive mode', () => {
498-
const nonInteractiveIoHost = CliIoHost.instance({
497+
describe('--yes mode', () => {
498+
const autoRespondingIoHost = CliIoHost.instance({
499499
logLevel: 'trace',
500-
nonInteractive: true,
500+
autoRespond: true,
501501
isCI: false,
502502
isTTY: true,
503503
}, true);
504504

505505
test('it does not prompt the user and return true', async () => {
506506
// WHEN
507-
const response = await nonInteractiveIoHost.requestResponse(plainMessage({
507+
const response = await autoRespondingIoHost.requestResponse(plainMessage({
508508
time: new Date(),
509509
level: 'info',
510510
action: 'synth',
@@ -518,20 +518,20 @@ describe('CliIoHost', () => {
518518
expect(response).toBe(true);
519519
});
520520

521-
test('approvalToolkitCodes also skip', async () => {
521+
test('messages with default are skipped', async () => {
522522
// WHEN
523-
const response = await nonInteractiveIoHost.requestResponse(plainMessage({
523+
const response = await autoRespondingIoHost.requestResponse(plainMessage({
524524
time: new Date(),
525525
level: 'info',
526526
action: 'synth',
527527
code: 'CDK_TOOLKIT_I5060',
528528
message: 'test message',
529-
defaultResponse: true,
529+
defaultResponse: 'foobar',
530530
}));
531531

532532
// THEN
533533
expect(mockStdout).not.toHaveBeenCalledWith(chalk.cyan('test message') + ' (y/n) ');
534-
expect(response).toBe(true);
534+
expect(response).toBe('foobar');
535535
});
536536
});
537537

0 commit comments

Comments
 (0)