add pub/sub cli (#91)

Signed-off-by: Liang Zheng <zhengliang0901@gmail.com>
This commit is contained in:
Liang Zheng
2022-01-30 17:41:18 +08:00
committed by GitHub
parent 6513e5226c
commit ca1b5b16bd

View File

@@ -39,6 +39,20 @@ enum Command {
#[structopt(parse(try_from_str = duration_from_ms_str))]
expires: Option<Duration>,
},
/// Publisher to send a message to a specific channel.
Publish {
/// Name of channel
channel: String,
#[structopt(parse(from_str = bytes_from_str))]
/// Message to publish
message: Bytes,
},
/// Subscribe a client to a specific channel or channels.
Subscribe {
/// Specific channel or channels
channels: Vec<String>,
},
}
/// Entry point for CLI tool.
@@ -93,6 +107,24 @@ async fn main() -> mini_redis::Result<()> {
client.set_expires(&key, value, expires).await?;
println!("OK");
}
Command::Publish { channel, message } => {
client.publish(&channel, message.into()).await?;
println!("Publish OK");
}
Command::Subscribe { channels } => {
if channels.is_empty() {
return Err("channel(s) must be provided".into());
}
let mut subscriber = client.subscribe(channels).await?;
// await messages on channels
while let Some(msg) = subscriber.next_message().await? {
println!(
"got message from the channel: {}; message = {:?}",
msg.channel, msg.content
);
}
}
}
Ok(())