From 03a106a3107b318ede91bbb791fc8a64720e6a12 Mon Sep 17 00:00:00 2001 From: Momo Kornher Date: Wed, 15 Jan 2025 00:47:24 +0000 Subject: [PATCH] chore(toolkit): use global cli io host for now --- packages/@aws-cdk/toolkit/lib/toolkit.ts | 7 ++- packages/aws-cdk/lib/cli.ts | 1 + packages/aws-cdk/lib/logging.ts | 6 +-- packages/aws-cdk/lib/toolkit/cli-io-host.ts | 43 ++++++++++++++++--- .../aws-cdk/test/toolkit/cli-io-host.test.ts | 4 +- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/packages/@aws-cdk/toolkit/lib/toolkit.ts b/packages/@aws-cdk/toolkit/lib/toolkit.ts index 61d1ad38d95c9..55bd5b0f708e2 100644 --- a/packages/@aws-cdk/toolkit/lib/toolkit.ts +++ b/packages/@aws-cdk/toolkit/lib/toolkit.ts @@ -9,6 +9,7 @@ import { StackActivityProgress } from 'aws-cdk/lib/api/util/cloudformation/stack import { ResourceMigrator } from 'aws-cdk/lib/migrator'; import { serializeStructure } from 'aws-cdk/lib/serialize'; import { tagsForStack } from 'aws-cdk/lib/tags'; +import { CliIoHost } from 'aws-cdk/lib/toolkit/cli-io-host'; import { validateSnsTopicArn } from 'aws-cdk/lib/util/validate-notification-arn'; import { Concurrency } from 'aws-cdk/lib/util/work-graph'; import { WorkGraphBuilder } from 'aws-cdk/lib/util/work-graph-builder'; @@ -37,7 +38,7 @@ export interface ToolkitOptions { /** * The IoHost implementation, handling the inline interactions between the Toolkit and an integration. */ - ioHost: IIoHost; + // ioHost: IIoHost; /** * Configuration options for the SDK. @@ -61,7 +62,9 @@ export class Toolkit { private toolkitStackName: string; public constructor(private readonly options: ToolkitOptions) { - this.ioHost = options.ioHost; + this.ioHost = CliIoHost.getIoHost(); + // this.ioHost = options.ioHost; + // @todo open this up this.toolkitStackName = options.toolkitStackName ?? DEFAULT_TOOLKIT_STACK_NAME; } diff --git a/packages/aws-cdk/lib/cli.ts b/packages/aws-cdk/lib/cli.ts index 75ab612a8d828..b1a40738fc819 100644 --- a/packages/aws-cdk/lib/cli.ts +++ b/packages/aws-cdk/lib/cli.ts @@ -175,6 +175,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise { + CliIoHost.currentAction = command as any; const toolkitStackName: string = ToolkitInfo.determineName(configuration.settings.get(['toolkitStackName'])); debug(`Toolkit stack: ${chalk.bold(toolkitStackName)}`); diff --git a/packages/aws-cdk/lib/logging.ts b/packages/aws-cdk/lib/logging.ts index 2df85c308708b..9262e94ea6f4a 100644 --- a/packages/aws-cdk/lib/logging.ts +++ b/packages/aws-cdk/lib/logging.ts @@ -4,7 +4,7 @@ import { IoMessageLevel, IoMessage, CliIoHost, IoMessageSpecificCode, IoMessageC // Corking mechanism let CORK_COUNTER = 0; -const logBuffer: IoMessage[] = []; +const logBuffer: IoMessage[] = []; const levelPriority: Record = { error: 0, @@ -86,12 +86,12 @@ function log(options: LogOptions) { return; } - const ioMessage: IoMessage = { + const ioMessage: IoMessage = { level: options.level, message: options.message, forceStdout: options.forceStdout, time: new Date(), - action: CliIoHost.currentAction ?? 'none', + action: CliIoHost.currentAction, code: options.code, }; diff --git a/packages/aws-cdk/lib/toolkit/cli-io-host.ts b/packages/aws-cdk/lib/toolkit/cli-io-host.ts index 41038734d9c25..d55c394a9efc6 100644 --- a/packages/aws-cdk/lib/toolkit/cli-io-host.ts +++ b/packages/aws-cdk/lib/toolkit/cli-io-host.ts @@ -9,7 +9,7 @@ export type IoMessageCode = IoMessageSpecificCode; * Basic message structure for toolkit notifications. * Messages are emitted by the toolkit and handled by the IoHost. */ -export interface IoMessage { +export interface IoMessage { /** * The time the message was emitted. */ @@ -55,6 +55,18 @@ export interface IoMessage { * @default false */ readonly forceStdout?: boolean; + + /** + * The data attached to the message. + */ + readonly data?: T; +} + +export interface IoRequest extends IoMessage { + /** + * The default response that will be used if no data is returned. + */ + readonly defaultResponse: U; } export type IoMessageLevel = 'error' | 'warn' | 'info' | 'debug' | 'trace'; @@ -62,7 +74,15 @@ export type IoMessageLevel = 'error' | 'warn' | 'info' | 'debug' | 'trace'; /** * The current action being performed by the CLI. 'none' represents the absence of an action. */ -export type ToolkitAction = 'synth' | 'list' | 'deploy' | 'destroy' | 'none'; +export type ToolkitAction = +| 'bootstrap' +| 'synth' +| 'list' +| 'diff' +| 'deploy' +| 'rollback' +| 'watch' +| 'destroy'; /** * A simple IO host for the CLI that writes messages to the console. @@ -116,14 +136,14 @@ export class CliIoHost { /** * the current {@link ToolkitAction} set by the CLI. */ - private currentAction: ToolkitAction | undefined; + private currentAction: ToolkitAction = 'synth'; private constructor() { this.isTTY = process.stdout.isTTY ?? false; this.ci = false; } - public static get currentAction(): ToolkitAction | undefined { + public static get currentAction(): ToolkitAction { return CliIoHost.getIoHost().currentAction; } @@ -151,7 +171,7 @@ export class CliIoHost { * Notifies the host of a message. * The caller waits until the notification completes. */ - async notify(msg: IoMessage): Promise { + async notify(msg: IoMessage): Promise { const output = this.formatMessage(msg); const stream = CliIoHost.getStream(msg.level, msg.forceStdout ?? false); @@ -167,10 +187,21 @@ export class CliIoHost { }); } + /** + * Notifies the host of a message that requires a response. + * + * If the host does not return a response the suggested + * default response from the input message will be used. + */ + async requestResponse(msg: IoRequest): Promise { + await this.notify(msg); + return msg.defaultResponse; + } + /** * Formats a message for console output with optional color support */ - private formatMessage(msg: IoMessage): string { + private formatMessage(msg: IoMessage): string { // apply provided style or a default style if we're in TTY mode let message_text = this.isTTY ? styleMap[msg.level](msg.message) diff --git a/packages/aws-cdk/test/toolkit/cli-io-host.test.ts b/packages/aws-cdk/test/toolkit/cli-io-host.test.ts index 7ea32efcf6c2e..910f2ad19df81 100644 --- a/packages/aws-cdk/test/toolkit/cli-io-host.test.ts +++ b/packages/aws-cdk/test/toolkit/cli-io-host.test.ts @@ -4,7 +4,7 @@ import { CliIoHost, IoMessage } from '../../lib/toolkit/cli-io-host'; describe('CliIoHost', () => { let mockStdout: jest.Mock; let mockStderr: jest.Mock; - let defaultMessage: IoMessage; + let defaultMessage: IoMessage; beforeEach(() => { mockStdout = jest.fn(); @@ -13,7 +13,7 @@ describe('CliIoHost', () => { // Reset singleton state CliIoHost.isTTY = process.stdout.isTTY ?? false; CliIoHost.ci = false; - CliIoHost.currentAction = 'none'; + CliIoHost.currentAction = 'synth'; defaultMessage = { time: new Date('2024-01-01T12:00:00'),