Skip to content

Commit

Permalink
feat: add check option to format command (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott8440 authored Jan 9, 2025
1 parent 8e368cf commit 4a5db76
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
25 changes: 24 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ tokio = { version = "1.41.0", features = ["full"] }
tracing-log = "0.2.0"
indicatif = "0.17.8"
clap-verbosity-flag = "2.2.2"
pretty_assertions = "1.4.1"
32 changes: 25 additions & 7 deletions src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use clap::Parser;
use codespan_reporting::files::SimpleFile;
use codespan_reporting::term::emit;
use colored::Colorize;
use pretty_assertions::StrComparison;
use walkdir::WalkDir;
use wdl::ast::Document;
use wdl::ast::Node;
Expand Down Expand Up @@ -70,8 +71,12 @@ pub struct FormatArgs {
pub indentation_size: Option<usize>,

/// Overwrite the WDL documents with the formatted versions
#[arg(long)]
#[arg(long, conflicts_with = "check")]
pub overwrite: bool,

/// Check if files are formatted correctly and print diff if not
#[arg(long, conflicts_with = "overwrite")]
pub check: bool,
}

/// Reads source from the given path.
Expand Down Expand Up @@ -103,14 +108,16 @@ fn format_document(
overwrite: bool,
report_mode: Mode,
no_color: bool,
check: bool,
) -> Result<usize> {
if path.to_str() != Some("-") {
let action = if check { "checking" } else { "formatting" };
println!(
"{formatting} `{path}`",
formatting = if no_color {
"formatting".normal()
"{action_colored} `{path}`",
action_colored = if no_color {
action.normal()
} else {
"formatting".green()
action.green()
},
path = path.display()
);
Expand Down Expand Up @@ -140,6 +147,15 @@ fn format_document(
let formatter = Formatter::new(config);
let formatted = formatter.format(&document)?;

if check {
if formatted != source {
print!("{}", StrComparison::new(&source, &formatted));
return Ok(1);
}
println!("`{path}` is formatted correctly", path = path.display());
return Ok(0);
}

if overwrite {
fs::write(path, formatted)
.with_context(|| format!("failed to write `{path}`", path = path.display()))?;
Expand Down Expand Up @@ -172,8 +188,8 @@ pub fn format(args: FormatArgs) -> Result<()> {

let mut diagnostics = 0;
if args.path.to_str() != Some("-") && args.path.is_dir() {
if !args.overwrite {
bail!("formatting a directory requires the `--overwrite` option");
if !args.overwrite && !args.check {
bail!("formatting a directory requires the `--overwrite` or `--check` option");
}

for entry in WalkDir::new(&args.path) {
Expand All @@ -194,6 +210,7 @@ pub fn format(args: FormatArgs) -> Result<()> {
args.overwrite,
args.report_mode,
args.no_color,
args.check,
)?;
}
} else {
Expand All @@ -203,6 +220,7 @@ pub fn format(args: FormatArgs) -> Result<()> {
args.overwrite,
args.report_mode,
args.no_color,
args.check,
)?;
}

Expand Down

0 comments on commit 4a5db76

Please sign in to comment.