summaryrefslogtreecommitdiff
path: root/src/cmd/init.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-04-09 11:54:19 +0300
committerLars Wirzenius <liw@liw.fi>2021-04-09 17:17:35 +0300
commitd0b0245edbb2f6ed8285358d83b98f3334bf1b12 (patch)
tree988f72832b8a5015f4dbfb49473eba022d089666 /src/cmd/init.rs
parent2d6c1c81bfb1c0be8dfaced1c70e825e46c66430 (diff)
downloadobnam2-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/init.rs')
-rw-r--r--src/cmd/init.rs28
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(())
+}