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

Merged
merged 6 commits into from
Aug 2, 2025

Conversation

Akshit222
Copy link
Contributor

@Akshit222 Akshit222 commented Jun 9, 2025

Fixes #9433

What

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

Why

The current output is not machine-readable.
Adding this flag 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
  • Suppresses the Wrangler banner when --json is used
  • Adds a unit test verifying valid JSON output
  • Linting fixed with pnpm fix
  • Added a changeset as per contribution guidelines

Test Plan

  • pnpm exec vitest run packages/wrangler/src/__tests__/r2/bucket.test.ts passed
  • ✅ Manual output verified
  • ✅ Verified that banner is suppressed when --json is passed

Example

With --json:

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

{
  "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)
Bucket Name: my-bucket-name  
Created At: 2025-06-07T15:55:22.222Z  
Location: APAC  
Storage Class: Standard  
Object Count: 0  
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: 9ec5cf8

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

This PR includes changesets to release 3 packages
Name Type
wrangler Minor
@cloudflare/vite-plugin Major
@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
Contributor 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
Contributor 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
Contributor 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: 9ec5cf8

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 :)

@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 2 times, most recently from 4cc8e51 to c7ddab9 Compare July 9, 2025 11:44
@Akshit222
Copy link
Contributor 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
Contributor 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!

@penalosa
Copy link
Contributor

@Akshit222 thanks for this, this is looking pretty good! Would you be able to restore the PR template and fill out the relevant checkboxes, so we can get this merged?

@Akshit222
Copy link
Contributor Author

Thanks for the feedback! I'll update the PR template and checkboxes shortly.

@Akshit222
Copy link
Contributor Author

Akshit222 commented Jul 21, 2025

I've updated the PR description with the template and filled out the checkboxes as requested.
Please let me know if there's anything else you'd like me to address!

Also, please let me know if any of the failing checks need action on my side — happy to fix if required.

@petebacondarwin petebacondarwin force-pushed the wrangler-r2-info-json branch from c7ddab9 to 1dec4af Compare July 23, 2025 09:02
Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

Thanks for this. I expect that there will be a bunch of snapshot failures that need updating now that the help text for these commands will have changed.

pnpm test:ci -F wrangler -- -u

@petebacondarwin
Copy link
Contributor

there are a bunch of linting errors which would mostly be solved by simplifying the test as suggested.

@petebacondarwin
Copy link
Contributor

Please open a draft docs-PR before merging and reference it in the PR description. It can then be reviewed and merged after this had been released.

@frankichiro
Copy link

Wow, this really required much more work than I imagined it would. Thanks @Akshit222 for seeing it through, I really appreciate your efforts for making this work.

@Akshit222
Copy link
Contributor Author

Thanks a lot for the thoughtful feedback and guidance
I will make the rest of mentioned changes as asked very soon

@Akshit222
Copy link
Contributor Author

Akshit222 commented Jul 30, 2025

Thanks for the detailed review! I've made the following changes based on your feedback:

Snapshot concerns: I'm not using Jest snapshots in these tests, so there's no need to update any. The existing test setup avoids snapshot-based output assertions — but let me know if I misunderstood and you were referring to something else.

Mock reset: You were right — we already call vi.resetAllMocks() by default, so that part was redundant. I've cleaned it up.

JSON parse block simplification: Simplified the --json test as suggested. It now directly asserts on the parsed output rather than wrapping the parse in a try-catch.

Linting issues: These were mostly resolved by the above simplification and cleanup — no lint errors now. I checked after running pnpm check:lint.

logger.json() integration: Updated the bucket.ts implementation to use logger.json() for consistency with other commands when the --json flag is used.

I'll send a follow-up PR to add the relevant docs to docs/content/en/r2 as discussed soon.

Let me know if there’s anything else to tweak!

Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

Thank you @Akshit222 for bearing with us. Looks good now.

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Aug 1, 2025
@petebacondarwin petebacondarwin self-assigned this Aug 1, 2025
@petebacondarwin petebacondarwin merged commit e82aa19 into cloudflare:main Aug 2, 2025
70 of 74 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Aug 2, 2025
@workers-devprod workers-devprod added the contribution [Holopin] Recognizes an open-source contribution, big or small label Aug 2, 2025
Copy link

holopin-bot bot commented Aug 2, 2025

Congratulations @Akshit222, the maintainer of this repository has issued you a holobyte! Here it is: https://holopin.io/holobyte/cmduai1z6006807l5zii5jc3t

This badge can only be claimed by you, so make sure that your GitHub account is linked to your Holopin account. You can manage those preferences here: https://holopin.io/account.
Or if you're new to Holopin, you can simply sign up with GitHub, which will do the trick!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution [Holopin] Recognizes an open-source contribution, big or small
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

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