diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-04-09 11:54:19 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-04-09 17:17:35 +0300 |
commit | d0b0245edbb2f6ed8285358d83b98f3334bf1b12 (patch) | |
tree | 988f72832b8a5015f4dbfb49473eba022d089666 /src/cmd | |
parent | 2d6c1c81bfb1c0be8dfaced1c70e825e46c66430 (diff) | |
download | obnam2-d0b0245edbb2f6ed8285358d83b98f3334bf1b12.tar.gz |
feat: add "obnam init" subcommand
This reads a passphrase and derives two passwords from that, and
stores them next to the configuration file. The passwords aren't yet
used for anything, that will come later.
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/backup.rs | 2 | ||||
-rw-r--r-- | src/cmd/init.rs | 28 | ||||
-rw-r--r-- | src/cmd/mod.rs | 3 | ||||
-rw-r--r-- | src/cmd/show_config.rs | 2 |
4 files changed, 34 insertions, 1 deletions
diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs index e6781e9..bd36a3a 100644 --- a/src/cmd/backup.rs +++ b/src/cmd/backup.rs @@ -53,6 +53,7 @@ fn initial_backup( info!("fresh backup without a previous generation"); let newtemp = NamedTempFile::new()?; let run = InitialBackup::new(config, &client)?; + let config = config.config(); let mut all_warnings = vec![]; let count = { let mut new = NascentGeneration::create(newtemp.path())?; @@ -79,6 +80,7 @@ fn incremental_backup( info!("incremental backup based on {}", old_ref); let newtemp = NamedTempFile::new()?; let mut run = IncrementalBackup::new(config, &client)?; + let config = config.config(); let mut all_warnings = vec![]; let count = { let oldtemp = NamedTempFile::new()?; 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(()) +} diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index fc517be..70dde59 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,3 +1,6 @@ +mod init; +pub use init::init; + mod backup; pub use backup::backup; diff --git a/src/cmd/show_config.rs b/src/cmd/show_config.rs index b4f4cdc..8acd1c8 100644 --- a/src/cmd/show_config.rs +++ b/src/cmd/show_config.rs @@ -2,6 +2,6 @@ use crate::client::ClientConfig; use crate::error::ObnamError; pub fn show_config(config: &ClientConfig) -> Result<(), ObnamError> { - println!("{}", serde_json::to_string_pretty(config)?); + println!("{}", serde_json::to_string_pretty(&config.config())?); Ok(()) } |