use abscissa_core::{status_err, Command, Runnable, Shutdown};
use anyhow::{bail, Result};
use dialoguer::Password;
use crate::{repository::CliRepo, Application, RUSTIC_APP};
use rustic_core::{ConfigOptions, KeyOptions, OpenStatus, Repository};
#[derive(clap::Parser, Command, Debug)]
pub(crate) struct InitCmd {
#[clap(flatten, next_help_heading = "Key options")]
key_opts: KeyOptions,
#[clap(flatten, next_help_heading = "Config options")]
config_opts: ConfigOptions,
}
impl Runnable for InitCmd {
fn run(&self) {
if let Err(err) = RUSTIC_APP
.config()
.repository
.run(|repo| self.inner_run(repo))
{
status_err!("{}", err);
RUSTIC_APP.shutdown(Shutdown::Crash);
};
}
}
impl InitCmd {
fn inner_run(&self, repo: CliRepo) -> Result<()> {
let config = RUSTIC_APP.config();
if repo.config_id()?.is_some() {
bail!("Config file already exists. Aborting.");
}
if config.global.dry_run {
bail!(
"cannot initialize repository {} in dry-run mode!",
repo.name
);
}
let _ = init(repo.0, &self.key_opts, &self.config_opts)?;
Ok(())
}
}
pub(crate) fn init<P, S>(
repo: Repository<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")
.allow_empty_password(true)
.with_confirmation("confirm password", "passwords do not match")
.interact()
{
Ok(it) => it,
Err(err) => {
status_err!("{}", err);
RUSTIC_APP.shutdown(Shutdown::Crash);
}
}
});
Ok(pass)
}