Skip to content

Commit

Permalink
update: version 1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaro17f committed Jan 18, 2024
1 parent 8b2b6f4 commit 03f7e60
Show file tree
Hide file tree
Showing 22 changed files with 822 additions and 463 deletions.
273 changes: 193 additions & 80 deletions Cargo.lock

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wrestic"
version = "1.3.0"
version = "1.5.0"
authors = ["alvaro17f"]
description = "Restic wrapper built in Rust"
homepage = "https://wrestic.com/"
Expand All @@ -10,19 +10,20 @@ edition = "2021"
keywords = ["restic", "wrapper", "rust", "backup", "tool"]

[dependencies]
anyhow = "1.0.72"
clap = { version = "4.4.6", features = ["derive"] }
clap_complete = "4.4.1"
cmd_lib = "1.3.0"
color-print = "0.3.4"
config = "0.13.3"
anyhow = "1.0.79"
clap = { version = "4.4.18", features = ["derive"] }
clap_complete = "4.4.7"
cmd_lib = "1.9.3"
color-print = "0.3.5"
config = "0.13.4"
dialoguer = "0.11.0"
flate2 = "1.0.27"
indicatif = "0.17.6"
flate2 = "1.0.28"
indicatif = "0.17.7"
lazy_static = "1.4.0"
nix = { version = "0.27.1", features = ["user"] }
regex = "1.9.3"
serde = "1.0.183"
serde_json = "1.0.104"
regex = "1.10.2"
serde = "1.0.195"
serde_json = "1.0.111"
sudo = "0.6.0"
tar = "0.4.40"
which = "4.4.0"
which = "6.0.0"
93 changes: 52 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
mod modules;
mod utils;

use crate::utils::{completions::set_completions, tools::clear};
use crate::utils::{
completions::set_completions, set_environment_variables::set_environment_variables,
tools::clear,
};
use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
Expand All @@ -12,10 +15,9 @@ use modules::{
initialize::initialize, repair::repair, restore::restore, selector::selector,
snapshots::snapshots, update::update,
};
use std::{env, process::exit};
use std::process::exit;
use utils::{
completions::print_completions, get_config::get_config, restic_checker::restic_checker,
root_checker::root_checker,
};

#[derive(Parser, Debug, PartialEq)]
Expand Down Expand Up @@ -61,23 +63,22 @@ enum Commands {
Custom { args: Vec<String> },
}

