Add --inplace/-i flag to format files in place#4
Conversation
Writes formatted output back to the source file instead of stdout. Uses clap conflicts_with to enforce mutual exclusivity with --check. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded an Changes
Sequence DiagramsequenceDiagram
participant User
participant CLI as "CLI Parser"
participant Run as "run() / Validator"
participant Process as "process(name, sql, path, cli)"
participant FS as "File I/O / Permissions"
participant Stdout as "Stdout / Reporter"
User->>CLI: invoke with flags and args
CLI->>Run: parsed args (incl. --inplace / --check)
Run->>Run: validate conflicts & presence of file paths
alt invalid (no files or stdin with --inplace)
Run->>User: return error
else valid with file paths and --inplace
Run->>Process: call with path(s)
Process->>Process: format SQL
alt check mode
Process->>Stdout: report "Would reformat"
else inplace mode
Process->>FS: write formatted SQL, restore perms
FS->>Stdout: confirm update
end
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main.rs (1)
42-44: Consider preserving file permissions when writing in place.
fs::writemay alter the file's permissions on some platforms. If the original file had non-default permissions (e.g.,0o600), they could be reset.♻️ Suggested approach to preserve permissions
+use std::os::unix::fs::PermissionsExt; + fn process(name: &str, sql: &str, path: Option<&PathBuf>, cli: &Cli) -> Result<bool, String> { // ... } else if cli.inplace { let path = path.ok_or_else(|| "cannot use --inplace with stdin".to_string())?; + let metadata = fs::metadata(path).map_err(|e| format!("{name}: {e}"))?; + let permissions = metadata.permissions(); fs::write(path, &formatted).map_err(|e| format!("{name}: {e}"))?; + fs::set_permissions(path, permissions).map_err(|e| format!("{name}: {e}"))?; }Alternatively, for maximum safety (atomic writes), write to a temp file in the same directory and rename it over the original. This prevents corruption if the process is interrupted mid-write. This is optional for a CLI tool of this nature.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/main.rs` around lines 42 - 44, The inplace write uses fs::write which can change file permissions; modify the cli.inplace branch (where path, formatted are used) to preserve permissions by reading the original file metadata (fs::metadata) and capturing its permissions, then write the output to a temporary file in the same directory (e.g., using a unique temp name next to path), set the temp file's permissions to the original permissions (fs::set_permissions or OpenOptions with mode on Unix), and atomically rename the temp file to the original path (fs::rename); ensure errors are propagated the same way as existing map_err formatting.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/main.rs`:
- Around line 42-44: The inplace write uses fs::write which can change file
permissions; modify the cli.inplace branch (where path, formatted are used) to
preserve permissions by reading the original file metadata (fs::metadata) and
capturing its permissions, then write the output to a temporary file in the same
directory (e.g., using a unique temp name next to path), set the temp file's
permissions to the original permissions (fs::set_permissions or OpenOptions with
mode on Unix), and atomically rename the temp file to the original path
(fs::rename); ensure errors are propagated the same way as existing map_err
formatting.
Read the original file's permissions before writing and restore them after, preventing fs::write from resetting non-default permissions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
--inplace/-iCLI flag that writes formatted output back to the source file instead of stdoutconflicts_withto enforce mutual exclusivity with--check--inplaceTest plan
pgfmt -i file.sqlformats and overwritesfile.sqlpgfmt --inplace file1.sql file2.sqlformats multiple files in placepgfmt -i --check file.sqlerrors with clap conflict messageecho "SELECT 1" | pgfmt -ierrors requiring filespgfmt file.sqlstill prints to stdout (default behavior unchanged)🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
--inplace/-iflag to write formatted SQL back to input files (conflicts with--check).Behavior Changes
--checkcontinues to only report "Would reformat" without writing.--inplacerestores original file permissions after writing, errors if used with stdin, and is rejected when no file paths are provided.