Skip to content

Commit

Permalink
feat: fmt command (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
schpet authored Sep 24, 2024
1 parent ebc0142 commit 50f6369
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
11 changes: 11 additions & 0 deletions 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 @@ -26,6 +26,7 @@ serde = { version = "1.0.210", features = ["derive"] }
peg = "0.8.4"
chumsky = "0.9.3"
similar = "2.6.0"
term_size = "0.3.2"

[dev-dependencies]
strip-ansi-escapes = "0.2.0"
Expand Down
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,37 @@ pub fn delete_env_vars(
Ok(updated_lines)
}

pub fn format_env_file(content: &str, prune: bool) -> Result<Vec<parser::Line>, std::io::Error> {
let lines = parser::parser().parse(content).map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("Error parsing .env file: {:?}", e),
)
})?;

let mut key_value_lines: Vec<parser::Line> = lines
.into_iter()
.filter(|line| match line {
parser::Line::KeyValue { value, .. } => !value.is_empty(),
parser::Line::Comment(_) => !prune,
})
.collect();

key_value_lines.sort_by(|a, b| {
if let (
parser::Line::KeyValue { key: key_a, .. },
parser::Line::KeyValue { key: key_b, .. },
) = (a, b)
{
key_a.cmp(key_b)
} else {
std::cmp::Ordering::Equal
}
});

Ok(key_value_lines)
}

fn needs_quoting(value: &str) -> bool {
value.chars().any(|c| {
c.is_whitespace()
Expand Down
59 changes: 53 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,31 @@ use envset::{

fn print_diff(old_content: &str, new_content: &str, use_color: bool) {
let diff = TextDiff::from_lines(old_content, new_content);
let term_width = term_size::dimensions().map(|(w, _)| w).unwrap_or(80);

for change in diff.iter_all_changes() {
if use_color {
match change.tag() {
ChangeTag::Delete => print!("{}", change.to_string().trim_end().on_bright_red()),
ChangeTag::Insert => print!("{}", change.to_string().trim_end().on_bright_green()),
ChangeTag::Delete => {
let line = change.to_string();
let padding = " ".repeat(term_width.saturating_sub(line.trim_end().len()));
print!(
"{}",
(line.trim_end().to_string() + &padding).on_bright_red()
);
println!();
}
ChangeTag::Insert => {
let line = change.to_string();
let padding = " ".repeat(term_width.saturating_sub(line.trim_end().len()));
print!(
"{}",
(line.trim_end().to_string() + &padding).on_bright_green()
);
println!();
}
ChangeTag::Equal => print!("{}", change),
}
// Print a newline after each colored line
if change.tag() != ChangeTag::Equal {
println!();
}
} else {
let sign = match change.tag() {
ChangeTag::Delete => "-",
Expand Down Expand Up @@ -75,6 +88,12 @@ enum Commands {
#[arg(required = true)]
keys: Vec<String>,
},
/// Format the .env file (sort keys and remove empty lines)
Fmt {
/// Remove whole line comments
#[arg(short = 'p', long = "prune")]
prune: bool,
},
}

fn main() {
Expand Down Expand Up @@ -146,6 +165,34 @@ fn main() {
process::exit(1);
}
},
Some(Commands::Fmt { prune }) => match read_env_file_contents(&cli.file) {
Ok(old_content) => match envset::format_env_file(&old_content, *prune) {
Ok(formatted_lines) => {
let mut buffer = Vec::new();
if let Err(e) = print_env_file_contents(&formatted_lines, &mut buffer) {
eprintln!("Error writing formatted .env file contents: {}", e);
process::exit(1);
}
let new_content = String::from_utf8_lossy(&buffer);

let use_color = atty::is(Stream::Stdout);
print_diff(&old_content, &new_content, use_color);

if let Err(e) = std::fs::write(&cli.file, buffer) {
eprintln!("Error writing formatted .env file: {}", e);
process::exit(1);
}
}
Err(e) => {
eprintln!("Error formatting .env file: {}", e);
process::exit(1);
}
},
Err(e) => {
eprintln!("Error reading .env file: {}", e);
process::exit(1);
}
},
None => {}
}

Expand Down

0 comments on commit 50f6369

Please sign in to comment.