Skip to content

Commit b73e3d9

Browse files
committed
refactor(cli): use IoHelper in internal APIs
1 parent db6aa96 commit b73e3d9

32 files changed

+381
-455
lines changed

packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/context-aware-source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class ContextAwareCloudAssembly implements ICloudAssemblySource {
4949
this.canLookup = props.lookups ?? true;
5050
this.context = props.context;
5151
this.contextFile = props.contextFile ?? PROJECT_CONTEXT; // @todo new feature not needed right now
52-
this.ioHost = props.services.ioHost;
52+
this.ioHost = props.services.ioHelper;
5353
}
5454

5555
/**

packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/prepare-source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function determineOutputDirectory(outdir?: string) {
3838
* @param context The context key/value bash.
3939
*/
4040
export async function prepareDefaultEnvironment(services: ToolkitServices, props: { outdir?: string } = {}): Promise<Env> {
41-
const logFn = (msg: string, ...args: any) => services.ioHost.notify(IO.CDK_ASSEMBLY_I0010.msg(format(msg, ...args)));
41+
const logFn = (msg: string, ...args: any) => services.ioHelper.notify(IO.CDK_ASSEMBLY_I0010.msg(format(msg, ...args)));
4242
const env = await oldPrepare(services.sdkProvider, logFn);
4343

4444
if (props.outdir) {

packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/source-builder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export abstract class CloudAssemblySourceBuilder {
6565
return assembly;
6666
}
6767

68-
return assemblyFromDirectory(assembly.directory, services.ioHost, props.loadAssemblyOptions);
68+
return assemblyFromDirectory(assembly.directory, services.ioHelper, props.loadAssemblyOptions);
6969
},
7070
},
7171
contextAssemblyProps,
@@ -89,8 +89,8 @@ export abstract class CloudAssemblySourceBuilder {
8989
{
9090
produce: async () => {
9191
// @todo build
92-
await services.ioHost.notify(IO.CDK_ASSEMBLY_I0150.msg('--app points to a cloud assembly, so we bypass synth'));
93-
return assemblyFromDirectory(directory, services.ioHost, props.loadAssemblyOptions);
92+
await services.ioHelper.notify(IO.CDK_ASSEMBLY_I0150.msg('--app points to a cloud assembly, so we bypass synth'));
93+
return assemblyFromDirectory(directory, services.ioHelper, props.loadAssemblyOptions);
9494
},
9595
},
9696
contextAssemblyProps,
@@ -139,17 +139,17 @@ export abstract class CloudAssemblySourceBuilder {
139139
eventPublisher: async (type, line) => {
140140
switch (type) {
141141
case 'data_stdout':
142-
await services.ioHost.notify(IO.CDK_ASSEMBLY_I1001.msg(line));
142+
await services.ioHelper.notify(IO.CDK_ASSEMBLY_I1001.msg(line));
143143
break;
144144
case 'data_stderr':
145-
await services.ioHost.notify(IO.CDK_ASSEMBLY_E1002.msg(line));
145+
await services.ioHelper.notify(IO.CDK_ASSEMBLY_E1002.msg(line));
146146
break;
147147
}
148148
},
149149
extraEnv: envWithContext,
150150
cwd: props.workingDirectory,
151151
});
152-
return assemblyFromDirectory(outdir, services.ioHost, props.loadAssemblyOptions);
152+
return assemblyFromDirectory(outdir, services.ioHelper, props.loadAssemblyOptions);
153153
});
154154
} finally {
155155
await lock?.release();

packages/@aws-cdk/toolkit-lib/lib/toolkit/private/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { IoHelper } from '../../api/shared-private';
99
*/
1010
export interface ToolkitServices {
1111
sdkProvider: SdkProvider;
12-
ioHost: IoHelper;
12+
ioHelper: IoHelper;
1313
}
1414

1515
/**

packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts

Lines changed: 63 additions & 64 deletions
Large diffs are not rendered by default.

packages/aws-cdk/lib/api/aws-auth/sdk-logger.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import { inspect, format } from 'util';
22
import { Logger } from '@smithy/types';
3-
import type { IIoHost } from '../../toolkit/cli-io-host';
3+
import { IoHelper } from '../../../../@aws-cdk/tmp-toolkit-helpers/src/api/io/private';
44
import { replacerBufferWithInfo } from '../../util';
55

66
export class SdkToCliLogger implements Logger {
7-
private readonly ioHost: IIoHost;
7+
private readonly ioHelper: IoHelper;
88

9-
public constructor(ioHost: IIoHost) {
10-
this.ioHost = ioHost;
9+
public constructor(ioHelper: IoHelper) {
10+
this.ioHelper = ioHelper;
1111
}
1212

1313
private notify(level: 'debug' | 'info' | 'warn' | 'error', ...content: any[]) {
14-
void this.ioHost.notify({
14+
void this.ioHelper.notify({
1515
time: new Date(),
1616
level: 'trace', // always log all SDK logs at trace level, no matter what level they are coming from the SDK
17-
action: 'none' as any,
1817
code: 'CDK_SDK_I0000',
1918
message: format('[SDK %s] %s', level, formatSdkLoggerContent(content)),
2019
});

packages/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import * as cxapi from '@aws-cdk/cx-api';
44
import type { BootstrapEnvironmentOptions, BootstrappingParameters } from './bootstrap-props';
55
import { BootstrapStack, bootstrapVersionFromTemplate } from './deploy-bootstrap';
66
import { legacyBootstrapTemplate } from './legacy-template';
7+
import { IoHelper } from '../../../../@aws-cdk/tmp-toolkit-helpers/src/api/io/private';
78
import { warn } from '../../cli/messages';
8-
import { IoMessaging } from '../../toolkit/cli-io-host';
99
import { ToolkitError } from '../../toolkit/error';
1010
import { bundledPackageRootDir, loadStructuredFile, serializeStructure } from '../../util';
1111
import type { SDK, SdkProvider } from '../aws-auth';
@@ -16,10 +16,13 @@ import { DEFAULT_TOOLKIT_STACK_NAME } from '../toolkit-info';
1616
export type BootstrapSource = { source: 'legacy' } | { source: 'default' } | { source: 'custom'; templateFile: string };
1717

1818
export class Bootstrapper {
19+
private readonly ioHelper: IoHelper;
20+
1921
constructor(
2022
private readonly source: BootstrapSource = { source: 'default' },
21-
private readonly msg: IoMessaging,
23+
ioHelper: IoHelper,
2224
) {
25+
this.ioHelper = ioHelper;
2326
}
2427

2528
public bootstrapEnvironment(
@@ -67,7 +70,7 @@ export class Bootstrapper {
6770
}
6871

6972
const toolkitStackName = options.toolkitStackName ?? DEFAULT_TOOLKIT_STACK_NAME;
70-
const current = await BootstrapStack.lookup(sdkProvider, environment, toolkitStackName, this.msg);
73+
const current = await BootstrapStack.lookup(sdkProvider, environment, toolkitStackName, this.ioHelper);
7174
return current.update(
7275
await this.loadTemplate(params),
7376
{},
@@ -92,7 +95,7 @@ export class Bootstrapper {
9295
const bootstrapTemplate = await this.loadTemplate();
9396

9497
const toolkitStackName = options.toolkitStackName ?? DEFAULT_TOOLKIT_STACK_NAME;
95-
const current = await BootstrapStack.lookup(sdkProvider, environment, toolkitStackName, this.msg);
98+
const current = await BootstrapStack.lookup(sdkProvider, environment, toolkitStackName, this.ioHelper);
9699
const partition = await current.partition();
97100

98101
if (params.createCustomerMasterKey !== undefined && params.kmsKeyId) {
@@ -148,8 +151,7 @@ export class Bootstrapper {
148151
// Would leave AdministratorAccess policies with a trust relationship, without the user explicitly
149152
// approving the trust policy.
150153
const implicitPolicy = `arn:${partition}:iam::aws:policy/AdministratorAccess`;
151-
await this.msg.ioHost.notify(warn(
152-
this.msg.action,
154+
await this.ioHelper.notify(warn(
153155
`Using default execution policy of '${implicitPolicy}'. Pass '--cloudformation-execution-policies' to customize.`,
154156
));
155157
} else if (cloudFormationExecutionPolicies.length === 0) {
@@ -197,18 +199,15 @@ export class Bootstrapper {
197199
}
198200
if (currentPermissionsBoundary !== policyName) {
199201
if (!currentPermissionsBoundary) {
200-
await this.msg.ioHost.notify(warn(
201-
this.msg.action,
202+
await this.ioHelper.notify(warn(
202203
`Adding new permissions boundary ${policyName}`,
203204
));
204205
} else if (!policyName) {
205-
await this.msg.ioHost.notify(warn(
206-
this.msg.action,
206+
await this.ioHelper.notify(warn(
207207
`Removing existing permissions boundary ${currentPermissionsBoundary}`,
208208
));
209209
} else {
210-
await this.msg.ioHost.notify(warn(
211-
this.msg.action,
210+
await this.ioHelper.notify(warn(
212211
`Changing permissions boundary from ${currentPermissionsBoundary} to ${policyName}`,
213212
));
214213
}

packages/aws-cdk/lib/api/bootstrap/deploy-bootstrap.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
BootstrapEnvironmentOptions,
1111
DEFAULT_BOOTSTRAP_VARIANT,
1212
} from './bootstrap-props';
13+
import { IoHelper } from '../../../../@aws-cdk/tmp-toolkit-helpers/src/api/io/private';
1314
import { warn } from '../../cli/messages';
14-
import { IoMessaging } from '../../toolkit/cli-io-host';
1515
import type { SDK, SdkProvider } from '../aws-auth';
1616
import { assertIsSuccessfulDeployStackResult, SuccessfulDeployStackResult } from '../deployments';
1717
import { deployStack } from '../deployments/deploy-stack';
@@ -34,15 +34,15 @@ import { DEFAULT_TOOLKIT_STACK_NAME, ToolkitInfo } from '../toolkit-info';
3434
* current bootstrap stack and doing something intelligent).
3535
*/
3636
export class BootstrapStack {
37-
public static async lookup(sdkProvider: SdkProvider, environment: Environment, toolkitStackName: string, msg: IoMessaging) {
37+
public static async lookup(sdkProvider: SdkProvider, environment: Environment, toolkitStackName: string, ioHelper: IoHelper) {
3838
toolkitStackName = toolkitStackName ?? DEFAULT_TOOLKIT_STACK_NAME;
3939

4040
const resolvedEnvironment = await sdkProvider.resolveEnvironment(environment);
4141
const sdk = (await sdkProvider.forEnvironment(resolvedEnvironment, Mode.ForWriting)).sdk;
4242

43-
const currentToolkitInfo = await ToolkitInfo.lookup(resolvedEnvironment, sdk, msg, toolkitStackName);
43+
const currentToolkitInfo = await ToolkitInfo.lookup(resolvedEnvironment, sdk, ioHelper, toolkitStackName);
4444

45-
return new BootstrapStack(sdkProvider, sdk, resolvedEnvironment, toolkitStackName, currentToolkitInfo, msg);
45+
return new BootstrapStack(sdkProvider, sdk, resolvedEnvironment, toolkitStackName, currentToolkitInfo, ioHelper);
4646
}
4747

4848
protected constructor(
@@ -51,7 +51,7 @@ export class BootstrapStack {
5151
private readonly resolvedEnvironment: Environment,
5252
private readonly toolkitStackName: string,
5353
private readonly currentToolkitInfo: ToolkitInfo,
54-
private readonly msg: IoMessaging,
54+
private readonly ioHelper: IoHelper,
5555
) {
5656
}
5757

@@ -88,8 +88,7 @@ export class BootstrapStack {
8888
const currentVariant = this.currentToolkitInfo.variant;
8989
const newVariant = bootstrapVariantFromTemplate(template);
9090
if (currentVariant !== newVariant) {
91-
await this.msg.ioHost.notify(warn(
92-
this.msg.action,
91+
await this.ioHelper.notify(warn(
9392
`Bootstrap stack already exists, containing '${currentVariant}'. Not overwriting it with a template containing '${newVariant}' (use --force if you intend to overwrite)`,
9493
));
9594
return abortResponse;
@@ -99,15 +98,13 @@ export class BootstrapStack {
9998
const newVersion = bootstrapVersionFromTemplate(template);
10099
const currentVersion = this.currentToolkitInfo.version;
101100
if (newVersion < currentVersion) {
102-
await this.msg.ioHost.notify(warn(
103-
this.msg.action,
101+
await this.ioHelper.notify(warn(
104102
`Bootstrap stack already at version ${currentVersion}. Not downgrading it to version ${newVersion} (use --force if you intend to downgrade)`,
105103
));
106104
if (newVersion === 0) {
107105
// A downgrade with 0 as target version means we probably have a new-style bootstrap in the account,
108106
// and an old-style bootstrap as current target, which means the user probably forgot to put this flag in.
109-
await this.msg.ioHost.notify(warn(
110-
this.msg.action,
107+
await this.ioHelper.notify(warn(
111108
"(Did you set the '@aws-cdk/core:newStyleStackSynthesis' feature flag in cdk.json?)",
112109
));
113110
}
@@ -145,8 +142,8 @@ export class BootstrapStack {
145142
parameters,
146143
usePreviousParameters: options.usePreviousParameters ?? true,
147144
// Obviously we can't need a bootstrap stack to deploy a bootstrap stack
148-
envResources: new NoBootstrapStackEnvironmentResources(this.resolvedEnvironment, this.sdk, this.msg),
149-
}, this.msg);
145+
envResources: new NoBootstrapStackEnvironmentResources(this.resolvedEnvironment, this.sdk, this.ioHelper),
146+
}, this.ioHelper);
150147

151148
assertIsSuccessfulDeployStackResult(ret);
152149

packages/aws-cdk/lib/api/deployments/asset-publishing.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import {
1313
type ISecretsManagerClient,
1414
} from 'cdk-assets';
1515
import type { SDK } from '..';
16+
import { IoHelper } from '../../../../@aws-cdk/tmp-toolkit-helpers/src/api/io/private';
1617
import { formatMessage } from '../../cli/messages';
17-
import { IIoHost, IoMessageLevel, IoMessaging } from '../../toolkit/cli-io-host';
18+
import type { IoMessageLevel } from '../../toolkit/cli-io-host';
1819
import { ToolkitError } from '../../toolkit/error';
1920
import type { SdkProvider } from '../aws-auth';
2021
import { Mode } from '../plugin';
@@ -43,7 +44,7 @@ export async function publishAssets(
4344
sdk: SdkProvider,
4445
targetEnv: Environment,
4546
options: PublishAssetsOptions,
46-
{ ioHost, action }: IoMessaging,
47+
ioHelper: IoHelper,
4748
) {
4849
// This shouldn't really happen (it's a programming error), but we don't have
4950
// the types here to guide us. Do an runtime validation to be super super sure.
@@ -58,7 +59,7 @@ export async function publishAssets(
5859

5960
const publisher = new AssetPublishing(manifest, {
6061
aws: new PublishingAws(sdk, targetEnv),
61-
progressListener: new PublishingProgressListener({ ioHost, action }),
62+
progressListener: new PublishingProgressListener(ioHelper),
6263
throwOnError: false,
6364
publishInParallel: options.parallel ?? true,
6465
buildAssets: true,
@@ -183,23 +184,20 @@ export const EVENT_TO_LEVEL: Record<EventType, IoMessageLevel | false> = {
183184
};
184185

185186
export abstract class BasePublishProgressListener implements IPublishProgressListener {
186-
protected readonly ioHost: IIoHost;
187-
protected readonly action: IoMessaging['action'];
187+
protected readonly ioHelper: IoHelper;
188188

189-
constructor({ ioHost, action }: IoMessaging) {
190-
this.ioHost = ioHost;
191-
this.action = action;
189+
constructor(ioHelper: IoHelper) {
190+
this.ioHelper = ioHelper;
192191
}
193192

194193
protected abstract getMessage(type: EventType, event: IPublishProgress): string;
195194

196195
public onPublishEvent(type: EventType, event: IPublishProgress): void {
197196
const level = EVENT_TO_LEVEL[type];
198197
if (level) {
199-
void this.ioHost.notify(
198+
void this.ioHelper.notify(
200199
formatMessage({
201200
level,
202-
action: this.action,
203201
message: this.getMessage(type, event),
204202
}),
205203
);

packages/aws-cdk/lib/api/deployments/assets.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import * as cxschema from '@aws-cdk/cloud-assembly-schema';
44
import * as cxapi from '@aws-cdk/cx-api';
55
import * as chalk from 'chalk';
66
import { AssetManifestBuilder } from './asset-manifest-builder';
7+
import { IoHelper } from '../../../../@aws-cdk/tmp-toolkit-helpers/src/api/io/private';
78
import { debug } from '../../cli/messages';
8-
import { IoMessaging } from '../../toolkit/cli-io-host';
99
import { ToolkitError } from '../../toolkit/error';
1010
import { EnvironmentResources } from '../environment';
1111
import { ToolkitInfo } from '../toolkit-info';
@@ -17,7 +17,7 @@ import { ToolkitInfo } from '../toolkit-info';
1717
* pass Asset coordinates.
1818
*/
1919
export async function addMetadataAssetsToManifest(
20-
{ ioHost, action }: IoMessaging,
20+
ioHelper: IoHelper,
2121
stack: cxapi.CloudFormationStackArtifact,
2222
assetManifest: AssetManifestBuilder,
2323
envResources: EnvironmentResources,
@@ -44,24 +44,24 @@ export async function addMetadataAssetsToManifest(
4444
const reuseAsset = reuse.indexOf(asset.id) > -1;
4545

4646
if (reuseAsset) {
47-
await ioHost.notify(debug(action, `Reusing asset ${asset.id}: ${JSON.stringify(asset)}`));
47+
await ioHelper.notify(debug(`Reusing asset ${asset.id}: ${JSON.stringify(asset)}`));
4848
continue;
4949
}
5050

51-
await ioHost.notify(debug(action, `Preparing asset ${asset.id}: ${JSON.stringify(asset)}`));
51+
await ioHelper.notify(debug(`Preparing asset ${asset.id}: ${JSON.stringify(asset)}`));
5252
if (!stack.assembly) {
5353
throw new ToolkitError('Unexpected: stack assembly is required in order to find assets in assembly directory');
5454
}
5555

56-
Object.assign(params, await prepareAsset({ ioHost, action }, asset, assetManifest, envResources, toolkitInfo));
56+
Object.assign(params, await prepareAsset(ioHelper, asset, assetManifest, envResources, toolkitInfo));
5757
}
5858

5959
return params;
6060
}
6161

6262
// eslint-disable-next-line max-len
6363
async function prepareAsset(
64-
{ ioHost, action }: IoMessaging,
64+
ioHelper: IoHelper,
6565
asset: cxschema.AssetMetadataEntry,
6666
assetManifest: AssetManifestBuilder,
6767
envResources: EnvironmentResources,
@@ -71,7 +71,7 @@ async function prepareAsset(
7171
case 'zip':
7272
case 'file':
7373
return prepareFileAsset(
74-
{ ioHost, action },
74+
ioHelper,
7575
asset,
7676
assetManifest,
7777
toolkitInfo,
@@ -85,7 +85,7 @@ async function prepareAsset(
8585
}
8686

8787
async function prepareFileAsset(
88-
{ ioHost, action }: IoMessaging,
88+
ioHelper: IoHelper,
8989
asset: cxschema.FileAssetMetadataEntry,
9090
assetManifest: AssetManifestBuilder,
9191
toolkitInfo: ToolkitInfo,
@@ -98,7 +98,7 @@ async function prepareFileAsset(
9898
const key = `${s3Prefix}${baseName}`;
9999
const s3url = `s3://${toolkitInfo.bucketName}/${key}`;
100100

101-
await ioHost.notify(debug(action, `Storing asset ${asset.path} at ${s3url}`));
101+
await ioHelper.notify(debug(`Storing asset ${asset.path} at ${s3url}`));
102102

103103
assetManifest.addFileAsset(asset.sourceHash, {
104104
path: asset.path,

0 commit comments

Comments
 (0)