use std::fs;
use std::io::{Read, Write};
use std::path::Path;
pub fn add_line_file(create_backup: &bool, path: &str, line: &str) -> std::io::Result<()> {
if !Path::new(&path).exists() {
fs::write(path, line)?;
}
let mut content = String::new();
fs::File::open(path)?.read_to_string(&mut content)?;
if content.lines().any(|l| l.trim() == line) {
println!("Entry already exists in {}", path);
return Ok(());
}
if *create_backup {
let backup_path = format!("{}.bak", path);
if !Path::new(&backup_path).exists() {
fs::copy(path, &backup_path)?;
println!("Created backup: {}", backup_path);
}
}
let mut file = fs::OpenOptions::new().append(true).open(path)?;
writeln!(file, "{}", line)?;
println!("Entry added to {}", path);
Ok(())
}