Skip to content

Commit c0114b8

Browse files
New command group: connections
It reuses the commands we already have under 'list' and 'close', and groups them together.
1 parent 26182c9 commit c0114b8

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Enhancements
66

7+
* `connections` is a new command group for operations on connections
78
* `policies set` and `policies update` are two new aliases for `policies declare`. The former follows the naming
89
used by `rabbitmqctl` and the latter reflects the fact that the command can be used to update an existing policy,
910
in particular, to override its definition

src/cli.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ pub fn parser(pre_flight_settings: PreFlightSettings) -> Command {
5656
.infer_subcommands(pre_flight_settings.infer_subcommands)
5757
.infer_long_args(pre_flight_settings.infer_long_options)
5858
.subcommands(close_subcommands(pre_flight_settings.clone()));
59+
let connections_group = Command::new("connections")
60+
.about("Operations on connections")
61+
.infer_subcommands(pre_flight_settings.infer_subcommands)
62+
.infer_long_args(pre_flight_settings.infer_long_options)
63+
.subcommands(connections_subcommands(pre_flight_settings.clone()));
5964
let declare_group = Command::new("declare")
6065
.about("Creates or declares objects")
6166
.infer_subcommands(pre_flight_settings.infer_subcommands)
@@ -283,6 +288,7 @@ pub fn parser(pre_flight_settings: PreFlightSettings) -> Command {
283288
let command_groups = [
284289
bindings_group,
285290
close_group,
291+
connections_group,
286292
declare_group,
287293
definitions_group,
288294
delete_group,
@@ -574,7 +580,7 @@ fn list_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 19] {
574580
.short('u')
575581
.long("username")
576582
.required(true)
577-
.help("Name of the user whose connections to list"),
583+
.help("Name of the user whose connections should be listed"),
578584
)
579585
.long_about("Lists client connections that authenticated with a specific username")
580586
.after_help(color_print::cformat!(
@@ -1842,6 +1848,53 @@ fn close_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 2] {
18421848
.map(|cmd| cmd.infer_long_args(pre_flight_settings.infer_long_options))
18431849
}
18441850

1851+
fn connections_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 4] {
1852+
let close_connection = Command::new("close")
1853+
.about("Closes a client connection")
1854+
.arg(
1855+
Arg::new("name")
1856+
.long("name")
1857+
.help("connection name (identifying string)")
1858+
.required(true),
1859+
);
1860+
let close_user_connections = Command::new("close_of_user")
1861+
.about("Closes all connections that are authenticated with a specific username")
1862+
.arg(
1863+
Arg::new("username")
1864+
.short('u')
1865+
.long("username")
1866+
.help("Name of the user whose connections should be closed")
1867+
.required(true),
1868+
);
1869+
let list_cmd = Command::new("list")
1870+
.long_about("Lists client connections")
1871+
.after_help(color_print::cformat!(
1872+
"<bold>Doc guide</bold>: {}",
1873+
CONNECTION_GUIDE_URL
1874+
));
1875+
let list_user_connections_cmd = Command::new("list_of_user")
1876+
.arg(
1877+
Arg::new("username")
1878+
.short('u')
1879+
.long("username")
1880+
.required(true)
1881+
.help("Name of the user whose connections should be listed"),
1882+
)
1883+
.long_about("Lists client connections that are authenticated with a specific username")
1884+
.after_help(color_print::cformat!(
1885+
"<bold>Doc guide</bold>: {}",
1886+
CONNECTION_GUIDE_URL
1887+
));
1888+
1889+
[
1890+
close_connection,
1891+
close_user_connections,
1892+
list_cmd,
1893+
list_user_connections_cmd,
1894+
]
1895+
.map(|cmd| cmd.infer_long_args(pre_flight_settings.infer_long_options))
1896+
}
1897+
18451898
fn definitions_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 4] {
18461899
let export_cmd = Command::new("export")
18471900
.about("Export cluster-wide definitions")
@@ -2365,7 +2418,7 @@ pub fn users_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 6]
23652418
.short('u')
23662419
.long("username")
23672420
.required(true)
2368-
.help("Name of the user whose connections to list"),
2421+
.help("Name of the user whose connections should be listed"),
23692422
)
23702423
.long_about("Lists client connections that authenticated with a specific username")
23712424
.after_help(color_print::cformat!(

src/main.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,22 @@ fn dispatch_common_subcommand(
299299
let result = commands::close_user_connections(client, second_level_args);
300300
res_handler.no_output_on_success(result);
301301
}
302+
("connections", "close") => {
303+
let result = commands::close_connection(client, second_level_args);
304+
res_handler.no_output_on_success(result);
305+
}
306+
("connections", "close_of_user") => {
307+
let result = commands::close_user_connections(client, second_level_args);
308+
res_handler.no_output_on_success(result);
309+
}
310+
("connections", "list") => {
311+
let result = commands::list_connections(client);
312+
res_handler.tabular_result(result)
313+
}
314+
("connections", "list_of_user") => {
315+
let result = commands::list_user_connections(client, second_level_args);
316+
res_handler.tabular_result(result)
317+
}
302318
("declare", "binding") => {
303319
let result = commands::declare_binding(client, &vhost, second_level_args);
304320
res_handler.no_output_on_success(result);

tests/connections_tests.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ mod test_helpers;
1616
use crate::test_helpers::*;
1717

1818
#[test]
19-
fn test_list_connections() -> Result<(), Box<dyn std::error::Error>> {
19+
fn test_list_connections1() -> Result<(), Box<dyn std::error::Error>> {
20+
run_succeeds(["connections", "list"]);
21+
22+
Ok(())
23+
}
24+
25+
#[test]
26+
fn test_list_connections2() -> Result<(), Box<dyn std::error::Error>> {
2027
run_succeeds(["list", "connections"]);
2128

2229
Ok(())
@@ -30,7 +37,21 @@ fn test_list_connections_table_styles() -> Result<(), Box<dyn std::error::Error>
3037
}
3138

3239
#[test]
33-
fn test_list_user_connections() -> Result<(), Box<dyn std::error::Error>> {
40+
fn test_list_user_connections1() -> Result<(), Box<dyn std::error::Error>> {
41+
run_succeeds([
42+
"--table-style",
43+
"markdown",
44+
"connections",
45+
"list_of_user",
46+
"--username",
47+
"monitoring",
48+
]);
49+
50+
Ok(())
51+
}
52+
53+
#[test]
54+
fn test_list_user_connections2() -> Result<(), Box<dyn std::error::Error>> {
3455
run_succeeds([
3556
"--table-style",
3657
"markdown",

0 commit comments

Comments
 (0)