fix(commands): Ask for missing password in copy when initializing (#1063)

`rustic copy --init` now supports asking for the destination password
when initializing the destination repository
This commit is contained in:
aawsome 2024-02-14 01:58:19 +01:00 committed by GitHub
parent a477e741bf
commit 85fcf8faab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 4 deletions

View File

@ -1,7 +1,7 @@
//! `copy` subcommand
use crate::{
commands::{get_repository, open_repository},
commands::{get_repository, init::init_password, open_repository},
config::AllRepositoryOptions,
helpers::table_with_titles,
status_err, Application, RUSTIC_APP,
@ -69,6 +69,7 @@ impl CopyCmd {
for target_opt in &config.copy.targets {
let repo_dest = get_repository(target_opt)?;
info!("copying to target {}...", repo_dest.name);
let repo_dest = if self.init && repo_dest.config_id()?.is_none() {
if config.global.dry_run {
error!(
@ -79,13 +80,12 @@ impl CopyCmd {
}
let mut config_dest = repo.config().clone();
config_dest.id = Id::random();
let pass = repo_dest.password()?.unwrap();
let pass = init_password(&repo_dest)?;
repo_dest.init_with_config(&pass, &self.key_opts, config_dest)?
} else {
open_repository(target_opt)?
};
info!("copying to target {}...", repo_dest.name);
if poly != repo_dest.config().poly()? {
bail!("cannot copy to repository with different chunker parameter (re-chunking not implemented)!");
}

View File

@ -83,6 +83,11 @@ pub(crate) fn init<P, S>(
key_opts: &KeyOptions,
config_opts: &ConfigOptions,
) -> Result<Repository<P, OpenStatus>> {
let pass = init_password(&repo)?;
Ok(repo.init_with_password(&pass, key_opts, config_opts)?)
}
pub(crate) fn init_password<P, S>(repo: &Repository<P, S>) -> Result<String> {
let pass = repo.password()?.unwrap_or_else(|| {
match Password::new()
.with_prompt("enter password for new key")
@ -98,5 +103,5 @@ pub(crate) fn init<P, S>(
}
});
Ok(repo.init_with_password(&pass, key_opts, config_opts)?)
Ok(pass)
}