Skip to content

Commit

Permalink
add confirm function
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaro17f committed Mar 26, 2024
1 parent e0a0a7e commit cf111e6
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 121 deletions.
43 changes: 24 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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/"
Expand All @@ -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"
Expand All @@ -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"
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn handle_repair() -> Result<()> {
let settings = get_config()?;

let selection = if settings.len() > 1 {
let selections: Vec<String> = settings.iter().map(|x| x.name.to_owned()).collect();
let selections: Vec<String> = settings.iter().map(|x| x.name.to_string()).collect();
Select::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>Where do you want to perform a repair?"))
.default(0)
Expand Down
25 changes: 11 additions & 14 deletions src/modules/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand All @@ -34,10 +34,8 @@ fn do_backup(setting: &Settings) -> Result<()> {
.is_err()
{
cprintln!("\n<r>Failed to delete old snapshots keeping last {keep_last}\n");
if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>Do you want to repair? (Y/n):"))
.default(true)
.interact()?

if confirm("Do you want to repair? (Y/n): ", true)
{
repair(backend, repository, true)?;

Expand Down Expand Up @@ -70,7 +68,7 @@ pub fn backup(noconfirm: bool) -> Result<()> {
}
} else {
let selection = if settings.len() > 1 {
let selections: Vec<String> = settings.iter().map(|x| x.name.to_owned()).collect();
let selections: Vec<String> = settings.iter().map(|x| x.name.to_string()).collect();
Select::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>Where do you want to perform a backup?"))
.default(0)
Expand All @@ -85,14 +83,13 @@ pub fn backup(noconfirm: bool) -> Result<()> {

set_environment_variables(setting)?;

if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!(
"<y>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()?;
}
Expand Down
12 changes: 3 additions & 9 deletions src/modules/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand All @@ -29,12 +28,7 @@ pub fn cache(noconfirm: bool) -> Result<()> {
cprintln!("<c,u,s>CACHE");
println!();

if noconfirm
|| Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>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 {
Expand Down
13 changes: 5 additions & 8 deletions src/modules/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand All @@ -21,11 +21,8 @@ fn do_check(backend: &str, repository: &str) -> Result<()> {
.is_err()
{
cprintln!("\n<r>Failed to check\n");
if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>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()?;
}
Expand All @@ -41,7 +38,7 @@ pub fn check(noconfirm: bool) -> Result<()> {
let settings = get_config()?;

let selection = if settings.len() > 1 {
let selections: Vec<String> = settings.iter().map(|x| x.name.to_owned()).collect();
let selections: Vec<String> = settings.iter().map(|x| x.name.to_string()).collect();
Select::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>Where do you want to check?"))
.default(0)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn custom(args: &Vec<String>) -> Result<()> {
let settings = get_config()?;

let selection = if settings.len() > 1 {
let selections: Vec<String> = settings.iter().map(|x| x.name.to_owned()).collect();
let selections: Vec<String> = settings.iter().map(|x| x.name.to_string()).collect();
Select::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>Where do you want to work?"))
.default(0)
Expand Down
23 changes: 8 additions & 15 deletions src/modules/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand All @@ -23,11 +23,7 @@ fn delete_snapshot(backend: &str, repository: &str, delete_snapshots: &str) -> R
{
cprintln!("\n<r>Failed to delete snapshot!\n");

if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>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;
Expand All @@ -51,7 +47,7 @@ pub fn delete(noconfirm: bool) -> Result<()> {
let settings = get_config()?;

let selection = if settings.len() > 1 {
let selections: Vec<String> = settings.iter().map(|x| x.name.to_owned()).collect();
let selections: Vec<String> = settings.iter().map(|x| x.name.to_string()).collect();
Select::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>What snapshot do you want to delete?"))
.default(0)
Expand All @@ -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!(
"<y>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)?;
}

Expand Down
13 changes: 3 additions & 10 deletions src/modules/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -42,13 +41,7 @@ pub fn initialize(noconfirm: bool) -> Result<()> {

let settings = get_config()?;

if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!(
"<y>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)?;

Expand Down
Loading

0 comments on commit cf111e6

Please sign in to comment.