Skip to content

Commit 15b6db8

Browse files
add "show help" command (#505)
This commit adds a new function to handle notify and use it in the SHOW HELP command, which displays the available options in the admin console. Also, adding Fabrízio as a co-author for all the help with the protocol and the help to structure this PR. Signed-off-by: Sebastian Webber <sebastian@swebber.me> Co-authored-by: Fabrízio de Royes Mello <fabriziomello@gmail.com>
1 parent b2e6dfd commit 15b6db8

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/admin.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::pool::BanReason;
22
use crate::stats::pool::PoolStats;
33
use bytes::{Buf, BufMut, BytesMut};
4-
use log::{error, info, trace};
4+
use log::{debug, error, info, trace};
55
use nix::sys::signal::{self, Signal};
66
use nix::unistd::Pid;
77
use std::collections::HashMap;
@@ -84,6 +84,10 @@ where
8484
shutdown(stream).await
8585
}
8686
"SHOW" => match query_parts[1].to_ascii_uppercase().as_str() {
87+
"HELP" => {
88+
trace!("SHOW HELP");
89+
show_help(stream).await
90+
}
8791
"BANS" => {
8892
trace!("SHOW BANS");
8993
show_bans(stream).await
@@ -271,6 +275,45 @@ where
271275
write_all_half(stream, &res).await
272276
}
273277

278+
/// Show all available options.
279+
async fn show_help<T>(stream: &mut T) -> Result<(), Error>
280+
where
281+
T: tokio::io::AsyncWrite + std::marker::Unpin,
282+
{
283+
let mut res = BytesMut::new();
284+
285+
let detail_msg = vec![
286+
"",
287+
"SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|USERS|VERSION",
288+
// "SHOW PEERS|PEER_POOLS", // missing PEERS|PEER_POOLS
289+
// "SHOW FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEM|STATE", // missing FDS|SOCKETS|ACTIVE_SOCKETS|MEM|STATE
290+
"SHOW LISTS",
291+
// "SHOW DNS_HOSTS|DNS_ZONES", // missing DNS_HOSTS|DNS_ZONES
292+
"SHOW STATS", // missing STATS_TOTALS|STATS_AVERAGES|TOTALS
293+
"SET key = arg",
294+
"RELOAD",
295+
"PAUSE [<db>, <user>]",
296+
"RESUME [<db>, <user>]",
297+
// "DISABLE <db>", // missing
298+
// "ENABLE <db>", // missing
299+
// "RECONNECT [<db>]", missing
300+
// "KILL <db>",
301+
// "SUSPEND",
302+
"SHUTDOWN",
303+
// "WAIT_CLOSE [<db>]", // missing
304+
];
305+
306+
res.put(notify("Console usage", detail_msg.join("\n\t")));
307+
res.put(command_complete("SHOW"));
308+
309+
// ReadyForQuery
310+
res.put_u8(b'Z');
311+
res.put_i32(5);
312+
res.put_u8(b'I');
313+
314+
write_all_half(stream, &res).await
315+
}
316+
274317
/// Show shards and replicas.
275318
async fn show_databases<T>(stream: &mut T) -> Result<(), Error>
276319
where

src/messages.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,26 @@ pub fn command_complete(command: &str) -> BytesMut {
530530
res
531531
}
532532

533+
/// Create a notify message.
534+
pub fn notify(message: &str, details: String) -> BytesMut {
535+
let mut notify_cmd = BytesMut::new();
536+
537+
notify_cmd.put_slice("SNOTICE\0".as_bytes());
538+
notify_cmd.put_slice("C00000\0".as_bytes());
539+
notify_cmd.put_slice(format!("M{}\0", message).as_bytes());
540+
notify_cmd.put_slice(format!("D{}\0", details).as_bytes());
541+
542+
// this extra byte says that is the end of the package
543+
notify_cmd.put_u8(0);
544+
545+
let mut res = BytesMut::new();
546+
res.put_u8(b'N');
547+
res.put_i32(notify_cmd.len() as i32 + 4);
548+
res.put(notify_cmd);
549+
550+
res
551+
}
552+
533553
pub fn flush() -> BytesMut {
534554
let mut bytes = BytesMut::new();
535555
bytes.put_u8(b'H');

0 commit comments

Comments
 (0)