Skip to content

Commit 05f8c50

Browse files
Merge pull request #54 from rabbitmq/mk-first-definition-transformation
'definitions export': introduce --transformations
2 parents 93bcf37 + dba459b commit 05f8c50

File tree

3 files changed

+93
-57
lines changed

3 files changed

+93
-57
lines changed

Cargo.lock

Lines changed: 30 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,22 @@ fn definitions_subcommands() -> [Command; 4] {
11871187
.help("output file path or '-' for standard output")
11881188
.required(false)
11891189
.default_value("-"),
1190+
)
1191+
.arg(
1192+
Arg::new("transformations")
1193+
.long("transformations")
1194+
.short('t')
1195+
.long_help(
1196+
r#"
1197+
Comma-separated names of the transformations to apply to the definitions.
1198+
1199+
Supported names: strip_cmq_policies"
1200+
"#,
1201+
)
1202+
.num_args(1..)
1203+
.value_delimiter(',')
1204+
.action(ArgAction::Append)
1205+
.required(false),
11901206
);
11911207

11921208
let export_from_vhost_cmd = Command::new("export_from_vhost")

src/commands.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::process;
3030

3131
use rabbitmq_http_client::commons::BindingDestinationType;
3232
use rabbitmq_http_client::commons::QueueType;
33+
use rabbitmq_http_client::transformers::TransformationChain;
3334
use rabbitmq_http_client::{password_hashing, requests, responses};
3435

3536
type APIClient<'a> = Client<&'a str, &'a str, &'a str>;
@@ -907,6 +908,52 @@ pub fn rebalance_queues(client: APIClient) -> ClientResult<()> {
907908
pub fn export_cluster_wide_definitions(
908909
client: APIClient,
909910
command_args: &ArgMatches,
911+
) -> ClientResult<()> {
912+
let transformations = command_args
913+
.get_many::<String>("transformations")
914+
.unwrap_or_default();
915+
916+
if transformations.len() == 0 {
917+
export_cluster_wide_definitions_without_transformations(client, command_args)
918+
} else {
919+
let transformations = transformations
920+
.into_iter()
921+
.map(String::from)
922+
.collect::<Vec<_>>();
923+
export_and_transform_cluster_wide_definitions(client, command_args, transformations)
924+
}
925+
}
926+
927+
fn export_and_transform_cluster_wide_definitions(
928+
client: APIClient,
929+
command_args: &ArgMatches,
930+
transformations: Vec<String>,
931+
) -> ClientResult<()> {
932+
match client.export_cluster_wide_definitions_as_data() {
933+
Ok(mut defs0) => {
934+
let chain = TransformationChain::from(transformations);
935+
let defs1 = chain.apply(&mut defs0);
936+
let json = serde_json::to_string_pretty(&defs1).unwrap();
937+
938+
let path = command_args.get_one::<String>("file").unwrap();
939+
match path.as_str() {
940+
"-" => {
941+
println!("{}", &json);
942+
Ok(())
943+
}
944+
file => {
945+
_ = fs::write(file, &json);
946+
Ok(())
947+
}
948+
}
949+
}
950+
Err(err) => Err(err),
951+
}
952+
}
953+
954+
fn export_cluster_wide_definitions_without_transformations(
955+
client: APIClient,
956+
command_args: &ArgMatches,
910957
) -> ClientResult<()> {
911958
match client.export_cluster_wide_definitions() {
912959
Ok(definitions) => {

0 commit comments

Comments
 (0)