diff options
Diffstat (limited to 'src/cmd/init.rs')
-rw-r--r-- | src/cmd/init.rs | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/cmd/init.rs b/src/cmd/init.rs index f0ddb69..cb61fba 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,28 +1,32 @@ -use crate::client::ClientConfigWithoutPasswords; +use crate::config::ClientConfigWithoutPasswords; use crate::error::ObnamError; use crate::passwords::{passwords_filename, Passwords}; -use std::path::Path; +use structopt::StructOpt; const PROMPT: &str = "Obnam passphrase: "; -pub fn init( - config: &ClientConfigWithoutPasswords, - config_filename: &Path, +#[derive(Debug, StructOpt)] +pub struct Init { + #[structopt(long)] insecure_passphrase: Option<String>, -) -> Result<(), ObnamError> { - if !config.encrypt { - panic!("no encryption specified"); - } +} - let passphrase = match insecure_passphrase { - Some(x) => x, - None => rpassword::read_password_from_tty(Some(PROMPT)).unwrap(), - }; +impl Init { + pub fn run(&self, config: &ClientConfigWithoutPasswords) -> Result<(), ObnamError> { + if !config.encrypt { + panic!("no encryption specified"); + } - let passwords = Passwords::new(&passphrase); - let filename = passwords_filename(config_filename); - passwords - .save(&filename) - .map_err(|err| ObnamError::PasswordSave(filename, err))?; - Ok(()) + let passphrase = match &self.insecure_passphrase { + Some(x) => x.to_string(), + None => rpassword::read_password_from_tty(Some(PROMPT)).unwrap(), + }; + + let passwords = Passwords::new(&passphrase); + let filename = passwords_filename(&config.filename); + passwords + .save(&filename) + .map_err(|err| ObnamError::PasswordSave(filename, err))?; + Ok(()) + } } |