Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export async function makeConfig(): Promise<CliConfig> {
'lookups': { type: 'boolean', desc: 'Perform context lookups (synthesis fails if this is disabled and context lookups need to be performed)', default: true },
'ignore-errors': { type: 'boolean', default: false, desc: 'Ignores synthesis errors, which will likely produce an invalid output' },
'json': { type: 'boolean', alias: 'j', desc: 'Use JSON output instead of YAML when templates are printed to STDOUT', default: false },
'jsonlog': { type: 'boolean', alias: 'J', desc: 'Print log output in JSON-lines format to stderr', default: false },
'verbose': { type: 'boolean', alias: 'v', desc: 'Show debug logs (specify multiple times to increase verbosity)', default: false, count: true },
'debug': { type: 'boolean', desc: 'Debug the CDK app. Log additional information during synthesis, such as creation stack traces of tokens (sets CDK_DEBUG, will slow down synthesis)', default: false },
'profile': { type: 'string', desc: 'Use the indicated AWS profile as the default environment', requiresArg: true },
Expand Down
6 changes: 6 additions & 0 deletions packages/aws-cdk/lib/cli/cli-type-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
"desc": "Use JSON output instead of YAML when templates are printed to STDOUT",
"default": false
},
"jsonlog": {
"type": "boolean",
"alias": "J",
"desc": "Print log output in JSON-lines format to stderr",
"default": false
},
"verbose": {
"type": "boolean",
"alias": "v",
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
currentAction: cmd,
stackProgress: argv.progress,
autoRespond: argv.yes,
printAsJson: argv.jsonlog,
}, true);
const ioHelper = asIoHelper(ioHost, ioHost.currentAction as any);

Expand Down
2 changes: 2 additions & 0 deletions packages/aws-cdk/lib/cli/convert-to-user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function convertYargsToUserInput(args: any): UserInput {
lookups: args.lookups,
ignoreErrors: args.ignoreErrors,
json: args.json,
jsonlog: args.jsonlog,
verbose: args.verbose,
debug: args.debug,
profile: args.profile,
Expand Down Expand Up @@ -352,6 +353,7 @@ export function convertConfigToUserInput(config: any): UserInput {
lookups: config.lookups,
ignoreErrors: config.ignoreErrors,
json: config.json,
jsonlog: config.jsonlog,
verbose: config.verbose,
debug: config.debug,
profile: config.profile,
Expand Down
27 changes: 25 additions & 2 deletions packages/aws-cdk/lib/cli/io-host/cli-io-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ export interface CliIoHostProps {
* @default false
*/
readonly autoRespond?: boolean;

/**
* Print output as JSON instead of as human-readable text
*
* @default false
*/
readonly printAsJson?: boolean;
}

/**
Expand Down Expand Up @@ -176,7 +183,7 @@ export class CliIoHost implements IIoHost {
private readonly corkedLoggingBuffer: IoMessage<unknown>[] = [];

private readonly autoRespond: boolean;

private readonly printAsJson: boolean;
public telemetry?: TelemetrySession;

private constructor(props: CliIoHostProps = {}) {
Expand All @@ -187,6 +194,7 @@ export class CliIoHost implements IIoHost {
this.requireDeployApproval = props.requireDeployApproval ?? RequireApproval.BROADENING;
this.stackProgress = props.stackProgress ?? StackActivityProgress.BAR;
this.autoRespond = props.autoRespond ?? false;
this.printAsJson = props.printAsJson ?? false;
}

public async startTelemetry(args: any, context: Context, proxyAgent?: Agent) {
Expand Down Expand Up @@ -254,14 +262,20 @@ export class CliIoHost implements IIoHost {
* Gets the stackProgress value.
*
* This takes into account other state of the ioHost,
* like if isTTY and isCI.
* like `isTTY`, `isCI` and `printAsJson`.
*/
public get stackProgress(): StackActivityProgress {
// We can always use EVENTS
if (this._progress === StackActivityProgress.EVENTS) {
return this._progress;
}

// If we're doing machine-readable output, we're not going to do
// live-updating ANSI characters.
if (this.printAsJson) {
return StackActivityProgress.EVENTS;
}

// if a debug message (and thus any more verbose messages) are relevant to the current log level, we have verbose logging
const verboseLogging = isMessageRelevantForLevel({ level: 'debug' }, this.logLevel);
if (verboseLogging) {
Expand Down Expand Up @@ -397,6 +411,11 @@ export class CliIoHost implements IIoHost {
* Determines the output stream, based on message and configuration.
*/
private selectStream(msg: IoMessage<any>): NodeJS.WriteStream | undefined {
if (this.printAsJson) {
// JSON output always goes to stderr
return process.stderr;
}

if (isNoticesMessage(msg)) {
return targetStreamObject(this.noticesDestination);
}
Expand Down Expand Up @@ -520,6 +539,10 @@ export class CliIoHost implements IIoHost {
* Formats a message for console output with optional color support
*/
private formatMessage(msg: IoMessage<unknown>): string {
if (this.printAsJson) {
return `${JSON.stringify(msg)}\n`;
}

// apply provided style or a default style if we're in TTY mode
let message_text = this.isTTY
? styleMap[msg.level](msg.message)
Expand Down
6 changes: 6 additions & 0 deletions packages/aws-cdk/lib/cli/parse-command-line-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export function parseCommandLineArguments(args: Array<string>): any {
alias: 'j',
desc: 'Use JSON output instead of YAML when templates are printed to STDOUT',
})
.option('jsonlog', {
default: false,
type: 'boolean',
alias: 'J',
desc: 'Print log output in JSON-lines format to stderr',
})
.option('verbose', {
default: false,
type: 'boolean',
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk/lib/cli/user-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ export interface GlobalOptions {
*/
readonly json?: boolean;

/**
* Print log output in JSON-lines format to stderr
*
* @default - false
*/
readonly jsonlog?: boolean;

/**
* Show debug logs (specify multiple times to increase verbosity)
*
Expand Down
6 changes: 6 additions & 0 deletions packages/aws-cdk/test/cli/cli-arguments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ describe('yargs', () => {
roleArn: undefined,
staging: true,
strict: undefined,
telemetryFile: undefined,
verbose: 1,
versionReporting: undefined,
ci: true,
color: undefined,
debug: false,
ec2creds: undefined,
json: false,
jsonlog: false,
lookups: true,
trace: undefined,
unstable: [],
Expand All @@ -50,6 +53,9 @@ describe('yargs', () => {
exclusively: undefined,
force: false,
hotswap: undefined,
hotswapEcsMaximumHealthyPercent: undefined,
hotswapEcsMinimumHealthyPercent: undefined,
hotswapEcsStabilizationTimeoutSeconds: undefined,
hotswapFallback: undefined,
ignoreNoStacks: false,
importExistingResources: false,
Expand Down
Loading