Skip to content

Commit

Permalink
server: Allow replacing current instance
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Oct 29, 2024
1 parent cfff210 commit 37198e7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version.workspace = true

[dependencies]
clap.workspace = true
enumflags2 = "0.7"
oo7 = { workspace = true, features = ["unstable"] }
rpassword = "7.3"
serde.workspace = true
Expand Down
9 changes: 8 additions & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct Args {
help = "Read a password from stdin, and use it to unlock the login keyring."
)]
login: bool,
#[arg(short, long, help = "Replace a running instance.")]
replace: bool,
}

#[tokio::main]
Expand All @@ -41,9 +43,14 @@ async fn main() -> Result<(), Error> {
secret = Some(Secret::from(password.into_bytes()));
}

let mut flags = zbus::fdo::RequestNameFlags::AllowReplacement.into();
if args.replace {
flags |= zbus::fdo::RequestNameFlags::ReplaceExisting;
}

tracing::info!("Starting {}", BINARY_NAME);

Service::run(secret).await?;
Service::run(secret, flags).await?;

std::future::pending::<()>().await;

Expand Down
15 changes: 11 additions & 4 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::{collections::HashMap, sync::Arc};

use enumflags2::BitFlags;
use oo7::{
dbus::{
api::{Properties, SecretInner},
Expand Down Expand Up @@ -186,10 +187,16 @@ impl Service {
}

impl Service {
pub async fn run(secret: Option<Secret>) -> Result<(), Error> {
let connection = zbus::connection::Builder::session()?
.name(oo7::dbus::api::Service::DESTINATION.as_deref().unwrap())?
.build()
pub async fn run(
secret: Option<Secret>,
flags: BitFlags<zbus::fdo::RequestNameFlags>,
) -> Result<(), Error> {
let connection = zbus::Connection::session().await?;
connection
.request_name_with_flags(
oo7::dbus::api::Service::DESTINATION.as_deref().unwrap(),
flags,
)
.await?;
let object_server = connection.object_server();
let service = Self {
Expand Down

0 comments on commit 37198e7

Please sign in to comment.