From f3ed6ba027dbb410161970c4a706192885d92a3d Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 9 Apr 2021 20:13:12 +0300 Subject: refactor: move function to better place --- src/bin/obnam.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 2dbbaa2..bb0cbdb 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -53,6 +53,19 @@ fn main() -> anyhow::Result<()> { Ok(()) } +fn setup_logging(filename: &Path) -> anyhow::Result<()> { + let logfile = FileAppender::builder().build(filename)?; + + let config = Config::builder() + .appender(Appender::builder().build("obnam", Box::new(logfile))) + .logger(Logger::builder().build("obnam", LevelFilter::Debug)) + .build(Root::builder().appender("obnam").build(LevelFilter::Debug))?; + + log4rs::init_config(config)?; + + Ok(()) +} + fn load_config_with_passwords(opt: &Opt) -> Result { Ok(ClientConfig::read_with_passwords(&config_filename(opt))?) } @@ -115,16 +128,3 @@ enum Command { }, Config, } - -fn setup_logging(filename: &Path) -> anyhow::Result<()> { - let logfile = FileAppender::builder().build(filename)?; - - let config = Config::builder() - .appender(Appender::builder().build("obnam", Box::new(logfile))) - .logger(Logger::builder().build("obnam", LevelFilter::Debug)) - .build(Root::builder().appender("obnam").build(LevelFilter::Debug))?; - - log4rs::init_config(config)?; - - Ok(()) -} -- cgit v1.2.1 From e13ef6a773755e8eed4d0fb1e55f3bd80a6c5cfd Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:02:27 +0300 Subject: refactor: move ClientConfig into its own module --- src/backup_run.rs | 3 +- src/bin/obnam.rs | 2 +- src/client.rs | 122 +------------------------------------------------ src/cmd/backup.rs | 3 +- src/cmd/get_chunk.rs | 2 +- src/cmd/init.rs | 2 +- src/cmd/list.rs | 3 +- src/cmd/list_files.rs | 2 +- src/cmd/restore.rs | 2 +- src/cmd/show_config.rs | 2 +- src/cmd/show_gen.rs | 2 +- src/config.rs | 122 +++++++++++++++++++++++++++++++++++++++++++++++++ src/error.rs | 3 +- src/lib.rs | 1 + 14 files changed, 140 insertions(+), 131 deletions(-) create mode 100644 src/config.rs diff --git a/src/backup_run.rs b/src/backup_run.rs index e9d094d..e966855 100644 --- a/src/backup_run.rs +++ b/src/backup_run.rs @@ -1,7 +1,8 @@ use crate::backup_progress::BackupProgress; use crate::backup_reason::Reason; use crate::chunkid::ChunkId; -use crate::client::{BackupClient, ClientConfig, ClientError}; +use crate::client::{BackupClient, ClientError}; +use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::FilesystemEntry; use crate::fsiter::{FsIterError, FsIterResult}; diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index bb0cbdb..10048a3 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -2,10 +2,10 @@ use directories_next::ProjectDirs; use log::{debug, error, info, LevelFilter}; use log4rs::append::file::FileAppender; use log4rs::config::{Appender, Config, Logger, Root}; -use obnam::client::ClientConfig; use obnam::cmd::{ backup, get_chunk, init, list, list_files, restore, show_config, show_generation, }; +use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; use structopt::StructOpt; diff --git a/src/client.rs b/src/client.rs index 37bfe91..1b33372 100644 --- a/src/client.rs +++ b/src/client.rs @@ -4,136 +4,18 @@ use crate::chunk::{GenerationChunk, GenerationChunkError}; use crate::chunker::{Chunker, ChunkerError}; use crate::chunkid::ChunkId; use crate::chunkmeta::ChunkMeta; +use crate::config::ClientConfig; use crate::fsentry::{FilesystemEntry, FilesystemKind}; use crate::generation::{FinishedGeneration, LocalGeneration, LocalGenerationError}; use crate::genlist::GenerationList; -use crate::passwords::{passwords_filename, PasswordError, Passwords}; -use bytesize::MIB; use chrono::{DateTime, Local}; use log::{debug, error, info, trace}; use reqwest::blocking::Client; -use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fs::File; use std::io::prelude::*; -use std::path::{Path, PathBuf}; - -const DEFAULT_CHUNK_SIZE: usize = MIB as usize; -const DEVNULL: &str = "/dev/null"; - -#[derive(Debug, Deserialize, Clone)] -#[serde(deny_unknown_fields)] -struct TentativeClientConfig { - server_url: String, - verify_tls_cert: Option, - chunk_size: Option, - roots: Vec, - log: Option, - encrypt: Option, -} - -#[derive(Debug, Serialize, Clone)] -pub enum ClientConfig { - Plain(ClientConfigWithoutPasswords), - WithPasswords(ClientConfigWithoutPasswords, Passwords), -} - -impl ClientConfig { - pub fn read_without_passwords(filename: &Path) -> Result { - let config = ClientConfigWithoutPasswords::read_config(filename)?; - Ok(ClientConfig::Plain(config)) - } - - pub fn read_with_passwords(filename: &Path) -> Result { - let config = ClientConfigWithoutPasswords::read_config(filename)?; - if config.encrypt { - let passwords = Passwords::load(&passwords_filename(filename)) - .map_err(ClientConfigError::PasswordsMissing)?; - Ok(ClientConfig::WithPasswords(config, passwords)) - } else { - Ok(ClientConfig::Plain(config)) - } - } - - pub fn config(&self) -> &ClientConfigWithoutPasswords { - match self { - Self::Plain(config) => &config, - Self::WithPasswords(config, _) => &config, - } - } -} - -#[derive(Debug, Serialize, Clone)] -pub struct ClientConfigWithoutPasswords { - pub server_url: String, - pub verify_tls_cert: bool, - pub chunk_size: usize, - pub roots: Vec, - pub log: PathBuf, - pub encrypt: bool, -} - -#[derive(Debug, thiserror::Error)] -pub enum ClientConfigError { - #[error("server_url is empty")] - ServerUrlIsEmpty, - - #[error("No backup roots in config; at least one is needed")] - NoBackupRoot, - - #[error("server URL doesn't use https: {0}")] - NotHttps(String), - - #[error("No passwords are set: you may need to run 'obnam init': {0}")] - PasswordsMissing(PasswordError), - - #[error(transparent)] - IoError(#[from] std::io::Error), - - #[error(transparent)] - SerdeYamlError(#[from] serde_yaml::Error), -} - -pub type ClientConfigResult = Result; - -impl ClientConfigWithoutPasswords { - pub fn read_config(filename: &Path) -> ClientConfigResult { - trace!("read_config: filename={:?}", filename); - let config = std::fs::read_to_string(filename)?; - let tentative: TentativeClientConfig = serde_yaml::from_str(&config)?; - - let encrypt = tentative.encrypt.or(Some(false)).unwrap(); - - let config = Self { - server_url: tentative.server_url, - roots: tentative.roots, - verify_tls_cert: tentative.verify_tls_cert.or(Some(false)).unwrap(), - chunk_size: tentative.chunk_size.or(Some(DEFAULT_CHUNK_SIZE)).unwrap(), - log: tentative - .log - .or_else(|| Some(PathBuf::from(DEVNULL))) - .unwrap(), - encrypt, - }; - - config.check()?; - Ok(config) - } - - fn check(&self) -> Result<(), ClientConfigError> { - if self.server_url.is_empty() { - return Err(ClientConfigError::ServerUrlIsEmpty); - } - if !self.server_url.starts_with("https://") { - return Err(ClientConfigError::NotHttps(self.server_url.to_string())); - } - if self.roots.is_empty() { - return Err(ClientConfigError::NoBackupRoot); - } - Ok(()) - } -} +use std::path::Path; #[derive(Debug, thiserror::Error)] pub enum ClientError { diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs index bd36a3a..1091241 100644 --- a/src/cmd/backup.rs +++ b/src/cmd/backup.rs @@ -1,7 +1,8 @@ use crate::backup_progress::BackupProgress; use crate::backup_run::{BackupError, IncrementalBackup, InitialBackup}; use crate::chunkid::ChunkId; -use crate::client::{BackupClient, ClientConfig}; +use crate::client::BackupClient; +use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsiter::FsIterator; use crate::generation::NascentGeneration; diff --git a/src/cmd/get_chunk.rs b/src/cmd/get_chunk.rs index 385c4d5..c9d640e 100644 --- a/src/cmd/get_chunk.rs +++ b/src/cmd/get_chunk.rs @@ -1,6 +1,6 @@ use crate::chunkid::ChunkId; use crate::client::BackupClient; -use crate::client::ClientConfig; +use crate::config::ClientConfig; use crate::error::ObnamError; use std::io::{stdout, Write}; diff --git a/src/cmd/init.rs b/src/cmd/init.rs index f0ddb69..fb8a6af 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,4 +1,4 @@ -use crate::client::ClientConfigWithoutPasswords; +use crate::config::ClientConfigWithoutPasswords; use crate::error::ObnamError; use crate::passwords::{passwords_filename, Passwords}; use std::path::Path; diff --git a/src/cmd/list.rs b/src/cmd/list.rs index a3f059b..f48512b 100644 --- a/src/cmd/list.rs +++ b/src/cmd/list.rs @@ -1,4 +1,5 @@ -use crate::client::{BackupClient, ClientConfig}; +use crate::client::BackupClient; +use crate::config::ClientConfig; use crate::error::ObnamError; pub fn list(config: &ClientConfig) -> Result<(), ObnamError> { diff --git a/src/cmd/list_files.rs b/src/cmd/list_files.rs index 71b0d68..d490e85 100644 --- a/src/cmd/list_files.rs +++ b/src/cmd/list_files.rs @@ -1,6 +1,6 @@ use crate::backup_reason::Reason; use crate::client::BackupClient; -use crate::client::ClientConfig; +use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::{FilesystemEntry, FilesystemKind}; use tempfile::NamedTempFile; diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs index 183f207..c2cbb7f 100644 --- a/src/cmd/restore.rs +++ b/src/cmd/restore.rs @@ -1,6 +1,6 @@ use crate::backup_reason::Reason; -use crate::client::ClientConfig; use crate::client::{BackupClient, ClientError}; +use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::{FilesystemEntry, FilesystemKind}; use crate::generation::{LocalGeneration, LocalGenerationError}; diff --git a/src/cmd/show_config.rs b/src/cmd/show_config.rs index 8acd1c8..88ee9ec 100644 --- a/src/cmd/show_config.rs +++ b/src/cmd/show_config.rs @@ -1,4 +1,4 @@ -use crate::client::ClientConfig; +use crate::config::ClientConfig; use crate::error::ObnamError; pub fn show_config(config: &ClientConfig) -> Result<(), ObnamError> { diff --git a/src/cmd/show_gen.rs b/src/cmd/show_gen.rs index 143aed6..5b786cf 100644 --- a/src/cmd/show_gen.rs +++ b/src/cmd/show_gen.rs @@ -1,5 +1,5 @@ use crate::client::BackupClient; -use crate::client::ClientConfig; +use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::FilesystemKind; use indicatif::HumanBytes; diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..41ddcd6 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,122 @@ +use crate::passwords::{passwords_filename, PasswordError, Passwords}; + +use bytesize::MIB; +use log::{error, trace}; +use serde::{Deserialize, Serialize}; +use std::path::{Path, PathBuf}; + +const DEFAULT_CHUNK_SIZE: usize = MIB as usize; +const DEVNULL: &str = "/dev/null"; + +#[derive(Debug, Deserialize, Clone)] +#[serde(deny_unknown_fields)] +struct TentativeClientConfig { + server_url: String, + verify_tls_cert: Option, + chunk_size: Option, + roots: Vec, + log: Option, + encrypt: Option, +} + +#[derive(Debug, Serialize, Clone)] +pub enum ClientConfig { + Plain(ClientConfigWithoutPasswords), + WithPasswords(ClientConfigWithoutPasswords, Passwords), +} + +impl ClientConfig { + pub fn read_without_passwords(filename: &Path) -> Result { + let config = ClientConfigWithoutPasswords::read_config(filename)?; + Ok(ClientConfig::Plain(config)) + } + + pub fn read_with_passwords(filename: &Path) -> Result { + let config = ClientConfigWithoutPasswords::read_config(filename)?; + if config.encrypt { + let passwords = Passwords::load(&passwords_filename(filename)) + .map_err(ClientConfigError::PasswordsMissing)?; + Ok(ClientConfig::WithPasswords(config, passwords)) + } else { + Ok(ClientConfig::Plain(config)) + } + } + + pub fn config(&self) -> &ClientConfigWithoutPasswords { + match self { + Self::Plain(config) => &config, + Self::WithPasswords(config, _) => &config, + } + } +} + +#[derive(Debug, Serialize, Clone)] +pub struct ClientConfigWithoutPasswords { + pub server_url: String, + pub verify_tls_cert: bool, + pub chunk_size: usize, + pub roots: Vec, + pub log: PathBuf, + pub encrypt: bool, +} + +#[derive(Debug, thiserror::Error)] +pub enum ClientConfigError { + #[error("server_url is empty")] + ServerUrlIsEmpty, + + #[error("No backup roots in config; at least one is needed")] + NoBackupRoot, + + #[error("server URL doesn't use https: {0}")] + NotHttps(String), + + #[error("No passwords are set: you may need to run 'obnam init': {0}")] + PasswordsMissing(PasswordError), + + #[error(transparent)] + IoError(#[from] std::io::Error), + + #[error(transparent)] + SerdeYamlError(#[from] serde_yaml::Error), +} + +pub type ClientConfigResult = Result; + +impl ClientConfigWithoutPasswords { + pub fn read_config(filename: &Path) -> ClientConfigResult { + trace!("read_config: filename={:?}", filename); + let config = std::fs::read_to_string(filename)?; + let tentative: TentativeClientConfig = serde_yaml::from_str(&config)?; + + let encrypt = tentative.encrypt.or(Some(false)).unwrap(); + + let config = Self { + server_url: tentative.server_url, + roots: tentative.roots, + verify_tls_cert: tentative.verify_tls_cert.or(Some(false)).unwrap(), + chunk_size: tentative.chunk_size.or(Some(DEFAULT_CHUNK_SIZE)).unwrap(), + log: tentative + .log + .or_else(|| Some(PathBuf::from(DEVNULL))) + .unwrap(), + encrypt, + }; + + config.check()?; + Ok(config) + } + + fn check(&self) -> Result<(), ClientConfigError> { + if self.server_url.is_empty() { + return Err(ClientConfigError::ServerUrlIsEmpty); + } + if !self.server_url.starts_with("https://") { + return Err(ClientConfigError::NotHttps(self.server_url.to_string())); + } + if self.roots.is_empty() { + return Err(ClientConfigError::NoBackupRoot); + } + Ok(()) + } +} diff --git a/src/error.rs b/src/error.rs index 454bba6..8241d5d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,7 @@ use crate::backup_run::BackupError; -use crate::client::{ClientConfigError, ClientError}; +use crate::client::ClientError; use crate::cmd::restore::RestoreError; +use crate::config::ClientConfigError; use crate::generation::{LocalGenerationError, NascentError}; use crate::genlist::GenerationListError; use crate::passwords::PasswordError; diff --git a/src/lib.rs b/src/lib.rs index fb4c7fe..82dab15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ pub mod chunkid; pub mod chunkmeta; pub mod client; pub mod cmd; +pub mod config; pub mod error; pub mod fsentry; pub mod fsiter; -- cgit v1.2.1 From 822a2f8c6c4d83d82d4543eb5fcf0ed01df21435 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:12:54 +0300 Subject: refactor: struct Init subcommand --- src/bin/obnam.rs | 21 ++++++--------------- src/cmd/init.rs | 43 ++++++++++++++++++++++++++----------------- src/cmd/mod.rs | 3 +-- 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 10048a3..4caa548 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -2,9 +2,8 @@ use directories_next::ProjectDirs; use log::{debug, error, info, LevelFilter}; use log4rs::append::file::FileAppender; use log4rs::config::{Appender, Config, Logger, Root}; -use obnam::cmd::{ - backup, get_chunk, init, list, list_files, restore, show_config, show_generation, -}; +use obnam::cmd::init::Init; +use obnam::cmd::{backup, get_chunk, list, list_files, restore, show_config, show_generation}; use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; use structopt::StructOpt; @@ -23,17 +22,12 @@ fn main() -> anyhow::Result<()> { debug!("configuration: {:#?}", config); let cfgname = config_filename(&opt); - let result = if let Command::Init { - insecure_passphrase, - } = opt.cmd - { - init(config.config(), &cfgname, insecure_passphrase) + let result = if let Command::Init(init) = opt.cmd { + init.run(config.config(), &cfgname) } else { let config = load_config_with_passwords(&opt)?; match opt.cmd { - Command::Init { - insecure_passphrase: _, - } => panic!("this cannot happen"), + Command::Init(_) => panic!("this cannot happen"), Command::Backup => backup(&config), Command::List => list(&config), Command::ShowGeneration { gen_id } => show_generation(&config, &gen_id), @@ -101,10 +95,7 @@ struct Opt { #[derive(Debug, StructOpt)] enum Command { - Init { - #[structopt(long)] - insecure_passphrase: Option, - }, + Init(Init), Backup, List, ListFiles { diff --git a/src/cmd/init.rs b/src/cmd/init.rs index fb8a6af..462174e 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -2,27 +2,36 @@ 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: "; -pub fn init( - config: &ClientConfigWithoutPasswords, - config_filename: &Path, +#[derive(Debug, StructOpt)] +pub struct Init { + #[structopt(long)] insecure_passphrase: Option, -) -> 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(), - }; +impl Init { + pub fn run( + &self, + config: &ClientConfigWithoutPasswords, + config_filename: &Path, + ) -> Result<(), ObnamError> { + if !config.encrypt { + panic!("no encryption specified"); + } - let passwords = Passwords::new(&passphrase); - let filename = passwords_filename(config_filename); - passwords - .save(&filename) - .map_err(|err| ObnamError::PasswordSave(filename, err))?; - Ok(()) + let passphrase = match &self.insecure_passphrase { + Some(x) => x.to_string(), + 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 70dde59..e252bc9 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,5 +1,4 @@ -mod init; -pub use init::init; +pub mod init; mod backup; pub use backup::backup; -- cgit v1.2.1 From 82eb02a0437ba8a7bf2025f5b61864b450ee68b2 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:16:30 +0300 Subject: refactor: struct Backup subcommand --- src/bin/obnam.rs | 7 ++++--- src/cmd/backup.rs | 32 +++++++++++++++++++------------- src/cmd/mod.rs | 4 +--- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 4caa548..f639153 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -2,8 +2,9 @@ use directories_next::ProjectDirs; use log::{debug, error, info, LevelFilter}; use log4rs::append::file::FileAppender; use log4rs::config::{Appender, Config, Logger, Root}; +use obnam::cmd::backup::Backup; use obnam::cmd::init::Init; -use obnam::cmd::{backup, get_chunk, list, list_files, restore, show_config, show_generation}; +use obnam::cmd::{get_chunk, list, list_files, restore, show_config, show_generation}; use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; use structopt::StructOpt; @@ -28,7 +29,7 @@ fn main() -> anyhow::Result<()> { let config = load_config_with_passwords(&opt)?; match opt.cmd { Command::Init(_) => panic!("this cannot happen"), - Command::Backup => backup(&config), + Command::Backup(x) => x.run(&config), Command::List => list(&config), Command::ShowGeneration { gen_id } => show_generation(&config, &gen_id), Command::ListFiles { gen_id } => list_files(&config, &gen_id), @@ -96,7 +97,7 @@ struct Opt { #[derive(Debug, StructOpt)] enum Command { Init(Init), - Backup, + Backup(Backup), List, ListFiles { #[structopt(default_value = "latest")] diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs index 1091241..a0e0599 100644 --- a/src/cmd/backup.rs +++ b/src/cmd/backup.rs @@ -10,27 +10,33 @@ use bytesize::MIB; use log::info; use std::path::Path; use std::time::SystemTime; +use structopt::StructOpt; use tempfile::NamedTempFile; const SQLITE_CHUNK_SIZE: usize = MIB as usize; -pub fn backup(config: &ClientConfig) -> Result<(), ObnamError> { - let runtime = SystemTime::now(); +#[derive(Debug, StructOpt)] +pub struct Backup {} - let client = BackupClient::new(config)?; - let genlist = client.list_generations()?; - let (gen_id, file_count, warnings) = match genlist.resolve("latest") { - Err(_) => initial_backup(&config, &client)?, - Ok(old_ref) => incremental_backup(&old_ref, &config, &client)?, - }; +impl Backup { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let runtime = SystemTime::now(); - for w in warnings.iter() { - println!("warning: {}", w); - } + let client = BackupClient::new(config)?; + let genlist = client.list_generations()?; + let (gen_id, file_count, warnings) = match genlist.resolve("latest") { + Err(_) => initial_backup(&config, &client)?, + Ok(old_ref) => incremental_backup(&old_ref, &config, &client)?, + }; + + for w in warnings.iter() { + println!("warning: {}", w); + } - report_stats(&runtime, file_count, &gen_id, warnings.len())?; + report_stats(&runtime, file_count, &gen_id, warnings.len())?; - Ok(()) + Ok(()) + } } fn report_stats( diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index e252bc9..fd5f8e9 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,8 +1,6 @@ +pub mod backup; pub mod init; -mod backup; -pub use backup::backup; - mod list; pub use list::list; -- cgit v1.2.1 From c196a68471dd9dafbcc4030342aa4164d7a2d3e3 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:19:03 +0300 Subject: refactor: struct List subcommand --- src/bin/obnam.rs | 7 ++++--- src/cmd/list.rs | 20 +++++++++++++------- src/cmd/mod.rs | 4 +--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index f639153..9e0ab0f 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -4,7 +4,8 @@ use log4rs::append::file::FileAppender; use log4rs::config::{Appender, Config, Logger, Root}; use obnam::cmd::backup::Backup; use obnam::cmd::init::Init; -use obnam::cmd::{get_chunk, list, list_files, restore, show_config, show_generation}; +use obnam::cmd::list::List; +use obnam::cmd::{get_chunk, list_files, restore, show_config, show_generation}; use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; use structopt::StructOpt; @@ -30,7 +31,7 @@ fn main() -> anyhow::Result<()> { match opt.cmd { Command::Init(_) => panic!("this cannot happen"), Command::Backup(x) => x.run(&config), - Command::List => list(&config), + Command::List(x) => x.run(&config), Command::ShowGeneration { gen_id } => show_generation(&config, &gen_id), Command::ListFiles { gen_id } => list_files(&config, &gen_id), Command::Restore { gen_id, to } => restore(&config, &gen_id, &to), @@ -98,7 +99,7 @@ struct Opt { enum Command { Init(Init), Backup(Backup), - List, + List(List), ListFiles { #[structopt(default_value = "latest")] gen_id: String, diff --git a/src/cmd/list.rs b/src/cmd/list.rs index f48512b..66036b9 100644 --- a/src/cmd/list.rs +++ b/src/cmd/list.rs @@ -1,14 +1,20 @@ use crate::client::BackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; +use structopt::StructOpt; -pub fn list(config: &ClientConfig) -> Result<(), ObnamError> { - let client = BackupClient::new(config)?; +#[derive(Debug, StructOpt)] +pub struct List {} - let generations = client.list_generations()?; - for finished in generations.iter() { - println!("{} {}", finished.id(), finished.ended()); - } +impl List { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let client = BackupClient::new(config)?; + + let generations = client.list_generations()?; + for finished in generations.iter() { + println!("{} {}", finished.id(), finished.ended()); + } - Ok(()) + Ok(()) + } } diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index fd5f8e9..1f6bd78 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,8 +1,6 @@ pub mod backup; pub mod init; - -mod list; -pub use list::list; +pub mod list; mod list_files; pub use list_files::list_files; -- cgit v1.2.1 From 7a32584448ebe00bdb45aa5daf506ce37c4811e0 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:22:45 +0300 Subject: refactor: struct ShowGeneration subcommand --- src/bin/obnam.rs | 10 ++++------ src/cmd/mod.rs | 4 +--- src/cmd/show_gen.rs | 49 +++++++++++++++++++++++++++++-------------------- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 9e0ab0f..72dc208 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -5,7 +5,8 @@ use log4rs::config::{Appender, Config, Logger, Root}; use obnam::cmd::backup::Backup; use obnam::cmd::init::Init; use obnam::cmd::list::List; -use obnam::cmd::{get_chunk, list_files, restore, show_config, show_generation}; +use obnam::cmd::show_gen::ShowGeneration; +use obnam::cmd::{get_chunk, list_files, restore, show_config}; use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; use structopt::StructOpt; @@ -32,7 +33,7 @@ fn main() -> anyhow::Result<()> { Command::Init(_) => panic!("this cannot happen"), Command::Backup(x) => x.run(&config), Command::List(x) => x.run(&config), - Command::ShowGeneration { gen_id } => show_generation(&config, &gen_id), + Command::ShowGeneration(x) => x.run(&config), Command::ListFiles { gen_id } => list_files(&config, &gen_id), Command::Restore { gen_id, to } => restore(&config, &gen_id, &to), Command::GetChunk { chunk_id } => get_chunk(&config, &chunk_id), @@ -111,10 +112,7 @@ enum Command { #[structopt(parse(from_os_str))] to: PathBuf, }, - ShowGeneration { - #[structopt(default_value = "latest")] - gen_id: String, - }, + ShowGeneration(ShowGeneration), GetChunk { #[structopt()] chunk_id: String, diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 1f6bd78..f96dc2a 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,6 +1,7 @@ pub mod backup; pub mod init; pub mod list; +pub mod show_gen; mod list_files; pub use list_files::list_files; @@ -11,8 +12,5 @@ pub use restore::restore; pub mod get_chunk; pub use get_chunk::get_chunk; -pub mod show_gen; -pub use show_gen::show_generation; - pub mod show_config; pub use show_config::show_config; diff --git a/src/cmd/show_gen.rs b/src/cmd/show_gen.rs index 5b786cf..ba39809 100644 --- a/src/cmd/show_gen.rs +++ b/src/cmd/show_gen.rs @@ -3,31 +3,40 @@ use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::FilesystemKind; use indicatif::HumanBytes; +use structopt::StructOpt; use tempfile::NamedTempFile; -pub fn show_generation(config: &ClientConfig, gen_ref: &str) -> Result<(), ObnamError> { - let temp = NamedTempFile::new()?; +#[derive(Debug, StructOpt)] +pub struct ShowGeneration { + #[structopt(default_value = "latest")] + gen_id: String, +} + +impl ShowGeneration { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let temp = NamedTempFile::new()?; - let client = BackupClient::new(config)?; + let client = BackupClient::new(config)?; - let genlist = client.list_generations()?; - let gen_id: String = genlist.resolve(gen_ref)?; - let gen = client.fetch_generation(&gen_id, temp.path())?; - let files = gen.files()?; + let genlist = client.list_generations()?; + let gen_id: String = genlist.resolve(&self.gen_id)?; + let gen = client.fetch_generation(&gen_id, temp.path())?; + let files = gen.files()?; - let total_bytes = files.iter().fold(0, |acc, file| { - let e = file.entry(); - if e.kind() == FilesystemKind::Regular { - acc + file.entry().len() - } else { - acc - } - }); + let total_bytes = files.iter().fold(0, |acc, file| { + let e = file.entry(); + if e.kind() == FilesystemKind::Regular { + acc + file.entry().len() + } else { + acc + } + }); - println!("generation-id: {}", gen_id); - println!("file-count: {}", gen.file_count()?); - println!("file-bytes: {}", HumanBytes(total_bytes)); - println!("file-bytes-raw: {}", total_bytes); + println!("generation-id: {}", gen_id); + println!("file-count: {}", gen.file_count()?); + println!("file-bytes: {}", HumanBytes(total_bytes)); + println!("file-bytes-raw: {}", total_bytes); - Ok(()) + Ok(()) + } } -- cgit v1.2.1 From 2aa0eef599db13db6802a75a8b285d75d8035033 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:26:19 +0300 Subject: refactor: struct ListFiles subcommand --- src/bin/obnam.rs | 10 ++++------ src/cmd/list_files.rs | 29 +++++++++++++++++++---------- src/cmd/mod.rs | 4 +--- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 72dc208..0552e3b 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -5,8 +5,9 @@ use log4rs::config::{Appender, Config, Logger, Root}; use obnam::cmd::backup::Backup; use obnam::cmd::init::Init; use obnam::cmd::list::List; +use obnam::cmd::list_files::ListFiles; use obnam::cmd::show_gen::ShowGeneration; -use obnam::cmd::{get_chunk, list_files, restore, show_config}; +use obnam::cmd::{get_chunk, restore, show_config}; use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; use structopt::StructOpt; @@ -34,7 +35,7 @@ fn main() -> anyhow::Result<()> { Command::Backup(x) => x.run(&config), Command::List(x) => x.run(&config), Command::ShowGeneration(x) => x.run(&config), - Command::ListFiles { gen_id } => list_files(&config, &gen_id), + Command::ListFiles(x) => x.run(&config), Command::Restore { gen_id, to } => restore(&config, &gen_id, &to), Command::GetChunk { chunk_id } => get_chunk(&config, &chunk_id), Command::Config => show_config(&config), @@ -101,10 +102,7 @@ enum Command { Init(Init), Backup(Backup), List(List), - ListFiles { - #[structopt(default_value = "latest")] - gen_id: String, - }, + ListFiles(ListFiles), Restore { #[structopt()] gen_id: String, diff --git a/src/cmd/list_files.rs b/src/cmd/list_files.rs index d490e85..22b102e 100644 --- a/src/cmd/list_files.rs +++ b/src/cmd/list_files.rs @@ -3,22 +3,31 @@ use crate::client::BackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::{FilesystemEntry, FilesystemKind}; +use structopt::StructOpt; use tempfile::NamedTempFile; -pub fn list_files(config: &ClientConfig, gen_ref: &str) -> Result<(), ObnamError> { - let temp = NamedTempFile::new()?; +#[derive(Debug, StructOpt)] +pub struct ListFiles { + #[structopt(default_value = "latest")] + gen_id: String, +} - let client = BackupClient::new(config)?; +impl ListFiles { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let temp = NamedTempFile::new()?; - let genlist = client.list_generations()?; - let gen_id: String = genlist.resolve(gen_ref)?; + let client = BackupClient::new(config)?; - let gen = client.fetch_generation(&gen_id, temp.path())?; - for file in gen.files()? { - println!("{}", format_entry(&file.entry(), file.reason())); - } + let genlist = client.list_generations()?; + let gen_id: String = genlist.resolve(&self.gen_id)?; - Ok(()) + let gen = client.fetch_generation(&gen_id, temp.path())?; + for file in gen.files()? { + println!("{}", format_entry(&file.entry(), file.reason())); + } + + Ok(()) + } } fn format_entry(e: &FilesystemEntry, reason: Reason) -> String { diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index f96dc2a..a5f4884 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,11 +1,9 @@ pub mod backup; pub mod init; pub mod list; +pub mod list_files; pub mod show_gen; -mod list_files; -pub use list_files::list_files; - pub mod restore; pub use restore::restore; -- cgit v1.2.1 From 3defb987dad68fa45dbf8a0044adb97104d04a0b Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:31:03 +0300 Subject: refactor: struct Restore subcommand --- src/bin/obnam.rs | 13 ++++------- src/cmd/mod.rs | 4 +--- src/cmd/restore.rs | 64 ++++++++++++++++++++++++++++++++++-------------------- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 0552e3b..dcd09f6 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -6,8 +6,9 @@ use obnam::cmd::backup::Backup; use obnam::cmd::init::Init; use obnam::cmd::list::List; use obnam::cmd::list_files::ListFiles; +use obnam::cmd::restore::Restore; use obnam::cmd::show_gen::ShowGeneration; -use obnam::cmd::{get_chunk, restore, show_config}; +use obnam::cmd::{get_chunk, show_config}; use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; use structopt::StructOpt; @@ -36,7 +37,7 @@ fn main() -> anyhow::Result<()> { Command::List(x) => x.run(&config), Command::ShowGeneration(x) => x.run(&config), Command::ListFiles(x) => x.run(&config), - Command::Restore { gen_id, to } => restore(&config, &gen_id, &to), + Command::Restore(x) => x.run(&config), Command::GetChunk { chunk_id } => get_chunk(&config, &chunk_id), Command::Config => show_config(&config), } @@ -103,13 +104,7 @@ enum Command { Backup(Backup), List(List), ListFiles(ListFiles), - Restore { - #[structopt()] - gen_id: String, - - #[structopt(parse(from_os_str))] - to: PathBuf, - }, + Restore(Restore), ShowGeneration(ShowGeneration), GetChunk { #[structopt()] diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index a5f4884..3657d3e 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -2,10 +2,8 @@ pub mod backup; pub mod init; pub mod list; pub mod list_files; -pub mod show_gen; - pub mod restore; -pub use restore::restore; +pub mod show_gen; pub mod get_chunk; pub use get_chunk::get_chunk; diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs index c2cbb7f..a321e80 100644 --- a/src/cmd/restore.rs +++ b/src/cmd/restore.rs @@ -18,32 +18,50 @@ use std::path::{Path, PathBuf}; use structopt::StructOpt; use tempfile::NamedTempFile; -pub fn restore(config: &ClientConfig, gen_ref: &str, to: &Path) -> Result<(), ObnamError> { - let temp = NamedTempFile::new()?; - - let client = BackupClient::new(config)?; - - let genlist = client.list_generations()?; - let gen_id: String = genlist.resolve(gen_ref)?; - info!("generation id is {}", gen_id); - - let gen = client.fetch_generation(&gen_id, temp.path())?; - info!("restoring {} files", gen.file_count()?); - let progress = create_progress_bar(gen.file_count()?, true); - for file in gen.files()? { - match file.reason() { - Reason::FileError => (), - _ => restore_generation(&client, &gen, file.fileno(), file.entry(), &to, &progress)?, +#[derive(Debug, StructOpt)] +pub struct Restore { + #[structopt()] + gen_id: String, + + #[structopt(parse(from_os_str))] + to: PathBuf, +} + +impl Restore { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let temp = NamedTempFile::new()?; + + let client = BackupClient::new(config)?; + + let genlist = client.list_generations()?; + let gen_id: String = genlist.resolve(&self.gen_id)?; + info!("generation id is {}", gen_id); + + let gen = client.fetch_generation(&gen_id, temp.path())?; + info!("restoring {} files", gen.file_count()?); + let progress = create_progress_bar(gen.file_count()?, true); + for file in gen.files()? { + match file.reason() { + Reason::FileError => (), + _ => restore_generation( + &client, + &gen, + file.fileno(), + file.entry(), + &self.to, + &progress, + )?, + } } - } - for file in gen.files()? { - if file.entry().is_dir() { - restore_directory_metadata(file.entry(), &to)?; + for file in gen.files()? { + if file.entry().is_dir() { + restore_directory_metadata(file.entry(), &self.to)?; + } } - } - progress.finish(); + progress.finish(); - Ok(()) + Ok(()) + } } #[derive(Debug, StructOpt)] -- cgit v1.2.1 From 20a5db2cc1b348087bca5e689ed38f98e1150955 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:35:59 +0300 Subject: refactor: struct GetChunk subcommand --- src/bin/obnam.rs | 10 ++++------ src/cmd/get_chunk.rs | 25 +++++++++++++++++-------- src/cmd/mod.rs | 4 +--- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index dcd09f6..a064bf9 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -3,12 +3,13 @@ use log::{debug, error, info, LevelFilter}; use log4rs::append::file::FileAppender; use log4rs::config::{Appender, Config, Logger, Root}; use obnam::cmd::backup::Backup; +use obnam::cmd::get_chunk::GetChunk; use obnam::cmd::init::Init; use obnam::cmd::list::List; use obnam::cmd::list_files::ListFiles; use obnam::cmd::restore::Restore; +use obnam::cmd::show_config; use obnam::cmd::show_gen::ShowGeneration; -use obnam::cmd::{get_chunk, show_config}; use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; use structopt::StructOpt; @@ -38,7 +39,7 @@ fn main() -> anyhow::Result<()> { Command::ShowGeneration(x) => x.run(&config), Command::ListFiles(x) => x.run(&config), Command::Restore(x) => x.run(&config), - Command::GetChunk { chunk_id } => get_chunk(&config, &chunk_id), + Command::GetChunk(x) => x.run(&config), Command::Config => show_config(&config), } }; @@ -106,9 +107,6 @@ enum Command { ListFiles(ListFiles), Restore(Restore), ShowGeneration(ShowGeneration), - GetChunk { - #[structopt()] - chunk_id: String, - }, + GetChunk(GetChunk), Config, } diff --git a/src/cmd/get_chunk.rs b/src/cmd/get_chunk.rs index c9d640e..4ee70fe 100644 --- a/src/cmd/get_chunk.rs +++ b/src/cmd/get_chunk.rs @@ -3,14 +3,23 @@ use crate::client::BackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; use std::io::{stdout, Write}; +use structopt::StructOpt; -pub fn get_chunk(config: &ClientConfig, chunk_id: &str) -> Result<(), ObnamError> { - let client = BackupClient::new(config)?; - let chunk_id: ChunkId = chunk_id.parse().unwrap(); - let chunk = client.fetch_chunk(&chunk_id)?; +#[derive(Debug, StructOpt)] +pub struct GetChunk { + #[structopt()] + chunk_id: String, +} + +impl GetChunk { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let client = BackupClient::new(config)?; + let chunk_id: ChunkId = self.chunk_id.parse().unwrap(); + let chunk = client.fetch_chunk(&chunk_id)?; - let stdout = stdout(); - let mut handle = stdout.lock(); - handle.write_all(chunk.data())?; - Ok(()) + let stdout = stdout(); + let mut handle = stdout.lock(); + handle.write_all(chunk.data())?; + Ok(()) + } } diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 3657d3e..02f4f01 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,12 +1,10 @@ pub mod backup; +pub mod get_chunk; pub mod init; pub mod list; pub mod list_files; pub mod restore; pub mod show_gen; -pub mod get_chunk; -pub use get_chunk::get_chunk; - pub mod show_config; pub use show_config::show_config; -- cgit v1.2.1 From 958839c264b3ef36ef1bc74a4794fc383cc39292 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:48:40 +0300 Subject: refactor: struct ShowConfig subcommand --- src/bin/obnam.rs | 10 +++++----- src/cmd/mod.rs | 4 +--- src/cmd/show_config.rs | 12 +++++++++--- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index a064bf9..0f3da30 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -1,14 +1,14 @@ use directories_next::ProjectDirs; use log::{debug, error, info, LevelFilter}; use log4rs::append::file::FileAppender; -use log4rs::config::{Appender, Config, Logger, Root}; +use log4rs::config::{Appender, Logger, Root}; use obnam::cmd::backup::Backup; use obnam::cmd::get_chunk::GetChunk; use obnam::cmd::init::Init; use obnam::cmd::list::List; use obnam::cmd::list_files::ListFiles; use obnam::cmd::restore::Restore; -use obnam::cmd::show_config; +use obnam::cmd::show_config::ShowConfig; use obnam::cmd::show_gen::ShowGeneration; use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; @@ -40,7 +40,7 @@ fn main() -> anyhow::Result<()> { Command::ListFiles(x) => x.run(&config), Command::Restore(x) => x.run(&config), Command::GetChunk(x) => x.run(&config), - Command::Config => show_config(&config), + Command::Config(x) => x.run(&config), } }; @@ -56,7 +56,7 @@ fn main() -> anyhow::Result<()> { fn setup_logging(filename: &Path) -> anyhow::Result<()> { let logfile = FileAppender::builder().build(filename)?; - let config = Config::builder() + let config = log4rs::Config::builder() .appender(Appender::builder().build("obnam", Box::new(logfile))) .logger(Logger::builder().build("obnam", LevelFilter::Debug)) .build(Root::builder().appender("obnam").build(LevelFilter::Debug))?; @@ -108,5 +108,5 @@ enum Command { Restore(Restore), ShowGeneration(ShowGeneration), GetChunk(GetChunk), - Config, + Config(ShowConfig), } diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 02f4f01..890e176 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -4,7 +4,5 @@ pub mod init; pub mod list; pub mod list_files; pub mod restore; -pub mod show_gen; - pub mod show_config; -pub use show_config::show_config; +pub mod show_gen; diff --git a/src/cmd/show_config.rs b/src/cmd/show_config.rs index 88ee9ec..424e2ed 100644 --- a/src/cmd/show_config.rs +++ b/src/cmd/show_config.rs @@ -1,7 +1,13 @@ use crate::config::ClientConfig; use crate::error::ObnamError; +use structopt::StructOpt; -pub fn show_config(config: &ClientConfig) -> Result<(), ObnamError> { - println!("{}", serde_json::to_string_pretty(&config.config())?); - Ok(()) +#[derive(Debug, StructOpt)] +pub struct ShowConfig {} + +impl ShowConfig { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + println!("{}", serde_json::to_string_pretty(&config.config())?); + Ok(()) + } } -- cgit v1.2.1 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 From 3295950f41342b8b9a312f203b111c7c277500b4 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 10:15:58 +0300 Subject: refactor: main function for clarity --- src/bin/obnam.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 4f4cd5b..cdb5179 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -27,19 +27,20 @@ fn main() -> anyhow::Result<()> { debug!("{:?}", opt); debug!("configuration: {:#?}", config); - let result = if let Command::Init(init) = opt.cmd { - init.run(config.config()) - } else { - let config = load_config_with_passwords(&opt)?; - match opt.cmd { - Command::Init(_) => panic!("this cannot happen"), - Command::Backup(x) => x.run(&config), - Command::List(x) => x.run(&config), - Command::ShowGeneration(x) => x.run(&config), - Command::ListFiles(x) => x.run(&config), - Command::Restore(x) => x.run(&config), - Command::GetChunk(x) => x.run(&config), - Command::Config(x) => x.run(&config), + let result = match opt.cmd { + Command::Init(x) => x.run(config.config()), + _ => { + let config = load_config_with_passwords(&opt)?; + match opt.cmd { + Command::Init(_) => panic!("this can't happen"), + Command::Backup(x) => x.run(&config), + Command::List(x) => x.run(&config), + Command::ShowGeneration(x) => x.run(&config), + Command::ListFiles(x) => x.run(&config), + Command::Restore(x) => x.run(&config), + Command::GetChunk(x) => x.run(&config), + Command::Config(x) => x.run(&config), + } } }; -- cgit v1.2.1