From 7cd84a0687dcfa4fa962883a5b14cb8c19df4d3f Mon Sep 17 00:00:00 2001 From: Romain Marcadier-Muller Date: Fri, 28 Dec 2018 16:12:07 +0100 Subject: [PATCH] feat(cli): Allow specifying options using env vars (#1447) 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`). --- packages/aws-cdk/bin/cdk.ts | 1 + packages/aws-cdk/lib/commands/doctor.ts | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk/bin/cdk.ts b/packages/aws-cdk/bin/cdk.ts index c91f10123ab8e..e505d6c6ef9d5 100644 --- a/packages/aws-cdk/bin/cdk.ts +++ b/packages/aws-cdk/bin/cdk.ts @@ -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 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' }) diff --git a/packages/aws-cdk/lib/commands/doctor.ts b/packages/aws-cdk/lib/commands/doctor.ts index 4fd301c2ff16e..8b19bb3be0cbc 100644 --- a/packages/aws-cdk/lib/commands/doctor.ts +++ b/packages/aws-cdk/lib/commands/doctor.ts @@ -1,3 +1,4 @@ +import cxapi = require('@aws-cdk/cx-api'); import colors = require('colors/safe'); import process = require('process'); import yargs = require('yargs'); @@ -25,7 +26,8 @@ export async function realHandler(_options: CommandOptions): Promise { const verifications: Array<() => boolean | Promise> = [ displayVersionInformation, - displayAwsEnvironmentVariables + displayAwsEnvironmentVariables, + displayCdkEnvironmentVariables, ]; // ### Verifications ### @@ -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; +}