Skip to content

Commit ca1b5b1

Browse files
authored
add pub/sub cli (#91)
Signed-off-by: Liang Zheng <[email protected]>
1 parent 6513e52 commit ca1b5b1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/bin/cli.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ enum Command {
3939
#[structopt(parse(try_from_str = duration_from_ms_str))]
4040
expires: Option<Duration>,
4141
},
42+
/// Publisher to send a message to a specific channel.
43+
Publish {
44+
/// Name of channel
45+
channel: String,
46+
47+
#[structopt(parse(from_str = bytes_from_str))]
48+
/// Message to publish
49+
message: Bytes,
50+
},
51+
/// Subscribe a client to a specific channel or channels.
52+
Subscribe {
53+
/// Specific channel or channels
54+
channels: Vec<String>,
55+
},
4256
}
4357

4458
/// Entry point for CLI tool.
@@ -93,6 +107,24 @@ async fn main() -> mini_redis::Result<()> {
93107
client.set_expires(&key, value, expires).await?;
94108
println!("OK");
95109
}
110+
Command::Publish { channel, message } => {
111+
client.publish(&channel, message.into()).await?;
112+
println!("Publish OK");
113+
}
114+
Command::Subscribe { channels } => {
115+
if channels.is_empty() {
116+
return Err("channel(s) must be provided".into());
117+
}
118+
let mut subscriber = client.subscribe(channels).await?;
119+
120+
// await messages on channels
121+
while let Some(msg) = subscriber.next_message().await? {
122+
println!(
123+
"got message from the channel: {}; message = {:?}",
124+
msg.channel, msg.content
125+
);
126+
}
127+
}
96128
}
97129

98130
Ok(())

0 commit comments

Comments
 (0)