From 15ce048ca21ad517e27ab598d861ae07f817067d Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 23 Dec 2020 09:44:31 +0200 Subject: feat! add a global --config option This breaks all invocations of the Obnam client, as the option needs to come before the subcommand name. The benefit of this breakage is simpler, less repetitive code. --- src/bin/obnam.rs | 35 +++++++++++++++++++---------------- src/cmd/backup.rs | 4 +--- src/cmd/list.rs | 4 +--- src/cmd/restore.rs | 4 +--- 4 files changed, 22 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 1394b6c..bc636dc 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -1,4 +1,5 @@ use log::{debug, info}; +use obnam::client::ClientConfig; use obnam::cmd::{backup, list, restore}; use std::path::PathBuf; use structopt::StructOpt; @@ -9,32 +10,34 @@ fn main() -> anyhow::Result<()> { pretty_env_logger::init(); let opt = Opt::from_args(); + let config = ClientConfig::read_config(&opt.config)?; + info!("obnam starts"); debug!("opt: {:?}", opt); - match opt { - Opt::Backup { config } => backup(&config, BUFFER_SIZE)?, - Opt::List { config } => list(&config)?, - Opt::Restore { config, gen_id, to } => restore(&config, &gen_id, &to)?, + match opt.cmd { + Command::Backup => backup(&config, BUFFER_SIZE)?, + Command::List => list(&config)?, + Command::Restore { gen_id, to } => restore(&config, &gen_id, &to)?, } Ok(()) } #[derive(Debug, StructOpt)] #[structopt(name = "obnam-backup", about = "Simplistic backup client")] -enum Opt { - Backup { - #[structopt(parse(from_os_str))] - config: PathBuf, - }, - List { - #[structopt(parse(from_os_str))] - config: PathBuf, - }, - Restore { - #[structopt(parse(from_os_str))] - config: PathBuf, +struct Opt { + #[structopt(long, short, parse(from_os_str))] + config: PathBuf, + + #[structopt(subcommand)] + cmd: Command, +} +#[derive(Debug, StructOpt)] +enum Command { + Backup, + List, + Restore { #[structopt()] gen_id: String, diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs index 86f1b96..178684f 100644 --- a/src/cmd/backup.rs +++ b/src/cmd/backup.rs @@ -3,13 +3,11 @@ use crate::fsiter::FsIterator; use crate::generation::Generation; use indicatif::{ProgressBar, ProgressStyle}; use log::info; -use std::path::Path; use tempfile::NamedTempFile; const GUESS_FILE_COUNT: u64 = 0; -pub fn backup(config: &Path, buffer_size: usize) -> anyhow::Result<()> { - let config = ClientConfig::read_config(config)?; +pub fn backup(config: &ClientConfig, buffer_size: usize) -> anyhow::Result<()> { let client = BackupClient::new(&config.server_url)?; // Create a named temporary file. We don't meed the open file diff --git a/src/cmd/list.rs b/src/cmd/list.rs index 1741bce..6c48244 100644 --- a/src/cmd/list.rs +++ b/src/cmd/list.rs @@ -1,8 +1,6 @@ use crate::client::{BackupClient, ClientConfig}; -use std::path::Path; -pub fn list(config: &Path) -> anyhow::Result<()> { - let config = ClientConfig::read_config(&config)?; +pub fn list(config: &ClientConfig) -> anyhow::Result<()> { let client = BackupClient::new(&config.server_url)?; for gen_id in client.list_generations()? { diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs index 27f0ce3..791bebf 100644 --- a/src/cmd/restore.rs +++ b/src/cmd/restore.rs @@ -14,7 +14,7 @@ use std::path::{Path, PathBuf}; use structopt::StructOpt; use tempfile::NamedTempFile; -pub fn restore(config: &Path, gen_id: &str, to: &Path) -> anyhow::Result<()> { +pub fn restore(config: &ClientConfig, gen_id: &str, to: &Path) -> anyhow::Result<()> { // Create a named temporary file. We don't meed the open file // handle, so we discard that. let dbname = { @@ -23,8 +23,6 @@ pub fn restore(config: &Path, gen_id: &str, to: &Path) -> anyhow::Result<()> { dbname }; - let config = ClientConfig::read_config(&config).unwrap(); - let client = BackupClient::new(&config.server_url)?; let gen_chunk = client.fetch_generation(&gen_id)?; debug!("gen: {:?}", gen_chunk); -- cgit v1.2.1