[PATCH] cli: possibly provide the configuration file as cmd line parameter
Export this patch
Fixes: https://todo.sr.ht/~whynothugo/vdirsyncer-rs/87
---
vdirsyncer/src/cli.rs | 4 ++++
vdirsyncer/src/main.rs | 16 ++++++++++++++--
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/vdirsyncer/src/cli.rs b/vdirsyncer/src/cli.rs
index 6dd8fb6..38f398c 100644
--- a/vdirsyncer/src/cli.rs
+++ b/vdirsyncer/src/cli.rs
@@ -14,6 +14,7 @@ pub(crate) enum Command {
pub(crate) struct Cli {
pub command: Command,
pub log_level: log::LevelFilter,
+ pub config_file: Option<String>,
pub pairs: Option<Vec<String>>,
}
@@ -21,6 +22,7 @@ impl Cli {
pub fn parse(mut args: impl Iterator<Item = String>) -> Result<Cli, lexopt::Error> {
let mut command = None;
let mut log_level = log::LevelFilter::Warn;
+ let mut config_file: Option<String> = None;
let mut pairs = Vec::new();
args.next(); // Skip arg0
@@ -28,6 +30,7 @@ impl Cli {
while let Some(arg) = parser.next()? {
match arg {
lexopt::Arg::Short('v') => log_level = parser.value()?.parse()?,
+ lexopt::Arg::Short('c') => config_file = Some(parser.value()?.string()?),
lexopt::Arg::Short('p') => {
let pair_name = parser.value()?.string()?;
pairs.push(pair_name);
@@ -89,6 +92,7 @@ impl Cli {
Ok(Cli {
command: command.ok_or(lexopt::Error::from("No command specified"))?,
log_level,
+ config_file,
pairs: if pairs.is_empty() { None } else { Some(pairs) },
})
}
diff --git a/vdirsyncer/src/main.rs b/vdirsyncer/src/main.rs
index 1e5fda8..75c5416 100644
--- a/vdirsyncer/src/main.rs
+++ b/vdirsyncer/src/main.rs
@@ -5,7 +5,9 @@
#![deny(clippy::unwrap_used)]
use std::{
+ fs::File,
io::{read_to_string, Seek, Write},
+ path::PathBuf,
sync::Arc,
time::Duration,
};
@@ -404,7 +406,7 @@ impl App {
async fn main() -> anyhow::Result<()> {
let cli = Cli::parse(std::env::args()).unwrap_or_else(|err| {
eprintln!("Bad usage: {err}\n");
- eprintln!("Usage: vdirsyncer [-v LOGLEVEL] [-p PAIR] COMMAND [ARGS...]");
+ eprintln!("Usage: vdirsyncer [-c CONFIGFILE ] [-v LOGLEVEL] [-p PAIR] COMMAND [ARGS...]");
eprintln!("Commands:");
eprintln!("\tcheck\t\t\tcheck configuration and exit");
eprintln!("\tdaemon -[r READY_FD]\tkeep storages in sync");
@@ -427,7 +429,17 @@ async fn main() -> anyhow::Result<()> {
.expect("logger should initialise");
info!("Logging enabled with {} level", cli.log_level);
- let (config_path, config_file) = open_default_path()?;
+ let (config_path, config_file) = match cli.config_file {
+ Some(file) => {
+ let path = PathBuf::from(file);
+ let file = File::open(&path)
+ .with_context(|| format!("Could not open {}.", path.to_string_lossy()))?;
+ debug!("Opened config file {}", path.to_string_lossy());
+ (path, file)
+ }
+ None => open_default_path()?,
+ };
+
let config_data = read_to_string(config_file)?;
let config = parse_config(&config_data, &cli.pairs).with_context(|| {
format!(
--
2.47.0
Applied, thanks!