diff --git a/Cargo.lock b/Cargo.lock index f29210f..8ccff4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -78,9 +78,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.80" +version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ad32ce52e4161730f7098c077cd2ed6229b5804ccf99e5366be1ab72a98b4e1" +checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" [[package]] name = "async-trait" @@ -134,9 +134,9 @@ checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" [[package]] name = "clap" -version = "4.5.1" +version = "4.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c918d541ef2913577a0f9566e9ce27cb35b6df072075769e0b26cb5a554520da" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" dependencies = [ "clap_builder", "clap_derive", @@ -144,9 +144,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.1" +version = "4.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f3e7391dad68afb0c2ede1bf619f579a3dc9c2ec67f089baa397123a2f3d1eb" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" dependencies = [ "anstream", "anstyle", @@ -165,9 +165,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.0" +version = "4.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" dependencies = [ "heck", "proc-macro2", @@ -429,9 +429,9 @@ dependencies = [ [[package]] name = "heck" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "hermit-abi" @@ -719,9 +719,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.3" +version = "1.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" dependencies = [ "aho-corasick", "memchr", @@ -808,9 +808,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.114" +version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" dependencies = [ "itoa", "ryu", @@ -978,15 +978,14 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "which" -version = "6.0.0" +version = "6.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fa5e0c10bf77f44aac573e498d1a82d5fbd5e91f6fc0a99e7be4b38e85e101c" +checksum = "8211e4f58a2b2805adfbefbc07bab82958fc91e3836339b1ab7ae32465dce0d7" dependencies = [ "either", "home", - "once_cell", "rustix", - "windows-sys 0.52.0", + "winsafe", ] [[package]] @@ -1218,9 +1217,15 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +[[package]] +name = "winsafe" +version = "0.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" + [[package]] name = "wrestic" -version = "1.6.0" +version = "1.6.1" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index f35e1d7..9b22636 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wrestic" -version = "1.6.0" +version = "1.6.1" authors = ["alvaro17f"] description = "Restic wrapper built in Rust" homepage = "https://wrestic.com/" @@ -10,8 +10,8 @@ edition = "2021" keywords = ["restic", "wrapper", "rust", "backup", "tool"] [dependencies] -anyhow = "1.0.80" -clap = { version = "4.5.1", features = ["derive"] } +anyhow = "1.0.81" +clap = { version = "4.5.4", features = ["derive"] } clap_complete = "4.5.1" cmd_lib = "1.9.3" color-print = "0.3.5" @@ -21,9 +21,9 @@ flate2 = "1.0.28" indicatif = "0.17.8" lazy_static = "1.4.0" nix = { version = "0.28.0", features = ["user"] } -regex = "1.10.3" +regex = "1.10.4" serde = "1.0.197" -serde_json = "1.0.114" +serde_json = "1.0.115" sudo = "0.6.0" tar = "0.4.40" -which = "6.0.0" +which = "6.0.1" diff --git a/src/main.rs b/src/main.rs index 02b39f3..eaf8744 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,7 +114,7 @@ fn handle_repair() -> Result<()> { let settings = get_config()?; let selection = if settings.len() > 1 { - let selections: Vec = settings.iter().map(|x| x.name.to_owned()).collect(); + let selections: Vec = settings.iter().map(|x| x.name.to_string()).collect(); Select::with_theme(&ColorfulTheme::default()) .with_prompt(cformat!("Where do you want to perform a repair?")) .default(0) diff --git a/src/modules/backup.rs b/src/modules/backup.rs index 078de83..85f54e3 100644 --- a/src/modules/backup.rs +++ b/src/modules/backup.rs @@ -4,13 +4,13 @@ use crate::{ get_config::{get_config, Settings}, root_checker::root_checker, set_environment_variables::set_environment_variables, - tools::{clear, pause}, + tools::{clear, confirm, pause}, }, }; use anyhow::Result; use cmd_lib::run_cmd; use color_print::{cformat, cprintln}; -use dialoguer::{theme::ColorfulTheme, Confirm, Select}; +use dialoguer::{theme::ColorfulTheme, Select}; fn do_backup(setting: &Settings) -> Result<()> { root_checker()?; @@ -34,10 +34,8 @@ fn do_backup(setting: &Settings) -> Result<()> { .is_err() { cprintln!("\nFailed to delete old snapshots keeping last {keep_last}\n"); - if Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt(cformat!("Do you want to repair? (Y/n):")) - .default(true) - .interact()? + + if confirm("Do you want to repair? (Y/n): ", true) { repair(backend, repository, true)?; @@ -70,7 +68,7 @@ pub fn backup(noconfirm: bool) -> Result<()> { } } else { let selection = if settings.len() > 1 { - let selections: Vec = settings.iter().map(|x| x.name.to_owned()).collect(); + let selections: Vec = settings.iter().map(|x| x.name.to_string()).collect(); Select::with_theme(&ColorfulTheme::default()) .with_prompt(cformat!("Where do you want to perform a backup?")) .default(0) @@ -85,14 +83,13 @@ pub fn backup(noconfirm: bool) -> Result<()> { set_environment_variables(setting)?; - if Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt(cformat!( - "Do you want to perform a backup for {}? (Y/n): ", + if confirm( + &format!( + "Do you want to perform a backup for {}? (Y/n): ", setting.name - )) - .default(true) - .interact()? - { + ), + true, + ) { do_backup(setting)?; pause()?; } diff --git a/src/modules/cache.rs b/src/modules/cache.rs index 30c4283..e420082 100644 --- a/src/modules/cache.rs +++ b/src/modules/cache.rs @@ -2,13 +2,12 @@ use crate::{ modules::selector::selector, utils::{ root_checker::root_checker, - tools::{clear, pause}, + tools::{clear, confirm, pause}, }, }; use anyhow::Result; use cmd_lib::run_cmd; -use color_print::{cformat, cprintln}; -use dialoguer::{theme::ColorfulTheme, Confirm}; +use color_print::cprintln; fn clean_cache() -> Result<()> { root_checker()?; @@ -29,12 +28,7 @@ pub fn cache(noconfirm: bool) -> Result<()> { cprintln!("CACHE"); println!(); - if noconfirm - || Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt(cformat!("Do you want to clean cache? (Y/n): ")) - .default(true) - .interact()? - { + if noconfirm || confirm("Do you want to clean cache? (Y/n): ", true) { clean_cache()?; if !noconfirm { diff --git a/src/modules/check.rs b/src/modules/check.rs index 8f2715c..5f8ee5e 100644 --- a/src/modules/check.rs +++ b/src/modules/check.rs @@ -4,13 +4,13 @@ use crate::{ get_config::get_config, root_checker::root_checker, set_environment_variables::set_environment_variables, - tools::{clear, pause}, + tools::{clear, confirm, pause}, }, }; use anyhow::Result; use cmd_lib::run_cmd; use color_print::{cformat, cprintln}; -use dialoguer::{theme::ColorfulTheme, Confirm, Select}; +use dialoguer::{theme::ColorfulTheme, Select}; fn do_check(backend: &str, repository: &str) -> Result<()> { root_checker()?; @@ -21,11 +21,8 @@ fn do_check(backend: &str, repository: &str) -> Result<()> { .is_err() { cprintln!("\nFailed to check\n"); - if Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt(cformat!("Do you want to repair? (Y/n):")) - .default(true) - .interact()? - { + + if confirm("Do you want to repair? (Y/n): ", true) { repair(backend, repository, true)?; pause()?; } @@ -41,7 +38,7 @@ pub fn check(noconfirm: bool) -> Result<()> { let settings = get_config()?; let selection = if settings.len() > 1 { - let selections: Vec = settings.iter().map(|x| x.name.to_owned()).collect(); + let selections: Vec = settings.iter().map(|x| x.name.to_string()).collect(); Select::with_theme(&ColorfulTheme::default()) .with_prompt(cformat!("Where do you want to check?")) .default(0) diff --git a/src/modules/custom.rs b/src/modules/custom.rs index 36806fb..1e2f637 100644 --- a/src/modules/custom.rs +++ b/src/modules/custom.rs @@ -40,7 +40,7 @@ pub fn custom(args: &Vec) -> Result<()> { let settings = get_config()?; let selection = if settings.len() > 1 { - let selections: Vec = settings.iter().map(|x| x.name.to_owned()).collect(); + let selections: Vec = settings.iter().map(|x| x.name.to_string()).collect(); Select::with_theme(&ColorfulTheme::default()) .with_prompt(cformat!("Where do you want to work?")) .default(0) diff --git a/src/modules/delete.rs b/src/modules/delete.rs index a7a74b1..f5c7a3d 100644 --- a/src/modules/delete.rs +++ b/src/modules/delete.rs @@ -5,13 +5,13 @@ use crate::{ root_checker::root_checker, set_environment_variables::set_environment_variables, snapshots_selector::snapshots_selector, - tools::{clear, pause}, + tools::{clear, confirm, pause}, }, }; use anyhow::Result; use cmd_lib::run_cmd; use color_print::{cformat, cprintln}; -use dialoguer::{theme::ColorfulTheme, Confirm, Select}; +use dialoguer::{theme::ColorfulTheme, Select}; fn delete_snapshot(backend: &str, repository: &str, delete_snapshots: &str) -> Result<()> { root_checker()?; @@ -23,11 +23,7 @@ fn delete_snapshot(backend: &str, repository: &str, delete_snapshots: &str) -> R { cprintln!("\nFailed to delete snapshot!\n"); - if Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt(cformat!("Do you want to repair? (Y/n):")) - .default(true) - .interact()? - { + if confirm("Do you want to repair? (Y/n): ", true) { repair(backend, repository, true)?; if run_cmd!( sudo -E restic -r $backend:$repository forget $delete_snapshots; @@ -51,7 +47,7 @@ pub fn delete(noconfirm: bool) -> Result<()> { let settings = get_config()?; let selection = if settings.len() > 1 { - let selections: Vec = settings.iter().map(|x| x.name.to_owned()).collect(); + let selections: Vec = settings.iter().map(|x| x.name.to_string()).collect(); Select::with_theme(&ColorfulTheme::default()) .with_prompt(cformat!("What snapshot do you want to delete?")) .default(0) @@ -68,13 +64,10 @@ pub fn delete(noconfirm: bool) -> Result<()> { let repository = &settings[selection].repository; let delete_snapshots = snapshots_selector(backend, repository)?; - if Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt(cformat!( - "Do you want to delete the snapshot with ID {delete_snapshots}? (Y/n): " - )) - .default(true) - .interact()? - { + if confirm( + &format!("Do you want to delete the snapshot with ID {delete_snapshots}? (Y/n): "), + true, + ) { delete_snapshot(backend, repository, &delete_snapshots)?; } diff --git a/src/modules/initialize.rs b/src/modules/initialize.rs index 2470080..6bfdb18 100644 --- a/src/modules/initialize.rs +++ b/src/modules/initialize.rs @@ -4,13 +4,12 @@ use crate::{ get_config::get_config, root_checker::root_checker, set_environment_variables::set_environment_variables, - tools::{clear, pause}, + tools::{clear, confirm, pause}, }, }; use anyhow::Result; use cmd_lib::run_cmd; -use color_print::{cformat, cprintln}; -use dialoguer::{theme::ColorfulTheme, Confirm}; +use color_print::cprintln; use indicatif::ProgressBar; use std::time::Duration; @@ -42,13 +41,7 @@ pub fn initialize(noconfirm: bool) -> Result<()> { let settings = get_config()?; - if Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt(cformat!( - "Do you want to initialize all repositories? (Y/n): " - )) - .default(true) - .interact()? - { + if confirm("Do you want to initialize all repositories? (Y/n): ", true) { for conf in settings { set_environment_variables(&conf)?; diff --git a/src/modules/repair.rs b/src/modules/repair.rs index 80462dd..9fcf293 100644 --- a/src/modules/repair.rs +++ b/src/modules/repair.rs @@ -1,11 +1,13 @@ use crate::{ modules::selector::selector, - utils::{root_checker::root_checker, tools::pause}, + utils::{ + root_checker::root_checker, + tools::{confirm, pause}, + }, }; use anyhow::Result; use cmd_lib::run_cmd; -use color_print::{cformat, cprintln}; -use dialoguer::{theme::ColorfulTheme, Confirm}; +use color_print::cprintln; fn repair_repository(backend: &str, repository: &str) -> Result<()> { root_checker()?; @@ -25,14 +27,7 @@ fn repair_repository(backend: &str, repository: &str) -> Result<()> { } pub fn repair(backend: &str, repository: &str, noconfirm: bool) -> Result<()> { - if noconfirm - || Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt(cformat!( - "Do you want to repair your repository? (Y/n): " - )) - .default(true) - .interact()? - { + if noconfirm || confirm("Do you want to repair your repository? (Y/n): ", true) { repair_repository(backend, repository)?; if !noconfirm { diff --git a/src/modules/restore.rs b/src/modules/restore.rs index 67aaacd..56156ae 100644 --- a/src/modules/restore.rs +++ b/src/modules/restore.rs @@ -5,13 +5,13 @@ use crate::{ root_checker::root_checker, set_environment_variables::set_environment_variables, snapshots_selector::snapshots_selector, - tools::{clear, pause}, + tools::{clear, confirm, pause}, }, }; use anyhow::Result; use cmd_lib::run_cmd; use color_print::{cformat, cprintln}; -use dialoguer::{theme::ColorfulTheme, Confirm, Select}; +use dialoguer::{theme::ColorfulTheme, Select}; fn do_restore( backend: &str, @@ -44,7 +44,7 @@ pub fn restore(noconfirm: bool) -> Result<()> { let settings = get_config()?; let selection = if settings.len() > 1 { - let selections: Vec = settings.iter().map(|x| x.name.to_owned()).collect(); + let selections: Vec = settings.iter().map(|x| x.name.to_string()).collect(); Select::with_theme(&ColorfulTheme::default()) .with_prompt(cformat!("Where do you want to restore from?")) .default(0) @@ -65,13 +65,10 @@ pub fn restore(noconfirm: bool) -> Result<()> { let restore_snapshot = snapshots_selector(backend, repository)?; let user = &setting.user; - if Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt(cformat!( - "Do you want to restore the snapshot with ID {restore_snapshot}? (Y/n): " - )) - .default(true) - .interact()? - { + if confirm( + &format!("Do you want to restore the snapshot with ID {restore_snapshot}? (Y/n): "), + true, + ) { do_restore(backend, repository, restore_folder, &restore_snapshot, user)?; pause()?; } diff --git a/src/modules/selector.rs b/src/modules/selector.rs index 5398025..b799081 100644 --- a/src/modules/selector.rs +++ b/src/modules/selector.rs @@ -37,7 +37,7 @@ fn handle_repair() -> Result<()> { let settings = get_config()?; let selection = if settings.len() > 1 { - let selections: Vec = settings.iter().map(|x| x.name.to_owned()).collect(); + let selections: Vec = settings.iter().map(|x| x.name.to_string()).collect(); Select::with_theme(&ColorfulTheme::default()) .with_prompt(cformat!("Where do you want to perform a repair?")) .default(0) diff --git a/src/modules/snapshots.rs b/src/modules/snapshots.rs index 67500f9..599bf83 100644 --- a/src/modules/snapshots.rs +++ b/src/modules/snapshots.rs @@ -43,7 +43,7 @@ pub fn snapshots(noconfirm: bool) -> Result<()> { let settings = get_config()?; let selection = if settings.len() > 1 { - let selections: Vec = settings.iter().map(|x| x.name.to_owned()).collect(); + let selections: Vec = settings.iter().map(|x| x.name.to_string()).collect(); Select::with_theme(&ColorfulTheme::default()) .with_prompt(cformat!("Where do you want to list snapshots from?")) .default(0) diff --git a/src/utils/restic_checker.rs b/src/utils/restic_checker.rs index 494f037..b4d2413 100644 --- a/src/utils/restic_checker.rs +++ b/src/utils/restic_checker.rs @@ -1,15 +1,12 @@ -use crate::{ - utils::macros::error, - utils::{ - get_current_shell::get_current_shell, - root_checker::root_checker, - tools::{clear, pause}, - }, +use crate::utils::{ + get_current_shell::get_current_shell, + macros::error, + root_checker::root_checker, + tools::{clear, confirm, pause}, }; use anyhow::Result; use cmd_lib::run_cmd; -use color_print::{cformat, cprintln}; -use dialoguer::{theme::ColorfulTheme, Confirm}; +use color_print::cprintln; use std::process::exit; use which::which; @@ -41,11 +38,7 @@ pub fn restic_checker() -> Result<()> { cprintln!("RESTIC"); println!(); cprintln!("Restic not found\n"); - if Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt(cformat!("Would you like to install Restic? (Y/n):")) - .default(true) - .interact()? - { + if confirm("Would you like to install Restic? (Y/n): ", true) { let shell = get_current_shell()?; Ok(install_restic(&shell, &command)?) } else { diff --git a/src/utils/tools.rs b/src/utils/tools.rs index 401cfa3..b440700 100644 --- a/src/utils/tools.rs +++ b/src/utils/tools.rs @@ -1,5 +1,6 @@ use anyhow::Result; -use color_print::cprintln; +use color_print::{cformat, cprintln}; +use dialoguer::{theme::ColorfulTheme, Confirm}; use std::{ io::{self, BufRead}, process::Command, @@ -16,3 +17,11 @@ pub fn pause() -> Result<()> { clear()?; Ok(()) } + +pub fn confirm(prompt: &str, default_value: bool) -> bool { + Confirm::with_theme(&ColorfulTheme::default()) + .with_prompt(cformat!("{}", prompt)) + .default(default_value) + .interact() + .unwrap_or(false) +}