Skip to content

Commit

Permalink
feat: add trailing newline option
Browse files Browse the repository at this point in the history
  • Loading branch information
sdd committed Apr 22, 2023
1 parent ed92114 commit d192eaa
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Write as FmtWrite;
use std::io::{Read, Write};
use std::process;
use std::{fs, io};
Expand Down Expand Up @@ -41,7 +42,14 @@ fn main() {
let mut input = String::new();
fs::File::open(&path)?.read_to_string(&mut input)?;

let formatted = format(&input, &QueryParams::default(), format_options);
let mut formatted = format(&input, &QueryParams::default(), format_options);

if options.trailing_newline {
if !formatted.ends_with("\n") {
writeln!(&mut formatted)?;
}
}

if options.check {
if input != formatted {
return Err(Error::Check);
Expand Down Expand Up @@ -76,6 +84,8 @@ enum Error {
Patter(#[from] glob::PatternError),
#[error("Input is not formatted correctly. Run without --check to format the input.")]
Check,
#[error("Failed to append a trailing newline to the formatted SQL.")]
Format(#[from] std::fmt::Error),
}

#[derive(Parser)]
Expand All @@ -96,4 +106,8 @@ struct Options {
/// Set the number of line breaks after a query
#[clap(short, long, default_value = "2")]
lines_between_queries: u8,
/// Enforce a tailing newline at the end of the file
#[clap(short = 'n', long, default_value = "false")]
trailing_newline: bool,
}

0 comments on commit d192eaa

Please sign in to comment.