Skip to content

Commit

Permalink
🔧 Support --config, --session (default), and --system
Browse files Browse the repository at this point in the history
  • Loading branch information
jokeyrhyme committed Dec 7, 2024
1 parent 25cc7eb commit 4bbcbfa
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/bin/busd.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 `<listen>` element in the configuration file.
#[clap(short = 'a', long, value_parser)]
address: Option<String>,

/// Use the given configuration file.
#[clap(long)]
config: Option<PathBuf>,

/// Print the address of the message bus to standard output.
#[clap(long)]
print_address: bool,
Expand All @@ -36,6 +42,15 @@ struct Args {
#[cfg(unix)]
#[clap(long)]
ready_fd: Option<i32>,

/// 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]
Expand All @@ -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 {
Expand Down

0 comments on commit 4bbcbfa

Please sign in to comment.