Skip to content

Commit

Permalink
chore(toolkit): use global cli io host for now
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Jan 15, 2025
1 parent 613a874 commit 03a106a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
7 changes: 5 additions & 2 deletions packages/@aws-cdk/toolkit/lib/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
}

async function main(command: string, args: any): Promise<number | void> {
CliIoHost.currentAction = command as any;
const toolkitStackName: string = ToolkitInfo.determineName(configuration.settings.get(['toolkitStackName']));
debug(`Toolkit stack: ${chalk.bold(toolkitStackName)}`);

Expand Down
6 changes: 3 additions & 3 deletions packages/aws-cdk/lib/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IoMessageLevel, IoMessage, CliIoHost, IoMessageSpecificCode, IoMessageC

// Corking mechanism
let CORK_COUNTER = 0;
const logBuffer: IoMessage[] = [];
const logBuffer: IoMessage<any>[] = [];

const levelPriority: Record<IoMessageLevel, number> = {
error: 0,
Expand Down Expand Up @@ -86,12 +86,12 @@ function log(options: LogOptions) {
return;
}

const ioMessage: IoMessage = {
const ioMessage: IoMessage<undefined> = {
level: options.level,
message: options.message,
forceStdout: options.forceStdout,
time: new Date(),
action: CliIoHost.currentAction ?? 'none',
action: CliIoHost.currentAction,
code: options.code,
};

Expand Down
43 changes: 37 additions & 6 deletions packages/aws-cdk/lib/toolkit/cli-io-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type IoMessageCode = IoMessageSpecificCode<IoCodeLevel>;
* Basic message structure for toolkit notifications.
* Messages are emitted by the toolkit and handled by the IoHost.
*/
export interface IoMessage {
export interface IoMessage<T> {
/**
* The time the message was emitted.
*/
Expand Down Expand Up @@ -55,14 +55,34 @@ export interface IoMessage {
* @default false
*/
readonly forceStdout?: boolean;

/**
* The data attached to the message.
*/
readonly data?: T;
}

export interface IoRequest<T, U> extends IoMessage<T> {
/**
* The default response that will be used if no data is returned.
*/
readonly defaultResponse: U;
}

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.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<void> {
async notify<T>(msg: IoMessage<T>): Promise<void> {
const output = this.formatMessage(msg);

const stream = CliIoHost.getStream(msg.level, msg.forceStdout ?? false);
Expand All @@ -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<T, U>(msg: IoRequest<T, U>): Promise<U> {
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<any>): string {
// 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
4 changes: 2 additions & 2 deletions packages/aws-cdk/test/toolkit/cli-io-host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>;

beforeEach(() => {
mockStdout = jest.fn();
Expand All @@ -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'),
Expand Down

0 comments on commit 03a106a

Please sign in to comment.