Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ struct Cli {
/// Check if files are already formatted (exit 1 if not)
#[arg(long)]
check: bool,

/// Format files in place
#[arg(short, long = "inplace", conflicts_with = "check")]
inplace: bool,
}

fn format_sql(sql: &str, style: Style) -> Result<String, String> {
libpgfmt::format(sql, style).map_err(|e| e.to_string())
}

fn process(name: &str, sql: &str, cli: &Cli) -> Result<bool, String> {
fn process(name: &str, sql: &str, path: Option<&PathBuf>, cli: &Cli) -> Result<bool, String> {
if sql.trim().is_empty() {
return Ok(true);
}
Expand All @@ -35,6 +39,13 @@ fn process(name: &str, sql: &str, cli: &Cli) -> Result<bool, String> {
eprintln!("Would reformat: {name}");
return Ok(false);
}
} else if cli.inplace {
let path = path.ok_or_else(|| "cannot use --inplace with stdin".to_string())?;
let permissions = fs::metadata(path)
.map_err(|e| format!("{name}: {e}"))?
.permissions();
fs::write(path, &formatted).map_err(|e| format!("{name}: {e}"))?;
fs::set_permissions(path, permissions).map_err(|e| format!("{name}: {e}"))?;
} else {
println!("{formatted}");
}
Expand All @@ -43,6 +54,11 @@ fn process(name: &str, sql: &str, cli: &Cli) -> Result<bool, String> {

fn run() -> Result<ExitCode, String> {
let cli = Cli::parse();

if cli.inplace && cli.files.is_empty() {
return Err("--inplace requires at least one file".to_string());
}

let mut all_ok = true;

if cli.files.is_empty() {
Expand All @@ -53,14 +69,14 @@ fn run() -> Result<ExitCode, String> {
io::stdin()
.read_to_string(&mut sql)
.map_err(|e| e.to_string())?;
if !process("<stdin>", &sql, &cli)? {
if !process("<stdin>", &sql, None, &cli)? {
all_ok = false;
}
} else {
for path in &cli.files {
let sql = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
let name = path.display().to_string();
if !process(&name, &sql, &cli)? {
if !process(&name, &sql, Some(path), &cli)? {
all_ok = false;
}
}
Expand Down
Loading