Skip to content

Commit

Permalink
feat(cli): Allow specifying options using env vars (#1447)
Browse files Browse the repository at this point in the history
Allows passing options to the `cdk` command using environment variables
with the `CDK_` name prefix. This can be used, for example, for the
following:

- `CDK_VERSION_REPORTING=false` disables version reporting
- `CDK_REQUIRE_APPROVAL=broadening` requires approval for changes to
  security policies that broaden the grants
- `CDK_VERBOSE=true` enables verbose logging in the toolkit

Added a validator in `cdk doctor` that will display the environment
variables that may be picked up by the toolkit, and highlight those that
are reserved for internal use by the toolkit (`CDK_CONTEXT_JSON` and
`CDK_OUTDIR`).
  • Loading branch information
RomainMuller committed Dec 28, 2018
1 parent 8c17ca7 commit 7cd84a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/aws-cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const DEFAULT_TOOLKIT_STACK_NAME = 'CDKToolkit';
async function parseCommandLineArguments() {
const initTemplateLanuages = await availableInitLanguages;
return yargs
.env('CDK')
.usage('Usage: cdk -a <cdk-app> COMMAND')
.option('app', { type: 'string', alias: 'a', desc: 'REQUIRED: Command-line for executing your CDK app (e.g. "node bin/my-app.js")' })
.option('context', { type: 'array', alias: 'c', desc: 'Add contextual string parameter.', nargs: 1, requiresArg: 'KEY=VALUE' })
Expand Down
23 changes: 22 additions & 1 deletion packages/aws-cdk/lib/commands/doctor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import cxapi = require('@aws-cdk/cx-api');
import colors = require('colors/safe');
import process = require('process');
import yargs = require('yargs');
Expand Down Expand Up @@ -25,7 +26,8 @@ export async function realHandler(_options: CommandOptions): Promise<number> {

const verifications: Array<() => boolean | Promise<boolean>> = [
displayVersionInformation,
displayAwsEnvironmentVariables
displayAwsEnvironmentVariables,
displayCdkEnvironmentVariables,
];

// ### Verifications ###
Expand All @@ -47,3 +49,22 @@ function displayAwsEnvironmentVariables() {
}
return true;
}

function displayCdkEnvironmentVariables() {
const keys = Object.keys(process.env).filter(s => s.startsWith('CDK_'));
if (keys.length === 0) {
print('ℹ️ No CDK environment variables');
return true;
}
print('ℹ️ CDK environment variables:');
let healthy = true;
for (const key of keys.sort()) {
if (key === cxapi.CONTEXT_ENV || key === cxapi.OUTDIR_ENV) {
print(` - ${colors.red(key)} = ${colors.green(process.env[key]!)} (⚠️ reserved for use by the CDK toolkit)`);
healthy = false;
} else {
print(` - ${colors.blue(key)} = ${colors.green(process.env[key]!)}`);
}
}
return healthy;
}

0 comments on commit 7cd84a0

Please sign in to comment.