fn main() -> Result<()> {
restic_checker()?;
root_checker()?;

let cli = Cli::parse();
if let Some(generator) = cli.generator {
fn handle_completions(cli: &Cli) -> Result<()> {
if let Some(generator) = cli.generator.as_ref() {
let mut cmd = Cli::command();
if generator == Shell::Zsh || generator == Shell::Bash {
set_completions(generator, &mut cmd);
if generator == &Shell::Zsh || generator == &Shell::Bash || generator == &Shell::Fish {
set_completions(*generator, &mut cmd)?;
cprintln!("<c>{}</c> <y>completions are set", generator);
exit(0)
} else {
print_completions(generator, &mut cmd);
print_completions(*generator, &mut cmd);
exit(0)
}
}
Ok(())
}

fn handle_commands(cli: &Cli) -> Result<()> {
match &cli.commands {
Some(Commands::Backup) => {
backup(true)?;
Expand All @@ -98,40 +99,13 @@ fn main() -> Result<()> {
check(true)?;
}
Some(Commands::Repair) => {
let settings = get_config()?;
clear()?;
cprintln!("<c,u,s>REPAIR");
println!();
let selection = if settings.len() > 1 {
let selections: Vec<String> = settings.iter().map(|x| x.name.to_owned()).collect();
Select::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>Where do you want to perform a repair?"))
.default(0)
.max_length(10)
.items(&selections[..])
.interact()?
} else {
0
};

env::set_var("USER", &settings[selection].user);
env::set_var("RESTIC_PASSWORD", &settings[selection].restic_password);
for env in &settings[selection].env {
for (key, value) in env {
env::set_var(key, value);
}
}

let backend = &settings[selection].backend;
let repository = &settings[selection].repository;

repair(backend, repository, true)?;
handle_repair()?;
}
Some(Commands::Cache) => {
cache(true)?;
}
Some(Commands::Update) => {
update(true)?;
update()?;
}
Some(Commands::Custom { args }) => {
custom(args)?;
Expand All @@ -140,6 +114,43 @@ fn main() -> Result<()> {
selector()?;
}
}
Ok(())
}

fn handle_repair() -> Result<()> {
clear()?;
cprintln!("<c,u,s>REPAIR");
println!();

let settings = get_config()?;

let selection = if settings.len() > 1 {
let selections: Vec<String> = settings.iter().map(|x| x.name.to_owned()).collect();
Select::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!("<y>Where do you want to perform a repair?"))
.default(0)
.max_length(10)
.items(&selections[..])
.interact()?
} else {
0
};

set_environment_variables(&settings[selection])?;

let backend = &settings[selection].backend;
let repository = &settings[selection].repository;

repair(backend, repository, true)?;
Ok(())
}

fn main() -> Result<()> {
restic_checker()?;

let cli = Cli::parse();
handle_completions(&cli)?;
handle_commands(&cli)?;

Ok(())
}
69 changes: 29 additions & 40 deletions src/modules/backup.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
use crate::{
modules::{repair::repair, selector::selector},
utils::{
get_config::get_config,
get_config::{get_config, Settings},
root_checker::root_checker,
set_environment_variables::set_environment_variables,
tools::{clear, pause},
},
};
use anyhow::Result;
use cmd_lib::run_cmd;
use color_print::{cformat, cprintln};
use dialoguer::{theme::ColorfulTheme, Confirm, Select};
use std::env;

fn do_backup(backend: &str, repository: &str, backup_folder: &str, keep_last: &str) -> Result<()> {
fn do_backup(setting: &Settings) -> Result<()> {
root_checker()?;

let backend = &setting.backend;
let repository = &setting.repository;
let backup_folder = &setting.backup_folder;
let keep_last = &setting.keep_last;

if run_cmd!(
restic -r $backend:$repository --verbose --verbose backup $backup_folder;
sudo -E restic -r $backend:$repository --verbose --verbose backup $backup_folder 2>/dev/null;
)
.is_err()
{
cprintln!("<r>Failed to backup");
cprintln!("\n<r>Failed to backup\n");
}

if run_cmd!(
restic -r $backend:$repository --verbose --verbose forget --keep-last $keep_last;
sudo -E restic -r $backend:$repository --verbose --verbose forget --keep-last $keep_last 2>/dev/null;
)
.is_err()
{
cprintln!("<r>Failed to delete keeping last {keep_last} snapshots.");
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)
Expand All @@ -34,13 +42,13 @@ fn do_backup(backend: &str, repository: &str, backup_folder: &str, keep_last: &s
repair(backend, repository, true)?;

if run_cmd!(
restic -r $backend:$repository --verbose --verbose forget --keep-last $keep_last;
sudo -E restic -r $backend:$repository --verbose --verbose forget --keep-last $keep_last 2>/dev/null;
)
.is_err()
{
cprintln!(
"<r>Houston, we have a problem! Failed to delete keeping last {keep_last} snapshots AGAIN."
)
"\n<r>Houston, we have a problem! Failed to delete old snapshots keeping last {keep_last} AGAIN\n"
)
}
}
}
Expand All @@ -49,27 +57,16 @@ fn do_backup(backend: &str, repository: &str, backup_folder: &str, keep_last: &s
}

pub fn backup(noconfirm: bool) -> Result<()> {
let settings = get_config()?;
clear()?;
cprintln!("<c,u,s>BACKUP");
println!();

if noconfirm {
for conf in settings {
let backend = &conf.backend;
let repository = &conf.repository;
let keep_last = &conf.keep_last;
let backup_folder = &conf.backup_folder;

env::set_var("USER", &conf.user);
env::set_var("RESTIC_PASSWORD", &conf.restic_password);
for env in &conf.env {
for (key, value) in env {
env::set_var(key, value);
}
}
let settings = get_config()?;

do_backup(backend, repository, backup_folder, keep_last)?;
if noconfirm {
for setting in settings {
set_environment_variables(&setting)?;
do_backup(&setting)?;
}
} else {
let selection = if settings.len() > 1 {
Expand All @@ -84,27 +81,19 @@ pub fn backup(noconfirm: bool) -> Result<()> {
0
};

let name = &settings[selection].name;
let backend = &settings[selection].backend;
let repository = &settings[selection].repository;
let keep_last = &settings[selection].keep_last;
let backup_folder = &settings[selection].backup_folder;
let setting = &settings[selection];

set_environment_variables(setting)?;

env::set_var("USER", &settings[selection].user);
env::set_var("RESTIC_PASSWORD", &settings[selection].restic_password);
for env in &settings[selection].env {
for (key, value) in env {
env::set_var(key, value);
}
}
if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(cformat!(
"<y>Do you want to perform a backup for {name}? (Y/n): "
"<y>Do you want to perform a backup for {}? (Y/n): ",
setting.name
))
.default(true)
.interact()?
{
do_backup(backend, repository, backup_folder, keep_last)?;
do_backup(setting)?;
pause()?;
}
if !noconfirm {
Expand Down
30 changes: 22 additions & 8 deletions src/modules/cache.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
use crate::{
modules::selector::selector,
utils::tools::{clear, pause},
utils::{
root_checker::root_checker,
tools::{clear, pause},
},
};
use anyhow::Result;
use cmd_lib::run_cmd;
use color_print::{cformat, cprintln};
use dialoguer::{theme::ColorfulTheme, Confirm};

fn clean_cache() -> Result<()> {
root_checker()?;

if run_cmd!(
sudo -E restic cache --cleanup
)
.is_err()
{
cprintln!("\n<r>Failed to clean cache\n");
}

Ok(())
}

pub fn cache(noconfirm: bool) -> Result<()> {
clear()?;
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 run_cmd!(
restic cache --cleanup
)
.is_err()
{
cprintln!("<r>Failed to clean cache");
}
clean_cache()?;

if !noconfirm {
pause()?;
selector()?;
}
} else {
selector()?;
}

Ok(())
}
Loading

0 comments on commit 03f7e60

Please sign in to comment.