From ad51d1d77483bf0b4dc73fd392f5cdefe4ddf5d8 Mon Sep 17 00:00:00 2001 From: Siddhant Date: Thu, 7 Nov 2024 16:36:43 -0500 Subject: [PATCH] improve(wrangler): [R2] Output suggested wrangler.toml changes after creating a bucket (#7193) --- .changeset/purple-poets-bow.md | 5 +++ packages/wrangler/src/__tests__/r2.test.ts | 48 ++++++++++++++++------ packages/wrangler/src/r2/create.ts | 8 +++- 3 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 .changeset/purple-poets-bow.md diff --git a/.changeset/purple-poets-bow.md b/.changeset/purple-poets-bow.md new file mode 100644 index 000000000000..ac5bb6906032 --- /dev/null +++ b/.changeset/purple-poets-bow.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +Output suggested wrangler.toml changes after creating an R2 bucket diff --git a/packages/wrangler/src/__tests__/r2.test.ts b/packages/wrangler/src/__tests__/r2.test.ts index b3bc1ebea6a4..5eca6e3ff7db 100644 --- a/packages/wrangler/src/__tests__/r2.test.ts +++ b/packages/wrangler/src/__tests__/r2.test.ts @@ -269,9 +269,15 @@ describe("r2", () => { ); await runWrangler("r2 bucket create testBucket"); expect(std.out).toMatchInlineSnapshot(` - "Creating bucket 'testBucket'... - ✅ Created bucket 'testBucket' with default storage class of Standard." - `); +"Creating bucket 'testBucket'... +✅ Created bucket 'testBucket' with default storage class of Standard. + +Configure your Worker to write objects to this bucket: + +[[r2_buckets]] +bucket_name = \\"testBucket\\" +binding = \\"testBucket\\"" + `); }); it("should create a bucket with the expected jurisdiction", async () => { @@ -290,17 +296,29 @@ describe("r2", () => { ); await runWrangler("r2 bucket create testBucket -J eu"); expect(std.out).toMatchInlineSnapshot(` - "Creating bucket 'testBucket (eu)'... - ✅ Created bucket 'testBucket (eu)' with default storage class of Standard." - `); +"Creating bucket 'testBucket (eu)'... +✅ Created bucket 'testBucket (eu)' with default storage class of Standard. + +Configure your Worker to write objects to this bucket: + +[[r2_buckets]] +bucket_name = \\"testBucket\\" +binding = \\"testBucket\\"" + `); }); it("should create a bucket with the expected default storage class", async () => { await runWrangler("r2 bucket create testBucket -s InfrequentAccess"); expect(std.out).toMatchInlineSnapshot(` - "Creating bucket 'testBucket'... - ✅ Created bucket 'testBucket' with default storage class of InfrequentAccess." - `); +"Creating bucket 'testBucket'... +✅ Created bucket 'testBucket' with default storage class of InfrequentAccess. + +Configure your Worker to write objects to this bucket: + +[[r2_buckets]] +bucket_name = \\"testBucket\\" +binding = \\"testBucket\\"" + `); }); it("should error if storage class is invalid", async () => { @@ -340,9 +358,15 @@ describe("r2", () => { ); await runWrangler("r2 bucket create testBucket --location weur"); expect(std.out).toMatchInlineSnapshot(` - "Creating bucket 'testBucket'... - ✅ Created bucket 'testBucket' with location hint weur and default storage class of Standard." - `); +"Creating bucket 'testBucket'... +✅ Created bucket 'testBucket' with location hint weur and default storage class of Standard. + +Configure your Worker to write objects to this bucket: + +[[r2_buckets]] +bucket_name = \\"testBucket\\" +binding = \\"testBucket\\"" + `); }); }); diff --git a/packages/wrangler/src/r2/create.ts b/packages/wrangler/src/r2/create.ts index 18ed120dfe2c..1eab044a0095 100644 --- a/packages/wrangler/src/r2/create.ts +++ b/packages/wrangler/src/r2/create.ts @@ -4,6 +4,7 @@ import { UserError } from "../errors"; import { logger } from "../logger"; import * as metrics from "../metrics"; import { requireAuth } from "../user"; +import { getValidBindingName } from "../utils/getValidBindingName"; import { LOCATION_CHOICES } from "./constants"; import { createR2Bucket, isValidR2BucketName } from "./helpers"; import type { @@ -68,8 +69,13 @@ export async function Handler(args: HandlerOptions) { logger.log( `✅ Created bucket '${fullBucketName}' with${ location ? ` location hint ${location} and` : `` - } default storage class of ${storageClass ? storageClass : `Standard`}.` + } default storage class of ${storageClass ? storageClass : `Standard`}.\n\n` + + "Configure your Worker to write objects to this bucket:\n\n" + + "[[r2_buckets]]\n" + + `bucket_name = "${args.name}"\n` + + `binding = "${getValidBindingName(args.name, "r2")}"` ); + await metrics.sendMetricsEvent("create r2 bucket", { sendMetrics: config.send_metrics, });