mirror of
https://github.com/rustic-rs/rustic.git
synced 2025-10-26 11:18:51 +00:00
add helpers.rs
This commit is contained in:
parent
571312363e
commit
f95fb70a1c
@ -28,7 +28,6 @@ pub(super) fn execute(
|
||||
) -> Result<()> {
|
||||
let config = ConfigFile::from_backend_no_id(be)?;
|
||||
|
||||
let poly = "37ffea04120bf1";
|
||||
let poly = u64::from_str_radix(config.chunker_polynomial(), 16)?;
|
||||
backup_file(opts.sources, &poly, be, key)?;
|
||||
Ok(())
|
||||
|
||||
31
src/commands/helpers.rs
Normal file
31
src/commands/helpers.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use rpassword::{prompt_password_stderr, read_password_with_reader};
|
||||
|
||||
use crate::backend::ReadBackend;
|
||||
use crate::crypto::Key;
|
||||
use crate::repo::find_key_in_backend;
|
||||
|
||||
const MAX_PASSWORD_RETRIES: usize = 5;
|
||||
|
||||
pub fn get_key(be: &impl ReadBackend, password_file: Option<PathBuf>) -> Result<Key> {
|
||||
let key = match password_file {
|
||||
None => (0..MAX_PASSWORD_RETRIES)
|
||||
.map(|_| {
|
||||
let pass = prompt_password_stderr("enter repository password: ")?;
|
||||
find_key_in_backend(be, &pass, None)
|
||||
})
|
||||
.find(Result::is_ok)
|
||||
.unwrap_or_else(|| bail!("tried too often...aborting!"))?,
|
||||
Some(file) => {
|
||||
let mut file = BufReader::new(File::open(file)?);
|
||||
let pass = read_password_with_reader(Some(&mut file))?;
|
||||
find_key_in_backend(be, &pass, None)?
|
||||
}
|
||||
};
|
||||
eprintln!("password is correct");
|
||||
Ok(key)
|
||||
}
|
||||
@ -1,22 +1,22 @@
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use anyhow::Result;
|
||||
use clap::{Parser, Subcommand};
|
||||
use rpassword::{prompt_password_stderr, read_password_with_reader};
|
||||
|
||||
use crate::backend::{DecryptBackend, LocalBackend};
|
||||
use crate::repo;
|
||||
|
||||
mod backup;
|
||||
mod cat;
|
||||
mod check;
|
||||
mod diff;
|
||||
mod helpers;
|
||||
mod list;
|
||||
mod ls;
|
||||
mod restore;
|
||||
mod snapshots;
|
||||
|
||||
use helpers::get_key;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(about, version)]
|
||||
struct Opts {
|
||||
@ -59,28 +59,11 @@ enum Command {
|
||||
Restore(restore::Opts),
|
||||
}
|
||||
|
||||
const MAX_PASSWORD_RETRIES: usize = 5;
|
||||
|
||||
pub fn execute() -> Result<()> {
|
||||
let args = Opts::parse();
|
||||
|
||||
let be = LocalBackend::new(&args.repository);
|
||||
|
||||
let key = match args.password_file {
|
||||
None => (0..MAX_PASSWORD_RETRIES)
|
||||
.map(|_| {
|
||||
let pass = prompt_password_stderr("enter repository password: ")?;
|
||||
repo::find_key_in_backend(&be, &pass, None)
|
||||
})
|
||||
.find(Result::is_ok)
|
||||
.unwrap_or_else(|| bail!("tried too often...aborting!"))?,
|
||||
Some(file) => {
|
||||
let pass = fs::read_to_string(file)?.replace("\n", "");
|
||||
repo::find_key_in_backend(&be, &pass, None)?
|
||||
}
|
||||
};
|
||||
eprintln!("password is correct");
|
||||
|
||||
let key = get_key(&be, args.password_file)?;
|
||||
let dbe = DecryptBackend::new(&be, key.clone());
|
||||
|
||||
match args.command {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user