Files
muscl/src/client/commands/check_auth.rs
h7x4 4c3677d6d3
All checks were successful
Build and test / docs (push) Successful in 7m1s
Build and test / check-license (push) Successful in 57s
Build and test / check (push) Successful in 2m46s
Build and test / build (push) Successful in 3m12s
Build and test / test (push) Successful in 3m25s
clippy pedantic fix + get rid of a few unwraps
2025-12-23 14:12:39 +09:00

72 lines
1.8 KiB
Rust

use crate::{
client::commands::erroneous_server_response,
core::{
protocol::{
ClientToServerMessageStream, Request, Response,
print_check_authorization_output_status, print_check_authorization_output_status_json,
},
types::DbOrUser,
},
};
use clap::Parser;
use futures_util::SinkExt;
use tokio_stream::StreamExt;
#[derive(Parser, Debug, Clone)]
pub struct CheckAuthArgs {
/// The `MySQL` database(s) or user(s) to check authorization for
#[arg(num_args = 1.., value_name = "NAME")]
name: Vec<String>,
/// Treat the provided names as users instead of databases
#[arg(short, long)]
users: bool,
/// Print the information as JSON
#[arg(short, long)]
json: bool,
}
pub async fn check_authorization(
args: CheckAuthArgs,
mut server_connection: ClientToServerMessageStream,
) -> anyhow::Result<()> {
if args.name.is_empty() {
anyhow::bail!("No database/user names provided");
}
let payload = args
.name
.into_iter()
.map(|name| {
if args.users {
DbOrUser::User(name.into())
} else {
DbOrUser::Database(name.into())
}
})
.collect::<Vec<_>>();
let message = Request::CheckAuthorization(payload);
server_connection.send(message).await?;
let result = match server_connection.next().await {
Some(Ok(Response::CheckAuthorization(response))) => response,
response => return erroneous_server_response(response),
};
server_connection.send(Request::Exit).await?;
if args.json {
print_check_authorization_output_status_json(&result);
} else {
print_check_authorization_output_status(&result);
}
if result.values().any(std::result::Result::is_err) {
std::process::exit(1);
}
Ok(())
}