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. --- obnam.md | 18 +++++++++--------- src/bin/obnam.rs | 35 +++++++++++++++++++---------------- src/cmd/backup.rs | 4 +--- src/cmd/list.rs | 4 +--- src/cmd/restore.rs | 4 +--- subplot/client.py | 12 +++++++++++- subplot/client.yaml | 2 +- 7 files changed, 43 insertions(+), 36 deletions(-) diff --git a/obnam.md b/obnam.md index 057996d..fbbf994 100644 --- a/obnam.md +++ b/obnam.md @@ -795,11 +795,11 @@ and a running chunk server and a client config based on smoke.yaml and a file live/data.dat containing some random data and a manifest of the directory live in live.yaml -when I run obnam backup smoke.yaml +when I run obnam --config smoke.yaml backup then backup generation is GEN -when I run obnam list smoke.yaml +when I run obnam --config smoke.yaml list then generation list contains -when I invoke obnam restore smoke.yaml rest +when I invoke obnam --config smoke.yaml restore rest given a manifest of the directory live restored in rest in rest.yaml then files live.yaml and rest.yaml match ~~~ @@ -832,9 +832,9 @@ and a running chunk server and a client config based on metadata.yaml and a file live/data.dat containing some random data and a manifest of the directory live in live.yaml -when I run obnam backup metadata.yaml +when I run obnam --config metadata.yaml backup then backup generation is GEN -when I invoke obnam restore metadata.yaml rest +when I invoke obnam --config metadata.yaml restore rest given a manifest of the directory live restored in rest in rest.yaml then files live.yaml and rest.yaml match ~~~ @@ -851,9 +851,9 @@ and a client config based on metadata.yaml and a file live/data.dat containing some random data and file live/data.dat has mode 464 and a manifest of the directory live in live.yaml -when I run obnam backup metadata.yaml +when I run obnam --config metadata.yaml backup then backup generation is GEN -when I invoke obnam restore metadata.yaml rest +when I invoke obnam --config metadata.yaml restore rest given a manifest of the directory live restored in rest in rest.yaml then files live.yaml and rest.yaml match ~~~ @@ -869,9 +869,9 @@ and a client config based on metadata.yaml and a file live/data.dat containing some random data and symbolink link live/link that points at data.dat and a manifest of the directory live in live.yaml -when I run obnam backup metadata.yaml +when I run obnam --config metadata.yaml backup then backup generation is GEN -when I invoke obnam restore metadata.yaml rest +when I invoke obnam --config metadata.yaml restore rest given a manifest of the directory live restored in rest in rest.yaml then files live.yaml and rest.yaml match ~~~ 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); diff --git a/subplot/client.py b/subplot/client.py index d89b7d4..1dcbe8c 100644 --- a/subplot/client.py +++ b/subplot/client.py @@ -28,7 +28,17 @@ def run_obnam_restore(ctx, filename=None, genid=None, todir=None): genid = ctx["vars"][genid] runcmd_run( - ctx, ["env", "RUST_LOG=obnam", "obnam", "restore", filename, genid, todir] + ctx, + [ + "env", + "RUST_LOG=obnam", + "obnam", + "--config", + filename, + "restore", + genid, + todir, + ], ) diff --git a/subplot/client.yaml b/subplot/client.yaml index 63033c3..f8e89b6 100644 --- a/subplot/client.yaml +++ b/subplot/client.yaml @@ -4,7 +4,7 @@ - given: "a client config based on {filename}" function: configure_client -- when: "I invoke obnam restore {filename} <{genid}> {todir}" +- when: "I invoke obnam --config {filename} restore <{genid}> {todir}" function: run_obnam_restore - then: "backup generation is {varname}" -- cgit v1.2.1