summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-12-23 09:44:31 +0200
committerLars Wirzenius <liw@liw.fi>2020-12-23 10:17:17 +0200
commit15ce048ca21ad517e27ab598d861ae07f817067d (patch)
tree01857ffa023c770164a9cc07d1024ed388150864 /src
parentd6584921d8bb77fd4de8b6997848599cb9d73e01 (diff)
downloadobnam2-15ce048ca21ad517e27ab598d861ae07f817067d.tar.gz
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.
Diffstat (limited to 'src')
-rw-r--r--src/bin/obnam.rs35
-rw-r--r--src/cmd/backup.rs4
-rw-r--r--src/cmd/list.rs4
-rw-r--r--src/cmd/restore.rs4
4 files changed, 22 insertions, 25 deletions
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);