-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93baf8f
commit f5bdc5a
Showing
10 changed files
with
427 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::ipc::commands::Command; | ||
use crate::ipc::responses::Response; | ||
use clap::Parser; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Parser, Debug, Serialize, Deserialize)] | ||
#[command(version)] | ||
pub struct Args { | ||
#[command(subcommand)] | ||
pub command: Option<Command>, | ||
} | ||
|
||
pub fn handle_response(response: Response) { | ||
match response { | ||
Response::Ok => println!("ok"), | ||
Response::OkValue { value } => println!("ok\n{value}"), | ||
Response::Err { message } => eprintln!("error\n{}", message.unwrap_or_default()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use super::Ipc; | ||
use crate::ipc::{Command, Response}; | ||
use color_eyre::Result; | ||
use color_eyre::{Help, Report}; | ||
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
use tokio::net::UnixStream; | ||
|
||
impl Ipc { | ||
/// Sends a command to the IPC server. | ||
/// The server response is returned. | ||
pub async fn send(&self, command: Command) -> Result<Response> { | ||
let mut stream = match UnixStream::connect(&self.path).await { | ||
Ok(stream) => Ok(stream), | ||
Err(err) => Err(Report::new(err) | ||
.wrap_err("Failed to connect to Ironbar IPC server") | ||
.suggestion("Is Ironbar running?")), | ||
}?; | ||
|
||
let write_buffer = serde_json::to_vec(&command)?; | ||
stream.write_all(&write_buffer).await?; | ||
|
||
let mut read_buffer = vec![0; 1024]; | ||
let bytes = stream.read(&mut read_buffer).await?; | ||
|
||
let response = serde_json::from_slice(&read_buffer[..bytes])?; | ||
Ok(response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use clap::Subcommand; | ||
use serde::{Deserialize, Serialize}; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Subcommand, Debug, Serialize, Deserialize)] | ||
#[serde(tag = "type")] | ||
pub enum Command { | ||
/// Return "ok" | ||
Ping, | ||
|
||
/// Open the GTK inspector | ||
Inspect, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
mod client; | ||
pub mod commands; | ||
pub mod responses; | ||
mod server; | ||
|
||
use std::path::PathBuf; | ||
use tracing::warn; | ||
|
||
pub use commands::Command; | ||
pub use responses::Response; | ||
|
||
#[derive(Debug)] | ||
pub struct Ipc { | ||
path: PathBuf, | ||
} | ||
|
||
impl Ipc { | ||
/// Creates a new IPC instance. | ||
/// This can be used as both a server and client. | ||
pub fn new() -> Self { | ||
let ipc_socket_file = std::env::var("XDG_RUNTIME_DIR") | ||
.map_or_else(|_| PathBuf::from("/tmp"), PathBuf::from) | ||
.join("ironbar-ipc.sock"); | ||
|
||
if format!("{}", ipc_socket_file.display()).len() > 100 { | ||
warn!("The IPC socket file's absolute path exceeds 100 bytes, the socket may fail to create."); | ||
} | ||
|
||
Self { | ||
path: ipc_socket_file, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(tag = "type")] | ||
pub enum Response { | ||
Ok, | ||
Err { message: Option<String> }, | ||
} | ||
|
||
impl Response { | ||
/// Creates a new `Response::Error`. | ||
pub fn error(message: &str) -> Self { | ||
Self::Err { | ||
message: Some(message.to_string()), | ||
} | ||
} | ||
} |
Oops, something went wrong.