Skip to content

[wrangler] Add --json flag to r2 bucket info command #9530

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Akshit222
Copy link

@Akshit222 Akshit222 commented Jun 9, 2025

What

Adds a --json flag to the wrangler r2 bucket info command, returning the bucket info in clean JSON format.

Why

Fixes #9433 — The current output is not machine-readable. Adding this makes the output usable in scripts and consistent with D1 commands that already support --json.

How

  • Adds a --json flag to r2 bucket info
  • Omits verbose output when --json is passed
  • Adds a unit test verifying valid JSON output

Test Plan

  • pnpm exec vitest run packages/wrangler/src/__tests__/r2/bucket.test.ts passed
  • ✅ Manual output verified

Notes

Currently, the banner line (⛅ wrangler 4.18.0) still appears even when using --json.
I left it in earlier to avoid blocking the main feature, but it has now been suppressed when --json is passed for cleaner machine-readable output.

Example

With --json:

$ wrangler r2 bucket info my-bucket-name --json

(⛅️ wrangler 4.18.0\n──────────────────)
{
  "name": "my-bucket-name",
  "created": "2025-06-07T15:55:22.222Z",
  "location": "APAC",
  "default_storage_class": "Standard",
  "object_count": "0",
  "bucket_size": "0 B"
}

Without --json:

$ wrangler r2 bucket info my-bucket-name

(⛅️ wrangler 4.18.0\n──────────────────)
Bucket Name: my-bucket-name  
Created At: 2025-06-07T15:55:22.222Z  
Location: APAC  
Storage Class: Standard  
Object Count: 0  
Size: 0 B

Edit (June 12, 2025)

Implemented the requested change to suppress the banner (⛅ wrangler 4.18.0) when --json is used.

$ wrangler r2 bucket info my-bucket-name

{
  "name": "my-bucket-name",
  "created": "2025-06-07T15:55:22.222Z",
  "location": "APAC",
  "default_storage_class": "Standard",
  "object_count": "0",
  "bucket_size": "0 B"
}

@Akshit222 Akshit222 requested a review from a team as a code owner June 9, 2025 10:53
Copy link

changeset-bot bot commented Jun 9, 2025

🦋 Changeset detected

Latest commit: 4cc8e51

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
wrangler Patch
@cloudflare/vitest-pool-workers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@frankichiro
Copy link

Hello. Since I'm who issued #9433, I'm very happy about this. Although it would be very preferable if it was possible to remove the banner line when using --json, otherwise the result needs to be filtered, which was essentially the original problem. This would also make it consistent with how --json works with D1.

@Akshit222
Copy link
Author

Akshit222 commented Jun 10, 2025

Thanks so much for the feedback, @frankichiro — totally agree with you.
You're right that having the banner in --json mode defeats the purpose of clean, machine-readable output and makes it inconsistent with other commands like D1.

I'll go ahead and update the PR to suppress the banner when --json is used, so the output is clean and parseable.

@Akshit222
Copy link
Author

Made the requested changes to suppress the banner when --json is used. Let me know if there's anything else I should address — happy to iterate further. Thanks again for the guidance!

@Akshit222
Copy link
Author

Just wanted to follow up on this! Let me know if there’s anything else needed from my side to move this forward — happy to make any changes if required. Thanks again for your time!

Copy link

pkg-pr-new bot commented Jul 7, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@9530

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@9530

miniflare

npm i https://pkg.pr.new/miniflare@9530

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@9530

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@9530

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@9530

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@9530

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@9530

wrangler

npm i https://pkg.pr.new/wrangler@9530

commit: 1064cfc

Copy link
Contributor

@emily-shen emily-shen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks so much for the contribution - sorry this was missed for a while, looks like our github automations went wonky and it wasn't added to our queue 😅

Just a couple of small things, but on the whole looks good.

Would you also be able to add a changeset? https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/CONTRIBUTING.md#changesets

And if you're up to it, would you also be able to create a PR to the docs to add it to the wrangler command documentation? https://developers.cloudflare.com/workers/wrangler/commands/#r2-bucket-info

Let me know if you have any questions about this :)

type: "boolean",
default: false,
},
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can hide the banner like this:

instead of directly modifying wrangler-banner.ts

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback , I’ve now reverted teh changes to wrangler-banner.ts. Instead, I’m using the behaviour: { showBanner: false } option inside the relevant CLI command for the --json path, similar to how it's done in d1/list.ts.

import { requireAuth } from "../../user";
import { getR2Bucket, getR2BucketMetrics } from "../../r2/helpers";

// Mock the dependencies
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add vi.unmock("../wrangler-banner"); to test the banner isn't included? we mock it out by default in tests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added vi.unmock("../wrangler-banner") to ensure we’re explicitly testing that the banner isn’t printed when using --json.

it("should output valid JSON format when --json flag is used", async () => {
let capturedOutput = "";

const originalConsoleLog = console.log;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should use the mockConsoleMethods() helper here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated! I’m now using mockConsoleMethods() in the test instead of manually mocking console.log

@emily-shen
Copy link
Contributor

also, it looks like you have some linting issues. you can run pnpm fix to fix them up

@Akshit222 Akshit222 force-pushed the wrangler-r2-info-json branch from 1064cfc to 4cc8e51 Compare July 9, 2025 10:36
@Akshit222 Akshit222 force-pushed the wrangler-r2-info-json branch from 4cc8e51 to c7ddab9 Compare July 9, 2025 11:44
@Akshit222
Copy link
Author

Thanks a lot for the detailed review,
I’ve addressed all feedback:

-Used behaviour: { showBanner: false } instead of editing the banner file
-Added vi.unmock("../wrangler-banner") to ensure the banner isn’t shown during --json output
-Switched to mockConsoleMethods() in tests
-Ran pnpm fix
-Added a changeset

Let me know if i made a mistake anywhere.

@Akshit222
Copy link
Author

Just a quick note: I ended up cleaning up the commit history because the original one had some unrelated files accidentally committed. To keep things clean and focused, I created a fresh branch from main, reapplied only the relevant changes, and force-pushed — which unfortunately removed previous commit-level context. But I’ve addressed all the feedback again in this clean version.

Once you’ve had a chance to review this updated commit, I’ll go ahead and create a follow-up PR to update the Wrangler documentation for the r2 bucket info command as well.

Let me know if anything else is needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Untriaged
Development

Successfully merging this pull request may close these issues.

Add --json option to R2 commands in Wrangler
3 participants