Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): use CliIoHost as logger everywhere #32708

Merged
merged 28 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d304251
initial functional IoHost logging
HBobertz Jan 1, 2025
12396ca
Merge branch 'main' into bobertzh/CliIoHost
HBobertz Jan 1, 2025
72c93da
add testing
HBobertz Jan 3, 2025
498dde1
Merge branch 'main' into bobertzh/CliIoHost
HBobertz Jan 3, 2025
fa3513d
fix lint problems
HBobertz Jan 3, 2025
ea2f842
better comments
HBobertz Jan 3, 2025
a5b59a6
lint fixes
HBobertz Jan 3, 2025
6f9add3
update logic and improve testing
HBobertz Jan 5, 2025
306f08b
update non-functionals
HBobertz Jan 5, 2025
879fa3c
update log function name
HBobertz Jan 5, 2025
255dc60
change comment
HBobertz Jan 5, 2025
cc2f7a9
add action logic
HBobertz Jan 5, 2025
ae48a18
Merge branch 'main' into bobertzh/CliIoHost
HBobertz Jan 5, 2025
3c6bb9b
add format
HBobertz Jan 6, 2025
65751a4
add cli-io-host logging
HBobertz Jan 8, 2025
7e45655
Update packages/aws-cdk/lib/logging.ts
HBobertz Jan 8, 2025
b9a8c60
chalk support the smart way
HBobertz Jan 8, 2025
826bd8d
Update packages/aws-cdk/lib/toolkit/cli-io-host.ts
HBobertz Jan 10, 2025
5e0d431
address feedback
HBobertz Jan 10, 2025
9c5bb1d
address feedback
HBobertz Jan 10, 2025
0a893b6
update activity base
HBobertz Jan 10, 2025
8330271
feat(toolkit): enforce codes at compile time
mrgrain Jan 13, 2025
699edd4
fixup test
mrgrain Jan 13, 2025
c9bc8ad
address feedback
HBobertz Jan 13, 2025
f342b96
Merge branch 'bobertzh/CliIoHost' into mrgrain/fix-compile-time-messa…
HBobertz Jan 13, 2025
0f2fb63
fix tests
HBobertz Jan 13, 2025
e104e60
chore(toolkit): enforce codes at compile time (#32893)
HBobertz Jan 13, 2025
59504c4
Merge branch 'main' into bobertzh/CliIoHost
HBobertz Jan 13, 2025
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
7 changes: 3 additions & 4 deletions packages/aws-cdk/lib/api/aws-auth/credential-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { inspect } from 'util';
import { inspect, format } from 'util';
import type { CredentialProviderSource, ForReading, ForWriting, PluginProviderResult, SDKv2CompatibleCredentials, SDKv3CompatibleCredentialProvider, SDKv3CompatibleCredentials } from '@aws-cdk/cli-plugin-contract';
import type { AwsCredentialIdentity, AwsCredentialIdentityProvider } from '@smithy/types';
import { credentialsAboutToExpire, makeCachingProvider } from './provider-caching';
import { debug, warning } from '../../logging';
import { debug, warning, info } from '../../logging';
import { AuthenticationError } from '../../toolkit/error';
import { formatErrorMessage } from '../../util/error';
import { Mode } from '../plugin/mode';
Expand Down Expand Up @@ -151,8 +151,7 @@ function v3ProviderFromV2Credentials(x: SDKv2CompatibleCredentials): AwsCredenti

function refreshFromPluginProvider(current: AwsCredentialIdentity, producer: () => Promise<PluginProviderResult>): AwsCredentialIdentityProvider {
return async () => {
// eslint-disable-next-line no-console
console.error(current, Date.now());
info(format(current), Date.now());
if (credentialsAboutToExpire(current)) {
HBobertz marked this conversation as resolved.
Show resolved Hide resolved
const newCreds = await producer();
if (!isV3Credentials(newCreds)) {
Expand Down
8 changes: 4 additions & 4 deletions packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as chalk from 'chalk';
import { minimatch } from 'minimatch';
import * as semver from 'semver';
import { error, print, warning } from '../../logging';
import { error, info, warning } from '../../logging';
import { ToolkitError } from '../../toolkit/error';
import { flatten } from '../../util';

Expand Down Expand Up @@ -264,7 +264,7 @@
printMessage(error, 'Error', message.id, message.entry);
break;
case cxapi.SynthesisMessageLevel.INFO:
printMessage(print, 'Info', message.id, message.entry);
printMessage(info, 'Info', message.id, message.entry);

Check warning on line 267 in packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts#L267

Added line #L267 was not covered by tests
break;
}
}
Expand Down Expand Up @@ -346,7 +346,7 @@
} while (madeProgress);

if (added.length > 0) {
print('Including depending stacks: %s', chalk.bold(added.join(', ')));
info('Including depending stacks: %s', chalk.bold(added.join(', ')));

Check warning on line 349 in packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts#L349

Added line #L349 was not covered by tests
}
}

Expand Down Expand Up @@ -376,7 +376,7 @@
}

