Skip to content

Commit b2ced3d

Browse files
authored
fix(cli): stack filter positional arg is not being respected (#846)
At some point, the positional argument was lost, and there were no tests for it. Add it to `cli-config.ts`. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent b45b1ab commit b2ced3d

File tree

8 files changed

+80
-1
lines changed

8 files changed

+80
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"app": "node refactoring.js",
3+
"versionReporting": false,
4+
"context": {
5+
"aws-cdk:enableDiffNoFail": "true"
6+
}
7+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const cdk = require('aws-cdk-lib');
2+
const s3 = require('aws-cdk-lib/aws-s3');
3+
4+
const BUCKET_ID = process.env.BUCKET_ID ?? 'OldName';
5+
const stackPrefix = process.env.STACK_NAME_PREFIX;
6+
7+
const app = new cdk.App();
8+
9+
let gamma = {
10+
region: 'eu-central-1',
11+
};
12+
let prod = {
13+
region: 'us-east-1',
14+
};
15+
16+
class MyStack extends cdk.Stack {
17+
constructor(scope, id) {
18+
super(scope, id);
19+
new s3.Bucket(this, BUCKET_ID);
20+
}
21+
}
22+
23+
new MyStack(app, `${stackPrefix}-gamma-stack`, { env: gamma });
24+
new MyStack(app, `${stackPrefix}-prod-stack`, { env: prod });
25+
26+
app.synth();

packages/@aws-cdk-testing/cli-integ/tests/cli-integ-tests/refactor/cdk-refactor-dry-run.integtest.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,38 @@ integTest(
4949
}),
5050
);
5151

52+
integTest(
53+
'cdk refactor - filters stacks by pattern',
54+
withSpecificFixture('refactoring-multiple-envs', async (fixture) => {
55+
// First, deploy the stacks
56+
await fixture.cdkDeploy('gamma-stack', {
57+
modEnv: {
58+
BUCKET_ID: 'OldName',
59+
},
60+
});
61+
await fixture.cdkDeploy('prod-stack', {
62+
modEnv: {
63+
BUCKET_ID: 'OldName',
64+
},
65+
});
66+
67+
// Then see if the refactoring tool detects the change
68+
const stdErr = await fixture.cdkRefactor({
69+
options: ['*-gamma-stack', '--dry-run', '--unstable=refactor'],
70+
allowErrExit: true,
71+
captureStderr: true,
72+
// Making sure the synthesized stack has a queue with
73+
// the new name so that a refactor is detected
74+
modEnv: {
75+
BUCKET_ID: 'NewName',
76+
},
77+
});
78+
79+
const numberOfEnvironments = (stdErr.match(/Resource Type/g) || []).length;
80+
expect(numberOfEnvironments).toEqual(1);
81+
}),
82+
);
83+
5284
function removeColor(str: string): string {
5385
return str.replace(/\x1B[[(?);]{0,2}(;?\d)*./g, '');
5486
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,10 @@ export async function makeConfig(): Promise<CliConfig> {
493493
desc: 'Whether to do the refactor without asking for confirmation',
494494
},
495495
},
496+
arg: {
497+
name: 'STACKS',
498+
variadic: true,
499+
},
496500
},
497501
'cli-telemetry': {
498502
description: 'Enable or disable anonymous telemetry',

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,10 @@
10151015
"default": false,
10161016
"desc": "Whether to do the refactor without asking for confirmation"
10171017
}
1018+
},
1019+
"arg": {
1020+
"name": "STACKS",
1021+
"variadic": true
10181022
}
10191023
},
10201024
"cli-telemetry": {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ export function convertYargsToUserInput(args: any): UserInput {
292292
overrideFile: args.overrideFile,
293293
revert: args.revert,
294294
force: args.force,
295+
STACKS: args.STACKS,
295296
};
296297
break;
297298

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ export function parseCommandLineArguments(args: Array<string>): any {
966966
}),
967967
)
968968
.command('doctor', 'Check your set-up for potential problems')
969-
.command('refactor', 'Moves resources between stacks or within the same stack', (yargs: Argv) =>
969+
.command('refactor [STACKS..]', 'Moves resources between stacks or within the same stack', (yargs: Argv) =>
970970
yargs
971971
.option('additional-stack-name', {
972972
type: 'array',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,11 @@ export interface RefactorOptions {
15691569
* @default - false
15701570
*/
15711571
readonly force?: boolean;
1572+
1573+
/**
1574+
* Positional argument for refactor
1575+
*/
1576+
readonly STACKS?: Array<string>;
15721577
}
15731578

15741579
/**

0 commit comments

Comments
 (0)