From 4bbcbfa8eacf92a3ac2f0383d2e3fd2997a80ca7 Mon Sep 17 00:00:00 2001 From: Ron Waldon-Howe Date: Sat, 30 Nov 2024 12:09:35 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Support=20`--config`,=20`--sessi?= =?UTF-8?q?on`=20(default),=20and=20`--system`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bin/busd.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/bin/busd.rs b/src/bin/busd.rs index fdd1424..bcc593f 100644 --- a/src/bin/busd.rs +++ b/src/bin/busd.rs @@ -1,9 +1,10 @@ extern crate busd; +use std::path::PathBuf; #[cfg(unix)] use std::{fs::File, io::Write, os::fd::FromRawFd}; -use busd::bus; +use busd::{bus, config::Config}; use anyhow::Result; use clap::Parser; @@ -18,9 +19,14 @@ use tracing::{info, warn}; #[clap(author, version, about, long_about = None)] struct Args { /// The address to listen on. + /// Takes precedence over any `` element in the configuration file. #[clap(short = 'a', long, value_parser)] address: Option, + /// Use the given configuration file. + #[clap(long)] + config: Option, + /// Print the address of the message bus to standard output. #[clap(long)] print_address: bool, @@ -36,6 +42,15 @@ struct Args { #[cfg(unix)] #[clap(long)] ready_fd: Option, + + /// Equivalent to `--config /usr/share/dbus-1/session.conf`. + /// This is the default if `--config` and `--system` are unspecified. + #[clap(long)] + session: bool, + + /// Equivalent to `--config /usr/share/dbus-1/system.conf`. + #[clap(long)] + system: bool, } #[tokio::main] @@ -44,7 +59,23 @@ async fn main() -> Result<()> { let args = Args::parse(); - let mut bus = bus::Bus::for_address(args.address.as_deref()).await?; + let config_path = if args.system { + PathBuf::from("/usr/share/dbus-1/system.conf") + } else if let Some(config_path) = args.config { + config_path + } else { + PathBuf::from("/usr/share/dbus-1/session.conf") + }; + eprintln!("reading configuration file {} ...", config_path.display()); + let config = Config::read_file(&config_path)?; + + let address = if let Some(address) = args.address { + Some(address) + } else { + config.listen.map(|address| format!("{address}")) + }; + + let mut bus = bus::Bus::for_address(address.as_deref()).await?; #[cfg(unix)] if let Some(fd) = args.ready_fd {