if (added.length > 0) {
print('Including dependency stacks: %s', chalk.bold(added.join(', ')));
info('Including dependency stacks: %s', chalk.bold(added.join(', ')));
}
}

Expand Down
16 changes: 8 additions & 8 deletions packages/aws-cdk/lib/api/deploy-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { HotswapMode, HotswapPropertyOverrides, ICON } from './hotswap/common';
import { tryHotswapDeployment } from './hotswap-deployments';
import { addMetadataAssetsToManifest } from '../assets';
import { debug, print, warning } from '../logging';
import { debug, info, warning } from '../logging';
import {
changeSetHasNoChanges,
CloudFormationStack,
Expand Down Expand Up @@ -333,7 +333,7 @@
// if we can skip deployment and we are performing a hotswap, let the user know
// that no hotswap deployment happened
if (hotswapMode !== HotswapMode.FULL_DEPLOYMENT) {
print(
info(

Check warning on line 336 in packages/aws-cdk/lib/api/deploy-stack.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deploy-stack.ts#L336

Added line #L336 was not covered by tests
`\n ${ICON} %s\n`,
chalk.bold('hotswap deployment skipped - no changes were detected (use --force to override)'),
);
Expand Down Expand Up @@ -379,22 +379,22 @@
if (hotswapDeploymentResult) {
return hotswapDeploymentResult;
}
print(
info(
'Could not perform a hotswap deployment, as the stack %s contains non-Asset changes',
stackArtifact.displayName,
);
} catch (e) {
if (!(e instanceof CfnEvaluationException)) {
throw e;
}
print(
info(

Check warning on line 390 in packages/aws-cdk/lib/api/deploy-stack.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deploy-stack.ts#L390

Added line #L390 was not covered by tests
'Could not perform a hotswap deployment, because the CloudFormation template could not be resolved: %s',
formatErrorMessage(e),
);
}

if (hotswapMode === HotswapMode.FALL_BACK) {
print('Falling back to doing a full deployment');
info('Falling back to doing a full deployment');
options.sdk.appendCustomUserAgent('cdk-hotswap/fallback');
} else {
return {
Expand Down Expand Up @@ -505,7 +505,7 @@
}

if (!execute) {
print(
info(
'Changeset %s created and waiting in review for manual execution (--no-execute)',
changeSetDescription.ChangeSetId,
);
Expand Down Expand Up @@ -538,7 +538,7 @@
await this.cleanupOldChangeset(changeSetName);

debug(`Attempting to create ChangeSet with name ${changeSetName} to ${this.verb} stack ${this.stackName}`);
print('%s: creating CloudFormation changeset...', chalk.bold(this.stackName));
info('%s: creating CloudFormation changeset...', chalk.bold(this.stackName));
const changeSet = await this.cfn.createChangeSet({
StackName: this.stackName,
ChangeSetName: changeSetName,
Expand Down Expand Up @@ -609,7 +609,7 @@
}

private async directDeployment(): Promise<SuccessfulDeployStackResult> {
print('%s: %s stack...', chalk.bold(this.stackName), this.update ? 'updating' : 'creating');
info('%s: %s stack...', chalk.bold(this.stackName), this.update ? 'updating' : 'creating');

const startTime = new Date();

Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk/lib/api/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,6 @@ export class Deployments {

// No need to validate anymore, we already did that during build
const publisher = this.cachedPublisher(assetManifest, stackEnv, options.stackName);
// eslint-disable-next-line no-console
HBobertz marked this conversation as resolved.
Show resolved Hide resolved
await publisher.publishEntry(asset, { allowCrossAccount: await this.allowCrossAccountAssetPublishingForEnv(options.stack) });
if (publisher.hasFailures) {
throw new Error(`Failed to publish asset ${asset.id}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Tag } from '@aws-sdk/client-s3';
import * as chalk from 'chalk';
import * as promptly from 'promptly';
import { debug, print } from '../../logging';
import { debug, info } from '../../logging';
import { IECRClient, IS3Client, SDK, SdkProvider } from '../aws-auth';
import { DEFAULT_TOOLKIT_STACK_NAME, ToolkitInfo } from '../toolkit-info';
import { ProgressPrinter } from './progress-printer';
Expand Down Expand Up @@ -526,7 +526,7 @@
printer.reportDeletedAsset(deletables.slice(0, deletedCount));
}
} catch (err) {
print(chalk.red(`Error deleting images: ${err}`));
info(chalk.red(`Error deleting images: ${err}`));

Check warning on line 529 in packages/aws-cdk/lib/api/garbage-collection/garbage-collector.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/garbage-collection/garbage-collector.ts#L529

Added line #L529 was not covered by tests
}
}

Expand Down Expand Up @@ -559,23 +559,23 @@
printer.reportDeletedAsset(deletables.slice(0, deletedCount));
}
} catch (err) {
print(chalk.red(`Error deleting objects: ${err}`));
info(chalk.red(`Error deleting objects: ${err}`));

Check warning on line 562 in packages/aws-cdk/lib/api/garbage-collection/garbage-collector.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/garbage-collection/garbage-collector.ts#L562

Added line #L562 was not covered by tests
}
}

private async bootstrapBucketName(sdk: SDK, bootstrapStackName: string): Promise<string> {
const info = await ToolkitInfo.lookup(this.props.resolvedEnvironment, sdk, bootstrapStackName);
return info.bucketName;
const toolkitInfo = await ToolkitInfo.lookup(this.props.resolvedEnvironment, sdk, bootstrapStackName);
return toolkitInfo.bucketName;
}

private async bootstrapRepositoryName(sdk: SDK, bootstrapStackName: string): Promise<string> {
const info = await ToolkitInfo.lookup(this.props.resolvedEnvironment, sdk, bootstrapStackName);
return info.repositoryName;
const toolkitInfo = await ToolkitInfo.lookup(this.props.resolvedEnvironment, sdk, bootstrapStackName);
return toolkitInfo.repositoryName;
}

private async bootstrapQualifier(sdk: SDK, bootstrapStackName: string): Promise<string | undefined> {
const info = await ToolkitInfo.lookup(this.props.resolvedEnvironment, sdk, bootstrapStackName);
return info.bootstrapStack.parameters.Qualifier;
const toolkitInfo = await ToolkitInfo.lookup(this.props.resolvedEnvironment, sdk, bootstrapStackName);
return toolkitInfo.bootstrapStack.parameters.Qualifier;
}

private async numObjectsInBucket(s3: IS3Client, bucket: string): Promise<number> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as chalk from 'chalk';
import { GcAsset as GCAsset } from './garbage-collector';
import { print } from '../../logging';
import { info } from '../../logging';

export class ProgressPrinter {
private totalAssets: number;
Expand Down Expand Up @@ -68,9 +68,9 @@
const percentage = ((this.assetsScanned / this.totalAssets) * 100).toFixed(2);
// print in MiB until we hit at least 1 GiB of data tagged/deleted
if (Math.max(this.taggedAssetsSizeMb, this.deletedAssetsSizeMb) >= 1000) {
print(chalk.green(`[${percentage}%] ${this.assetsScanned} files scanned: ${this.taggedAsset} assets (${(this.taggedAssetsSizeMb / 1000).toFixed(2)} GiB) tagged, ${this.deletedAssets} assets (${(this.deletedAssetsSizeMb / 1000).toFixed(2)} GiB) deleted.`));
info(chalk.green(`[${percentage}%] ${this.assetsScanned} files scanned: ${this.taggedAsset} assets (${(this.taggedAssetsSizeMb / 1000).toFixed(2)} GiB) tagged, ${this.deletedAssets} assets (${(this.deletedAssetsSizeMb / 1000).toFixed(2)} GiB) deleted.`));

Check warning on line 71 in packages/aws-cdk/lib/api/garbage-collection/progress-printer.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/garbage-collection/progress-printer.ts#L71

Added line #L71 was not covered by tests
} else {
print(chalk.green(`[${percentage}%] ${this.assetsScanned} files scanned: ${this.taggedAsset} assets (${this.taggedAssetsSizeMb.toFixed(2)} MiB) tagged, ${this.deletedAssets} assets (${this.deletedAssetsSizeMb.toFixed(2)} MiB) deleted.`));
info(chalk.green(`[${percentage}%] ${this.assetsScanned} files scanned: ${this.taggedAsset} assets (${this.taggedAssetsSizeMb.toFixed(2)} MiB) tagged, ${this.deletedAssets} assets (${this.deletedAssetsSizeMb.toFixed(2)} MiB) deleted.`));
}
}
}
18 changes: 9 additions & 9 deletions packages/aws-cdk/lib/api/hotswap-deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as chalk from 'chalk';
import type { SDK, SdkProvider } from './aws-auth';
import type { SuccessfulDeployStackResult } from './deploy-stack';
import { EvaluateCloudFormationTemplate } from './evaluate-cloudformation-template';
import { print } from '../logging';
import { info } from '../logging';
import { isHotswappableAppSyncChange } from './hotswap/appsync-mapping-templates';
import { isHotswappableCodeBuildProjectChange } from './hotswap/code-build-projects';
import {
Expand Down Expand Up @@ -400,7 +400,7 @@ function isCandidateForHotswapping(

async function applyAllHotswappableChanges(sdk: SDK, hotswappableChanges: HotswappableChange[]): Promise<void[]> {
if (hotswappableChanges.length > 0) {
print(`\n${ICON} hotswapping resources:`);
info(`\n${ICON} hotswapping resources:`);
}
const limit = pLimit(10);
// eslint-disable-next-line @cdklabs/promiseall-no-unbounded-parallelism
Expand All @@ -415,7 +415,7 @@ async function applyHotswappableChange(sdk: SDK, hotswapOperation: HotswappableC
sdk.appendCustomUserAgent(customUserAgent);

for (const name of hotswapOperation.resourceNames) {
print(` ${ICON} %s`, chalk.bold(name));
info(` ${ICON} %s`, chalk.bold(name));
}

// if the SDK call fails, an error will be thrown by the SDK
Expand All @@ -436,7 +436,7 @@ async function applyHotswappableChange(sdk: SDK, hotswapOperation: HotswappableC
}

for (const name of hotswapOperation.resourceNames) {
print(`${ICON} %s %s`, chalk.bold(name), chalk.green('hotswapped!'));
info(`${ICON} %s %s`, chalk.bold(name), chalk.green('hotswapped!'));
}

sdk.removeCustomUserAgent(customUserAgent);
Expand All @@ -461,33 +461,33 @@ function logNonHotswappableChanges(nonHotswappableChanges: NonHotswappableChange
}
}
if (hotswapMode === HotswapMode.HOTSWAP_ONLY) {
print(
info(
'\n%s %s',
chalk.red('⚠️'),
chalk.red(
'The following non-hotswappable changes were found. To reconcile these using CloudFormation, specify --hotswap-fallback',
),
);
} else {
print('\n%s %s', chalk.red('⚠️'), chalk.red('The following non-hotswappable changes were found:'));
info('\n%s %s', chalk.red('⚠️'), chalk.red('The following non-hotswappable changes were found:'));
}

for (const change of nonHotswappableChanges) {
change.rejectedChanges.length > 0
? print(
? info(
' logicalID: %s, type: %s, rejected changes: %s, reason: %s',
chalk.bold(change.logicalId),
chalk.bold(change.resourceType),
chalk.bold(change.rejectedChanges),
chalk.red(change.reason),
)
: print(
: info(
' logicalID: %s, type: %s, reason: %s',
chalk.bold(change.logicalId),
chalk.bold(change.resourceType),
chalk.red(change.reason),
);
}

print(''); // newline
info(''); // newline
}
4 changes: 2 additions & 2 deletions packages/aws-cdk/lib/api/logs/logs-monitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as util from 'util';
import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import { print, error } from '../../logging';
import { info, error } from '../../logging';
import { flatten } from '../../util/arrays';
import type { SDK } from '../aws-auth';

Expand Down Expand Up @@ -162,7 +162,7 @@ export class CloudWatchLogEventMonitor {
* Print out a cloudwatch event
*/
private print(event: CloudWatchLogEvent): void {
print(
info(
util.format(
'[%s] %s %s',
chalk.blue(event.logGroupName),
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/plugin/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { inspect } from 'util';
import type { CredentialProviderSource, IPluginHost, Plugin } from '@aws-cdk/cli-plugin-contract';
import * as chalk from 'chalk';

import * as chalk from 'chalk';
import { type ContextProviderPlugin, isContextProviderPlugin } from './context-provider-plugin';
import { error } from '../../logging';
import { ToolkitError } from '../../toolkit/error';
Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk/lib/api/util/cloudformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ export async function createDiffChangeSet(
// This causes CreateChangeSet to fail with `Template Error: Fn::Equals cannot be partially collapsed`.
for (const resource of Object.values(options.stack.template.Resources ?? {})) {
if ((resource as any).Type === 'AWS::CloudFormation::Stack') {
// eslint-disable-next-line no-console
debug('This stack contains one or more nested stacks, falling back to template-only diff...');

return undefined;
Expand Down
Loading
Loading