diff options
Diffstat (limited to 'src/cmd/init.rs')
-rw-r--r-- | src/cmd/init.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/cmd/init.rs b/src/cmd/init.rs new file mode 100644 index 0000000..f0ddb69 --- /dev/null +++ b/src/cmd/init.rs @@ -0,0 +1,28 @@ +use crate::client::ClientConfigWithoutPasswords; +use crate::error::ObnamError; +use crate::passwords::{passwords_filename, Passwords}; +use std::path::Path; + +const PROMPT: &str = "Obnam passphrase: "; + +pub fn init( + config: &ClientConfigWithoutPasswords, + config_filename: &Path, + 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(), + }; + + let passwords = Passwords::new(&passphrase); + let filename = passwords_filename(config_filename); + passwords + .save(&filename) + .map_err(|err| ObnamError::PasswordSave(filename, err))?; + Ok(()) +} |