Skip to content

Commit

Permalink
do not overwrite unconditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
tshepang committed Oct 18, 2022
1 parent 77bfaff commit 19d2632
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 9 deletions.
162 changes: 162 additions & 0 deletions ci/semantic-line-breaks/Cargo.lock

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

4 changes: 4 additions & 0 deletions ci/semantic-line-breaks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ ignore = "0.4"
[dependencies.regex]
version = "1"
features = ["pattern"]

[dependencies.clap]
version = "4"
features = ["derive"]
29 changes: 20 additions & 9 deletions ci/semantic-line-breaks/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use std::{env, fs, process};
use std::{fs, path::PathBuf};

use anyhow::Result;
use clap::Parser;
use ignore::Walk;
use regex::Regex;

#[derive(Parser)]
struct Cli {
root_dir: PathBuf,
#[arg(long)]
overwrite: bool,
}

fn main() -> Result<()> {
let mut args = env::args();
if args.len() == 1 {
eprintln!("error: expected root Markdown directory as CLI argument");
process::exit(1);
}
let root_dir = args.nth(1).unwrap();
for result in Walk::new(root_dir) {
let cli = Cli::parse();
for result in Walk::new(cli.root_dir) {
let entry = result?;
if entry.file_type().expect("no stdin").is_dir() {
continue;
Expand All @@ -27,7 +30,15 @@ fn main() -> Result<()> {
let old = fs::read_to_string(path)?;
let new = comply(&old)?;
if new != old {
fs::write(path, new)?;
if cli.overwrite {
fs::write(path, new)?;
} else {
println!("{}:", path.display());
println!("{new}");
println!();
println!("---");
println!();
}
}
}
Ok(())
Expand Down

0 comments on commit 19d2632

Please sign in to comment.