From 9243bc30891fc9db615560da8af83cf715664d9a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 10:10:20 +0300 Subject: refactor: have ClientConfigWithoutPasswords know its filename This allows Init::run to not need to be given it as an argument. --- src/bin/obnam.rs | 3 +-- src/cmd/init.rs | 9 ++------- src/config.rs | 2 ++ 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 0f3da30..4f4cd5b 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -27,9 +27,8 @@ fn main() -> anyhow::Result<()> { debug!("{:?}", opt); debug!("configuration: {:#?}", config); - let cfgname = config_filename(&opt); let result = if let Command::Init(init) = opt.cmd { - init.run(config.config(), &cfgname) + init.run(config.config()) } else { let config = load_config_with_passwords(&opt)?; match opt.cmd { diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 462174e..cb61fba 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,7 +1,6 @@ use crate::config::ClientConfigWithoutPasswords; use crate::error::ObnamError; use crate::passwords::{passwords_filename, Passwords}; -use std::path::Path; use structopt::StructOpt; const PROMPT: &str = "Obnam passphrase: "; @@ -13,11 +12,7 @@ pub struct Init { } impl Init { - pub fn run( - &self, - config: &ClientConfigWithoutPasswords, - config_filename: &Path, - ) -> Result<(), ObnamError> { + pub fn run(&self, config: &ClientConfigWithoutPasswords) -> Result<(), ObnamError> { if !config.encrypt { panic!("no encryption specified"); } @@ -28,7 +23,7 @@ impl Init { }; let passwords = Passwords::new(&passphrase); - let filename = passwords_filename(config_filename); + let filename = passwords_filename(&config.filename); passwords .save(&filename) .map_err(|err| ObnamError::PasswordSave(filename, err))?; diff --git a/src/config.rs b/src/config.rs index 41ddcd6..d6ffbc5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -52,6 +52,7 @@ impl ClientConfig { #[derive(Debug, Serialize, Clone)] pub struct ClientConfigWithoutPasswords { + pub filename: PathBuf, pub server_url: String, pub verify_tls_cert: bool, pub chunk_size: usize, @@ -92,6 +93,7 @@ impl ClientConfigWithoutPasswords { let encrypt = tentative.encrypt.or(Some(false)).unwrap(); let config = Self { + filename: filename.to_path_buf(), server_url: tentative.server_url, roots: tentative.roots, verify_tls_cert: tentative.verify_tls_cert.or(Some(false)).unwrap(), -- cgit v1.2.1