Skip to content

Commit

Permalink
feat!: DDS client-server version check (#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Jun 1, 2024
1 parent add801f commit e4d6712
Show file tree
Hide file tree
Showing 21 changed files with 136 additions and 72 deletions.
17 changes: 9 additions & 8 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion yazi-adaptor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ imagesize = "0.12.0"
kamadak-exif = "0.5.5"
ratatui = "0.26.3"
scopeguard = "1.2.0"
tokio = { version = "1.37.0", features = [ "full" ] }
tokio = { version = "1.38.0", features = [ "full" ] }

# Logging
tracing = { version = "0.1.40", features = [ "max_level_debug", "release_max_level_warn" ] }
2 changes: 1 addition & 1 deletion yazi-boot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ yazi-shared = { path = "../yazi-shared", version = "0.2.5" }

# External dependencies
clap = { version = "4.5.4", features = [ "derive" ] }
serde = { version = "1.0.202", features = [ "derive" ] }
serde = { version = "1.0.203", features = [ "derive" ] }

[build-dependencies]
clap = { version = "4.5.4", features = [ "derive" ] }
Expand Down
2 changes: 1 addition & 1 deletion yazi-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ clap = { version = "4.5.4", features = [ "derive" ] }
crossterm = "0.27.0"
md-5 = "0.10.6"
serde_json = "1.0.117"
tokio = { version = "1.37.0", features = [ "full" ] }
tokio = { version = "1.38.0", features = [ "full" ] }
toml_edit = "0.22.13"

[build-dependencies]
Expand Down
31 changes: 21 additions & 10 deletions yazi-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ pub(super) enum Command {

#[derive(clap::Args)]
pub(super) struct CommandPub {
/// The receiver ID.
#[arg(index = 1)]
pub(super) receiver: u64,
/// The kind of message.
#[arg(index = 2)]
#[arg(index = 1)]
pub(super) kind: String,
/// The receiver ID.
#[arg(index = 2)]
pub(super) receiver: Option<u64>,
/// Send the message with a string body.
#[arg(long)]
pub(super) str: Option<String>,
Expand All @@ -41,26 +41,37 @@ pub(super) struct CommandPub {
}

impl CommandPub {
#[allow(dead_code)]
pub(super) fn receiver(&self) -> Result<u64> {
if let Some(receiver) = self.receiver {
Ok(receiver)
} else if let Ok(s) = std::env::var("YAZI_ID") {
Ok(s.parse()?)
} else {
bail!("No receiver ID provided, also no YAZI_ID environment variable found.")
}
}

#[allow(dead_code)]
pub(super) fn body(&self) -> Result<Cow<str>> {
if let Some(json) = &self.json {
Ok(json.into())
} else if let Some(str) = &self.str {
Ok(serde_json::to_string(str)?.into())
} else {
bail!("No body provided");
Ok("".into())
}
}
}

#[derive(clap::Args)]
pub(super) struct CommandPubStatic {
/// The severity of the message.
#[arg(index = 1)]
pub(super) severity: u16,
/// The kind of message.
#[arg(index = 2)]
#[arg(index = 1)]
pub(super) kind: String,
/// The severity of the message.
#[arg(index = 2)]
pub(super) severity: u16,
/// Send the message with a string body.
#[arg(long)]
pub(super) str: Option<String>,
Expand All @@ -77,7 +88,7 @@ impl CommandPubStatic {
} else if let Some(str) = &self.str {
Ok(serde_json::to_string(str)?.into())
} else {
bail!("No body provided");
Ok("".into())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion yazi-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn main() -> anyhow::Result<()> {
match Args::parse().command {
Command::Pub(cmd) => {
yazi_dds::init();
if let Err(e) = yazi_dds::Client::shot(&cmd.kind, cmd.receiver, None, &cmd.body()?).await {
if let Err(e) = yazi_dds::Client::shot(&cmd.kind, cmd.receiver()?, None, &cmd.body()?).await {
eprintln!("Cannot send message: {e}");
std::process::exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion yazi-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ crossterm = "0.27.0"
globset = "0.4.14"
indexmap = "2.2.6"
ratatui = "0.26.3"
serde = { version = "1.0.202", features = [ "derive" ] }
serde = { version = "1.0.203", features = [ "derive" ] }
shell-words = "1.1.0"
toml = { version = "0.8.13", features = [ "preserve_order" ] }
validator = { version = "0.18.1", features = [ "derive" ] }
4 changes: 2 additions & 2 deletions yazi-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ parking_lot = "0.12.3"
ratatui = "0.26.3"
regex = "1.10.4"
scopeguard = "1.2.0"
serde = "1.0.202"
serde = "1.0.203"
shell-words = "1.1.0"
tokio = { version = "1.37.0", features = [ "full" ] }
tokio = { version = "1.38.0", features = [ "full" ] }
tokio-stream = "0.1.15"
tokio-util = "0.7.11"
unicode-width = "0.1.12"
Expand Down
32 changes: 16 additions & 16 deletions yazi-core/src/tab/commands/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ bitflags! {
pub struct Opt: u8 {
const FIND = 0b00001;
const VISUAL = 0b00010;
const SELECT = 0b00100;
const FILTER = 0b01000;
const FILTER = 0b00100;
const SELECT = 0b01000;
const SEARCH = 0b10000;
}
}
Expand All @@ -21,8 +21,8 @@ impl From<Cmd> for Opt {
("all", true) => Self::all(),
("find", true) => acc | Self::FIND,
("visual", true) => acc | Self::VISUAL,
("select", true) => acc | Self::SELECT,
("filter", true) => acc | Self::FILTER,
("select", true) => acc | Self::SELECT,
("search", true) => acc | Self::SEARCH,
_ => acc,
}
Expand All @@ -36,8 +36,8 @@ impl Tab {
if opt.is_empty() {
_ = self.escape_find()
|| self.escape_visual()
|| self.escape_select()
|| self.escape_filter()
|| self.escape_select()
|| self.escape_search();
return;
}
Expand All @@ -48,12 +48,12 @@ impl Tab {
if opt.contains(Opt::VISUAL) {
self.escape_visual();
}
if opt.contains(Opt::SELECT) {
self.escape_select();
}
if opt.contains(Opt::FILTER) {
self.escape_filter();
}
if opt.contains(Opt::SELECT) {
self.escape_select();
}
if opt.contains(Opt::SEARCH) {
self.escape_search();
}
Expand All @@ -70,24 +70,24 @@ impl Tab {
true
}

pub fn escape_select(&mut self) -> bool {
if self.selected.is_empty() {
pub fn escape_filter(&mut self) -> bool {
if self.current.files.filter().is_none() {
return false;
}

self.selected.clear();
if self.current.hovered().is_some_and(|h| h.is_dir()) {
ManagerProxy::peek(true);
}
self.filter_do(super::filter::Opt::default());
render_and!(true)
}

pub fn escape_filter(&mut self) -> bool {
if self.current.files.filter().is_none() {
pub fn escape_select(&mut self) -> bool {
if self.selected.is_empty() {
return false;
}

self.filter_do(super::filter::Opt::default());
self.selected.clear();
if self.current.hovered().is_some_and(|h| h.is_dir()) {
ManagerProxy::peek(true);
}
render_and!(true)
}

Expand Down
7 changes: 5 additions & 2 deletions yazi-dds/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ yazi-shared = { path = "../yazi-shared", version = "0.2.5" }
anyhow = "1.0.86"
mlua = { version = "0.9.8", features = [ "lua54" ] }
parking_lot = "0.12.3"
serde = { version = "1.0.202", features = [ "derive" ] }
serde = { version = "1.0.203", features = [ "derive" ] }
serde_json = "1.0.117"
tokio = { version = "1.37.0", features = [ "full" ] }
tokio = { version = "1.38.0", features = [ "full" ] }
tokio-stream = "0.1.15"
tokio-util = "0.7.11"

[build-dependencies]
vergen = { version = "8.3.1", features = [ "build", "git", "gitcl" ] }

[target."cfg(unix)".dependencies]
uzers = "0.12.0"
9 changes: 9 additions & 0 deletions yazi-dds/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::error::Error;

use vergen::EmitBuilder;

fn main() -> Result<(), Box<dyn Error>> {
EmitBuilder::builder().git_sha(true).emit()?;

Ok(())
}
4 changes: 2 additions & 2 deletions yazi-dds/src/body/bye.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use serde::{Deserialize, Serialize};
use super::Body;

#[derive(Debug, Serialize, Deserialize)]
pub struct BodyBye {}
pub struct BodyBye;

impl BodyBye {
#[inline]
pub fn borrowed() -> Body<'static> { Self {}.into() }
pub fn owned() -> Body<'static> { Self.into() }
}

impl<'a> From<BodyBye> for Body<'a> {
Expand Down
12 changes: 10 additions & 2 deletions yazi-dds/src/body/hey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ use std::collections::HashMap;
use mlua::{ExternalResult, IntoLua, Lua, Value};
use serde::{Deserialize, Serialize};

use super::Body;
use super::{Body, BodyHi};
use crate::Peer;

#[derive(Debug, Serialize, Deserialize)]
pub struct BodyHey {
pub peers: HashMap<u64, Peer>,
pub peers: HashMap<u64, Peer>,
pub version: String,
}

impl BodyHey {
#[inline]
pub fn owned(peers: HashMap<u64, Peer>) -> Body<'static> {
Self { peers, version: BodyHi::version() }.into()
}
}

impl From<BodyHey> for Body<'_> {
Expand Down
10 changes: 9 additions & 1 deletion yazi-dds/src/body/hi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ use super::Body;
#[derive(Debug, Serialize, Deserialize)]
pub struct BodyHi<'a> {
pub abilities: HashSet<Cow<'a, String>>,
pub version: String,
}

impl<'a> BodyHi<'a> {
#[inline]
pub fn borrowed(abilities: HashSet<&'a String>) -> Body<'a> {
Self { abilities: abilities.into_iter().map(Cow::Borrowed).collect() }.into()
Self {
abilities: abilities.into_iter().map(Cow::Borrowed).collect(),
version: Self::version(),
}
.into()
}

#[inline]
pub fn version() -> String { format!("{} {}", env!("CARGO_PKG_VERSION"), env!("VERGEN_GIT_SHA")) }
}

impl<'a> From<BodyHi<'a>> for Body<'a> {
Expand Down
24 changes: 19 additions & 5 deletions yazi-dds/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::{HashMap, HashSet}, mem, str::FromStr};

use anyhow::Result;
use anyhow::{bail, Result};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use tokio::{io::AsyncWriteExt, select, sync::mpsc, task::JoinHandle, time};
Expand Down Expand Up @@ -69,20 +69,34 @@ impl Client {
let payload = format!(
"{}\n{kind},{receiver},{sender},{body}\n{}\n",
Payload::new(BodyHi::borrowed(Default::default())),
Payload::new(BodyBye::borrowed())
Payload::new(BodyBye::owned())
);

let (mut lines, mut writer) = Stream::connect().await?;
writer.write_all(payload.as_bytes()).await?;
writer.flush().await?;
drop(writer);

while let Ok(Some(s)) = lines.next_line().await {
if matches!(s.split(',').next(), Some(kind) if kind == "bye") {
break;
let mut version = None;
while let Ok(Some(line)) = lines.next_line().await {
match line.split(',').next() {
Some("hey") if version.is_none() => {
if let Ok(Body::Hey(hey)) = Payload::from_str(&line).map(|p| p.body) {
version = Some(hey.version);
}
}
Some("bye") => break,
_ => {}
}
}

if version != Some(BodyHi::version()) {
bail!(
"Incompatible version (Ya {}, Yazi {})",
BodyHi::version(),
version.as_deref().unwrap_or("Unknown")
);
}
Ok(())
}

Expand Down
Loading

0 comments on commit e4d6712

Please sign in to comment.