This repository hosts r2, a Go (1.22) CLI and library for Cloudflare R2 built on Cobra and the AWS S3 SDK. Use this guide to contribute changes consistently and safely.
cmd/: Cobra commands (one file per subcommand, e.g.cp.go,ls.go,sync.go; root inroot.go).pkg/: Library for R2 operations (client.go,bucket.go,helpers.go). Prefer adding logic here first.main.go: CLI entry point.assets/: Static assets (e.g.,bucket.svg)..github/workflows/: Release/automation.- Docs:
README.md,USAGE.md,ARCHITECTURE.md.
- Build:
go build -o bin/r2 .(module root), orgo build ./...to verify packages. - Run locally:
go run .or after installr2 help. - Tests:
go test ./...(add package tests underpkg/). - Format/Vet:
go fmt ./... && go vet ./.... - Release (dry run):
goreleaser release --snapshot --skip-publish --clean.
- Formatting: enforce
go fmt; keep imports tidy (go mod tidybefore PRs). - Structure: business logic in
pkg, thin Cobra layers incmdfor flags/UX. - Naming: exported types/methods in
pkguse Go conventions (e.g.,R2Client,R2Bucket.Sync...);cmd/<name>.gofor subcommands. - Errors: return errors from
pkg; CLI maylog.Fatalon unrecoverable user errors.
- Framework: standard
testingwith table‑driven tests. - Location: co‑locate
*_test.gowith sources (focus onpkg). - Run/Coverage:
go test -v -cover ./pkg/.... - Isolation: avoid real R2 calls in tests; mock/stub S3 interactions or factor logic for unit testing.
- Commits: follow Conventional Commits (
feat:,fix:,docs:,refactor:), as in this repo’s history. - PRs must include: clear description, linked issue, CLI examples (e.g.,
r2 cp r2://bucket/a b), and doc updates when UX changes. - Checks: ensure
go fmt,go vet,go test, andgo mod tidypass; avoid diff ingo.sumunless necessary.
- Never commit credentials;
r2 configurestores profiles in~/.r2. - Redact secrets in logs/output; validate bucket names and paths (see
pkg/helpers.go).