This commit is contained in:
sxyazi 2024-06-20 17:55:27 +08:00
parent 5816d81f1b
commit 9ef7d27daa
No known key found for this signature in database
3 changed files with 3 additions and 42 deletions

View file

@ -18,8 +18,6 @@ pub(super) struct Args {
pub(super) enum Command {
/// Publish a message to remote instance(s).
Pub(CommandPub),
/// Publish a static message to all remote instances.
PubStatic(CommandPubStatic),
/// Manage packages.
Pack(CommandPack),
/// Subscribe to messages from all remote instances.
@ -66,35 +64,6 @@ impl CommandPub {
}
}
#[derive(clap::Args)]
pub(super) struct CommandPubStatic {
/// The kind of message.
#[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>,
/// Send the message with a JSON body.
#[arg(long)]
pub(super) json: Option<String>,
}
impl CommandPubStatic {
#[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 {
Ok("".into())
}
}
}
#[derive(clap::Args)]
#[command(arg_required_else_help = true)]
pub(super) struct CommandPack {

View file

@ -19,14 +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 {
eprintln!("Cannot send message: {e}");
std::process::exit(1);
}
}
Command::PubStatic(cmd) => {
yazi_dds::init();
if let Err(e) = yazi_dds::Client::shot(&cmd.kind, 0, Some(cmd.severity), &cmd.body()?).await {
if let Err(e) = yazi_dds::Client::shot(&cmd.kind, cmd.receiver()?, &cmd.body()?).await {
eprintln!("Cannot send message: {e}");
std::process::exit(1);
}

View file

@ -64,12 +64,11 @@ impl Client {
}
/// Connect to an existing server to send a single message.
pub async fn shot(kind: &str, receiver: u64, severity: Option<u16>, body: &str) -> Result<()> {
pub async fn shot(kind: &str, receiver: u64, body: &str) -> Result<()> {
Body::validate(kind)?;
let sender = severity.map(Into::into).unwrap_or(*ID);
let payload = format!(
"{}\n{kind},{receiver},{sender},{body}\n{}\n",
"{}\n{kind},{receiver},{ID},{body}\n{}\n",
Payload::new(BodyHi::borrowed(Default::default())),
Payload::new(BodyBye::owned())
);