feat: checks in ya pub and ya emit subcommands to verify receiver exists and has necessary abilities (#2696)

This commit is contained in:
三咲雅 · Misaki Masa 2025-04-30 00:24:29 +08:00 committed by GitHub
parent eba9f1e4f0
commit fbf8345d90
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 35 additions and 26 deletions

28
Cargo.lock generated
View file

@ -389,9 +389,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_complete" name = "clap_complete"
version = "4.5.47" version = "4.5.48"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06f5378ea264ad4f82bbc826628b5aad714a75abf6ece087e923010eb937fb6" checksum = "be8c97f3a6f02b9e24cadc12aaba75201d18754b53ea0a9d99642f806ccdb4c9"
dependencies = [ dependencies = [
"clap", "clap",
] ]
@ -787,7 +787,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e" checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -2132,7 +2132,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys 0.4.15", "linux-raw-sys 0.4.15",
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -2145,7 +2145,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys 0.9.4", "linux-raw-sys 0.9.4",
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -2595,9 +2595,9 @@ dependencies = [
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.8.21" version = "0.8.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900f6c86a685850b1bc9f6223b20125115ee3f31e01207d81655bbcc0aea9231" checksum = "05ae329d1f08c4d17a59bed7ff5b5a769d062e64a62d34a3261b219e62cd5aae"
dependencies = [ dependencies = [
"serde", "serde",
"serde_spanned", "serde_spanned",
@ -2616,9 +2616,9 @@ dependencies = [
[[package]] [[package]]
name = "toml_edit" name = "toml_edit"
version = "0.22.25" version = "0.22.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10558ed0bd2a1562e630926a2d1f0b98c827da99fabd3fe20920a59642504485" checksum = "310068873db2c5b3e7659d2cc35d21855dbafa50d1ce336397c666e3cb08137e"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"serde", "serde",
@ -2630,9 +2630,9 @@ dependencies = [
[[package]] [[package]]
name = "toml_write" name = "toml_write"
version = "0.1.0" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28391a4201ba7eb1984cfeb6862c0b3ea2cfe23332298967c749dddc0d6cd976" checksum = "bfb942dfe1d8e29a7ee7fcbde5bd2b9a25fb89aa70caea2eba3bee836ff41076"
[[package]] [[package]]
name = "tracing" name = "tracing"
@ -3000,7 +3000,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -3205,9 +3205,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]] [[package]]
name = "winnow" name = "winnow"
version = "0.7.6" version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63d3fcd9bba44b03821e7d699eeee959f3126dcc4aa8e4ae18ec617c2a5cea10" checksum = "6cb8234a863ea0e8cd7284fcdd4f145233eb00fee02bbdd9861aec44e6477bc5"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]

View file

@ -48,7 +48,7 @@ syntect = { version = "5.2.0", default-features = false, features =
tokio = { version = "1.44.2", features = [ "full" ] } tokio = { version = "1.44.2", features = [ "full" ] }
tokio-stream = "0.1.17" tokio-stream = "0.1.17"
tokio-util = "0.7.15" tokio-util = "0.7.15"
toml = { version = "0.8.21" } toml = { version = "0.8.22" }
tracing = { version = "0.1.41", features = [ "max_level_debug", "release_max_level_debug" ] } tracing = { version = "0.1.41", features = [ "max_level_debug", "release_max_level_debug" ] }
twox-hash = { version = "2.1.0", default-features = false, features = [ "std", "random", "xxhash3_128" ] } twox-hash = { version = "2.1.0", default-features = false, features = [ "std", "random", "xxhash3_128" ] }
unicode-width = "0.2.0" unicode-width = "0.2.0"

View file

@ -25,7 +25,7 @@ yazi-shared = { path = "../yazi-shared", version = "25.4.8" }
# External dependencies # External dependencies
clap = { workspace = true } clap = { workspace = true }
clap_complete = "4.5.47" clap_complete = "4.5.48"
clap_complete_fig = "4.5.2" clap_complete_fig = "4.5.2"
clap_complete_nushell = "4.5.5" clap_complete_nushell = "4.5.5"
vergen-gitcl = { version = "1.0.8", features = [ "build", "rustc" ] } vergen-gitcl = { version = "1.0.8", features = [ "build", "rustc" ] }

View file

@ -31,7 +31,7 @@ yazi-shared = { path = "../yazi-shared", version = "25.4.8" }
# External build dependencies # External build dependencies
anyhow = { workspace = true } anyhow = { workspace = true }
clap = { workspace = true } clap = { workspace = true }
clap_complete = "4.5.47" clap_complete = "4.5.48"
clap_complete_fig = "4.5.2" clap_complete_fig = "4.5.2"
clap_complete_nushell = "4.5.5" clap_complete_nushell = "4.5.5"
serde_json = { workspace = true } serde_json = { workspace = true }

View file

@ -2,7 +2,7 @@ use std::borrow::Cow;
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use clap::{Parser, Subcommand, command}; use clap::{Parser, Subcommand, command};
use yazi_shared::event::Cmd; use yazi_shared::{Id, event::Cmd};
#[derive(Parser)] #[derive(Parser)]
#[command(name = "Ya", about, long_about = None)] #[command(name = "Ya", about, long_about = None)]
@ -43,7 +43,7 @@ pub(super) struct CommandEmit {
#[derive(clap::Args)] #[derive(clap::Args)]
pub(super) struct CommandEmitTo { pub(super) struct CommandEmitTo {
/// The receiver ID. /// The receiver ID.
pub(super) receiver: u64, pub(super) receiver: Id,
/// The name of the command. /// The name of the command.
pub(super) name: String, pub(super) name: String,
/// The arguments of the command. /// The arguments of the command.
@ -89,7 +89,7 @@ pub(super) struct CommandPub {
impl CommandPub { impl CommandPub {
#[allow(dead_code)] #[allow(dead_code)]
pub(super) fn receiver() -> Result<u64> { pub(super) fn receiver() -> Result<Id> {
if let Some(s) = std::env::var("YAZI_PID").ok().filter(|s| !s.is_empty()) { if let Some(s) = std::env::var("YAZI_PID").ok().filter(|s| !s.is_empty()) {
Ok(s.parse()?) Ok(s.parse()?)
} else { } else {
@ -102,7 +102,7 @@ impl CommandPub {
pub(super) struct CommandPubTo { pub(super) struct CommandPubTo {
/// The receiver ID. /// The receiver ID.
#[arg(index = 1)] #[arg(index = 1)]
pub(super) receiver: u64, pub(super) receiver: Id,
/// The kind of message. /// The kind of message.
#[arg(index = 2)] #[arg(index = 2)]
pub(super) kind: String, pub(super) kind: String,

View file

@ -65,7 +65,7 @@ impl Client {
} }
/// Connect to an existing server to send a single message. /// Connect to an existing server to send a single message.
pub async fn shot(kind: &str, receiver: u64, body: &str) -> Result<()> { pub async fn shot(kind: &str, receiver: Id, body: &str) -> Result<()> {
Body::validate(kind)?; Body::validate(kind)?;
let payload = format!( let payload = format!(
@ -79,12 +79,12 @@ impl Client {
writer.flush().await?; writer.flush().await?;
drop(writer); drop(writer);
let mut version = None; let (mut peers, mut version) = Default::default();
while let Ok(Some(line)) = lines.next_line().await { while let Ok(Some(line)) = lines.next_line().await {
match line.split(',').next() { match line.split(',').next() {
Some("hey") if version.is_none() => { Some("hey") => {
if let Ok(Body::Hey(hey)) = Payload::from_str(&line).map(|p| p.body) { if let Ok(Body::Hey(hey)) = Payload::from_str(&line).map(|p| p.body) {
version = Some(hey.version); (peers, version) = (hey.peers, Some(hey.version));
} }
} }
Some("bye") => break, Some("bye") => break,
@ -94,11 +94,20 @@ impl Client {
if version.as_deref() != Some(BodyHi::version()) { if version.as_deref() != Some(BodyHi::version()) {
bail!( bail!(
"Incompatible version (Ya {}, Yazi {}), please restart all `ya` and `yazi` processes if you upgrade either one.", "Incompatible version (Ya {}, Yazi {}). Restart all `ya` and `yazi` processes if you upgrade either one.",
BodyHi::version(), BodyHi::version(),
version.as_deref().unwrap_or("Unknown") version.as_deref().unwrap_or("Unknown")
); );
} }
match peers.get(&receiver).map(|p| p.able(kind)) {
Some(true) => {}
Some(false) => {
bail!("Receiver `{receiver}` does not have the ability to receive `{kind}` messages.")
}
None => bail!("Receiver `{receiver}` not found. Check if the receiver is running."),
}
Ok(()) Ok(())
} }