Skip to content

'definitions export': introduce --transformations #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 30 additions & 57 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,22 @@ fn definitions_subcommands() -> [Command; 4] {
.help("output file path or '-' for standard output")
.required(false)
.default_value("-"),
)
.arg(
Arg::new("transformations")
.long("transformations")
.short('t')
.long_help(
r#"
Comma-separated names of the transformations to apply to the definitions.

Supported names: strip_cmq_policies"
"#,
)
.num_args(1..)
.value_delimiter(',')
.action(ArgAction::Append)
.required(false),
);

let export_from_vhost_cmd = Command::new("export_from_vhost")
Expand Down
47 changes: 47 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::process;

use rabbitmq_http_client::commons::BindingDestinationType;
use rabbitmq_http_client::commons::QueueType;
use rabbitmq_http_client::transformers::TransformationChain;
use rabbitmq_http_client::{password_hashing, requests, responses};

type APIClient<'a> = Client<&'a str, &'a str, &'a str>;
Expand Down Expand Up @@ -907,6 +908,52 @@ pub fn rebalance_queues(client: APIClient) -> ClientResult<()> {
pub fn export_cluster_wide_definitions(
client: APIClient,
command_args: &ArgMatches,
) -> ClientResult<()> {
let transformations = command_args
.get_many::<String>("transformations")
.unwrap_or_default();

if transformations.len() == 0 {
export_cluster_wide_definitions_without_transformations(client, command_args)
} else {
let transformations = transformations
.into_iter()
.map(String::from)
.collect::<Vec<_>>();
export_and_transform_cluster_wide_definitions(client, command_args, transformations)
}
}

fn export_and_transform_cluster_wide_definitions(
client: APIClient,
command_args: &ArgMatches,
transformations: Vec<String>,
) -> ClientResult<()> {
match client.export_cluster_wide_definitions_as_data() {
Ok(mut defs0) => {
let chain = TransformationChain::from(transformations);
let defs1 = chain.apply(&mut defs0);
let json = serde_json::to_string_pretty(&defs1).unwrap();

let path = command_args.get_one::<String>("file").unwrap();
match path.as_str() {
"-" => {
println!("{}", &json);
Ok(())
}
file => {
_ = fs::write(file, &json);
Ok(())
}
}
}
Err(err) => Err(err),
}
}

fn export_cluster_wide_definitions_without_transformations(
client: APIClient,
command_args: &ArgMatches,
) -> ClientResult<()> {
match client.export_cluster_wide_definitions() {
Ok(definitions) => {
Expand Down