add possibility to force add file

This commit is contained in:
Dmitriy Pleshevskiy 2024-03-18 23:58:52 +03:00
parent 5b75b9d516
commit 0350484200
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2

View file

@ -1,4 +1,10 @@
use std::{collections::HashMap, env::current_dir, fs, path::PathBuf}; use std::{
collections::HashMap,
env::current_dir,
fs,
io::{BufRead, Write},
path::PathBuf,
};
use clap::Parser; use clap::Parser;
@ -37,6 +43,8 @@ struct AddCommand {
enum Command { enum Command {
Cook(CookCommand), Cook(CookCommand),
Add { Add {
#[clap(short, long)]
force: bool,
#[clap(subcommand)] #[clap(subcommand)]
command: AddModuleCommand, command: AddModuleCommand,
}, },
@ -51,16 +59,70 @@ struct Args {
command: Command, command: Command,
} }
pub fn cook( fn tui_question(question: &str, default: bool) -> bool {
print!("{question}: ({}) ", if default { "Y/n" } else { "y/N" });
std::io::stdout()
.flush()
.expect("Flushing stdout shouldn't fail");
let stdin = std::io::stdin();
let mut handle = stdin.lock();
let value = loop {
let mut buffer = String::new();
handle
.read_line(&mut buffer)
.expect("Input string shouldn't fail");
break match buffer.to_lowercase().trim() {
"" => default,
"y" => true,
"n" => false,
_ => continue,
};
};
value
}
fn cook(
current_dir: PathBuf, current_dir: PathBuf,
cook_files: HashMap<String, String>, cook_files: HashMap<String, String>,
force: bool,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
for (file_name, file_content) in cook_files { for (file_name, file_content) in cook_files {
let file_path = &current_dir.join(file_name); let file_path = &current_dir.join(file_name);
if file_content.trim().is_empty() { let file_exists = file_path.exists();
fs::remove_file(file_path)?; let is_empty_file_content = file_content.trim().is_empty();
println!("{}", file_path.to_str().unwrap());
if !force && file_exists {
if !(tui_question(
&format!(
" > already exists. Do you want to {} it?",
if is_empty_file_content {
"delete"
} else {
"overwrite"
}
),
false,
)) {
println!(" > skipped");
continue;
}
}
if is_empty_file_content {
if file_exists {
fs::remove_file(file_path)?;
println!(" > deleted")
}
} else { } else {
fs::write(file_path, file_content)?; fs::write(file_path, file_content)?;
if file_exists {
println!(" > overwritten")
} else {
println!(" > created")
}
} }
} }
@ -87,9 +149,9 @@ pub fn run() -> Result<(), Box<dyn std::error::Error>> {
cook_files.extend(GitIgnoreModule.cook(cfg.into())); cook_files.extend(GitIgnoreModule.cook(cfg.into()));
} }
cook(current_dir, cook_files) cook(current_dir, cook_files, true)
} }
Command::Add { command } => { Command::Add { force, command } => {
let mut cook_files: HashMap<String, String> = HashMap::new(); let mut cook_files: HashMap<String, String> = HashMap::new();
match command { match command {
AddModuleCommand::Direnv(args) => cook_files.extend(DirenvModule.cook(args.into())), AddModuleCommand::Direnv(args) => cook_files.extend(DirenvModule.cook(args.into())),
@ -101,7 +163,7 @@ pub fn run() -> Result<(), Box<dyn std::error::Error>> {
} }
}; };
cook(current_dir, cook_files) cook(current_dir, cook_files, force)
} }
} }
} }