[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/config.rs | 8 +++++---
vdirsyncer/src/main.rs | 6 +++---
3 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/vdirsyncer/src/cli.rs b/vdirsyncer/src/cli.rs
index 6dd8fb6..74f30d3 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>>,
}
@@ -22,12 +23,14 @@ impl Cli {
let mut command = None;
let mut log_level = log::LevelFilter::Warn;
let mut pairs = Vec::new();
+ let mut config_file: Option<String> = None;
args.next(); // Skip arg0
let mut parser = lexopt::Parser::from_args(args);
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/config.rs b/vdirsyncer/src/config.rs
index 2431ff1..160de3f 100644
--- a/vdirsyncer/src/config.rs
+++ b/vdirsyncer/src/config.rs
@@ -648,11 +648,13 @@ pub(crate) fn parse_from_file(mut path: File) -> anyhow::Result<Config> {
Ok(config)
}
-/// Open the configuration file, expecting it in the default path.
+/// Open the configuration file, expecting it as parameter or in the default path.
///
/// Returns the path of the file opened and the file itself.
-pub(crate) fn open_default_path() -> anyhow::Result<(PathBuf, File)> {
- let path = if let Some(xdg) = std::env::var_os("XDG_CONFIG_HOME") {
+pub(crate) fn open_config_path(config_file: Option<String>) -> anyhow::Result<(PathBuf, File)> {
+ let path = if let Some(file) = config_file {
+ PathBuf::from(file)
+ } else if let Some(xdg) = std::env::var_os("XDG_CONFIG_HOME") {
PathBuf::from(xdg).join("vdirsyncer/config.toml")
} else {
#[allow(deprecated)]
diff --git a/vdirsyncer/src/main.rs b/vdirsyncer/src/main.rs
index af9daac..66208ae 100644
--- a/vdirsyncer/src/main.rs
+++ b/vdirsyncer/src/main.rs
@@ -13,7 +13,7 @@ use std::{
use anyhow::{bail, Context};
use camino::Utf8PathBuf;
-use config::{open_default_path, parse_from_file};
+use config::{open_config_path, parse_from_file};
use log::{debug, error, info, trace, warn};
use rustix::fs::sync;
use stdio::{StdIo, StdIoLock};
@@ -396,7 +396,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");
@@ -419,7 +419,7 @@ 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) = open_config_path(cli.config_file)?;
let config = parse_from_file(config_file).with_context(|| {
format!(
"Could not parse configuration file at {}",
--
2.46.0
I'd rather leave `open_default_path` unchanged and use
`match cli.config_file {..}` here.