summaryrefslogtreecommitdiff
path: root/src/cmd/init.rs
blob: f0ddb699ee305af375ce99991176731882578472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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(())
}