summaryrefslogtreecommitdiff
path: root/src/cmd/init.rs
blob: ebb3d8aec23ca90d791948c6150b0e267bb404d9 (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
29
30
31
32
33
//! The `init` subcommand.

use crate::config::ClientConfig;
use crate::error::ObnamError;
use crate::passwords::{passwords_filename, Passwords};
use structopt::StructOpt;

const PROMPT: &str = "Obnam passphrase: ";

/// Initialize client by setting passwords.
#[derive(Debug, StructOpt)]
pub struct Init {
    /// Only for testing.
    #[structopt(long)]
    insecure_passphrase: Option<String>,
}

impl Init {
    /// Run the command.
    pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> {
        let passphrase = match &self.insecure_passphrase {
            Some(x) => x.to_string(),
            None => rpassword::prompt_password(PROMPT).unwrap(),
        };

        let passwords = Passwords::new(&passphrase);
        let filename = passwords_filename(&config.filename);
        passwords
            .save(&filename)
            .map_err(|err| ObnamError::PasswordSave(filename, err))?;
        Ok(())
    }
}