Skip to content

Commit 2a1fb1e

Browse files
Scott8440a-frantz
andauthored
feat: require one of check or overwrite options in format command (#51)
* feat: require one of check or overwrite options in format command * Apply suggestions from code review * Update src/commands/format.rs * Update format.rs * Update CHANGELOG.md --------- Co-authored-by: Andrew Frantz <andrew.frantz@stjude.org>
1 parent 4a5db76 commit 2a1fb1e

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Changed
11+
12+
* `format` now requires one of the `--check` or `--overwrite` arguments ([#51](https://github.com/stjude-rust-labs/sprocket/pull/51)).
13+
1014
## 0.9.0 - 10-22-2024
1115

1216
### Changed

src/commands/format.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ const DEFAULT_SPACE_IDENT_SIZE: usize = 4;
4343
author,
4444
version,
4545
about,
46-
after_help = "By default, the `format` command will print a single formatted WDL \
47-
document.\n\nUse the `--overwrite` option to replace a WDL document, or a \
48-
directory containing WDL documents, with the formatted source."
46+
after_help = "Use the `--overwrite` option to replace a WDL document or a directory \
47+
containing WDL documents with the formatted source.\nUse the `--check` option \
48+
to verify that a document or a directory containing WDL documents is already \
49+
formatted and print the diff if not."
4950
)]
5051
pub struct FormatArgs {
5152
/// The path to the WDL document to format (`-` for STDIN); the path may be
@@ -70,12 +71,21 @@ pub struct FormatArgs {
7071
#[arg(long, value_name = "SIZE")]
7172
pub indentation_size: Option<usize>,
7273

74+
/// Argument group defining the mode of behavior
75+
#[command(flatten)]
76+
mode: ModeGroup,
77+
}
78+
79+
/// Argument group defining the mode of behavior
80+
#[derive(Parser, Debug)]
81+
#[group(required = true, multiple = false)]
82+
pub struct ModeGroup {
7383
/// Overwrite the WDL documents with the formatted versions
7484
#[arg(long, conflicts_with = "check")]
7585
pub overwrite: bool,
7686

7787
/// Check if files are formatted correctly and print diff if not
78-
#[arg(long, conflicts_with = "overwrite")]
88+
#[arg(long)]
7989
pub check: bool,
8090
}
8191

@@ -98,20 +108,23 @@ fn read_source(path: &Path) -> Result<String> {
98108

99109
/// Formats a document.
100110
///
111+
/// If `check_only` is true, checks if the document is formatted correctly and
112+
/// prints the diff if not then exits. Else will format and overwrite the
113+
/// document.
114+
///
101115
/// If the document failed to parse, this emits the diagnostics and returns
102116
/// `Ok(count)` of the diagnostics to the caller.
103117
///
104118
/// A return value of `Ok(0)` indicates the document was formatted.
105119
fn format_document(
106120
config: Config,
107121
path: &Path,
108-
overwrite: bool,
109122
report_mode: Mode,
110123
no_color: bool,
111-
check: bool,
124+
check_only: bool,
112125
) -> Result<usize> {
113126
if path.to_str() != Some("-") {
114-
let action = if check { "checking" } else { "formatting" };
127+
let action = if check_only { "checking" } else { "formatting" };
115128
println!(
116129
"{action_colored} `{path}`",
117130
action_colored = if no_color {
@@ -147,7 +160,7 @@ fn format_document(
147160
let formatter = Formatter::new(config);
148161
let formatted = formatter.format(&document)?;
149162

150-
if check {
163+
if check_only {
151164
if formatted != source {
152165
print!("{}", StrComparison::new(&source, &formatted));
153166
return Ok(1);
@@ -156,12 +169,9 @@ fn format_document(
156169
return Ok(0);
157170
}
158171

159-
if overwrite {
160-
fs::write(path, formatted)
161-
.with_context(|| format!("failed to write `{path}`", path = path.display()))?;
162-
} else {
163-
print!("{formatted}");
164-
}
172+
// write file because check is not true
173+
fs::write(path, formatted)
174+
.with_context(|| format!("failed to write `{path}`", path = path.display()))?;
165175

166176
Ok(0)
167177
}
@@ -188,10 +198,6 @@ pub fn format(args: FormatArgs) -> Result<()> {
188198

189199
let mut diagnostics = 0;
190200
if args.path.to_str() != Some("-") && args.path.is_dir() {
191-
if !args.overwrite && !args.check {
192-
bail!("formatting a directory requires the `--overwrite` or `--check` option");
193-
}
194-
195201
for entry in WalkDir::new(&args.path) {
196202
let entry = entry.with_context(|| {
197203
format!(
@@ -207,20 +213,18 @@ pub fn format(args: FormatArgs) -> Result<()> {
207213
diagnostics += format_document(
208214
config,
209215
path,
210-
args.overwrite,
211216
args.report_mode,
212217
args.no_color,
213-
args.check,
218+
args.mode.check,
214219
)?;
215220
}
216221
} else {
217222
diagnostics += format_document(
218223
config,
219224
&args.path,
220-
args.overwrite,
221225
args.report_mode,
222226
args.no_color,
223-
args.check,
227+
args.mode.check,
224228
)?;
225229
}
226230

0 commit comments

Comments
 (0)