Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add trailing newline option #5

Merged
merged 1 commit into from
Apr 30, 2023
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
14 changes: 13 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,12 @@ 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 && !formatted.ends_with('\n') {
writeln!(&mut formatted)?;
}

if options.check {
if input != formatted {
return Err(Error::Check);
Expand Down Expand Up @@ -76,6 +82,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 +104,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,
}