Compare commits
1 Commits
main
...
status-sub
| Author | SHA1 | Date | |
|---|---|---|---|
|
4ea960edbf
|
@@ -1,5 +1,6 @@
|
|||||||
mod common;
|
mod common;
|
||||||
pub mod database_command;
|
pub mod database_command;
|
||||||
|
pub mod other_command;
|
||||||
pub mod user_command;
|
pub mod user_command;
|
||||||
|
|
||||||
#[cfg(feature = "mysql-admutils-compatibility")]
|
#[cfg(feature = "mysql-admutils-compatibility")]
|
||||||
|
|||||||
59
src/cli/other_command.rs
Normal file
59
src/cli/other_command.rs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
use clap::Parser;
|
||||||
|
use futures_util::{SinkExt, StreamExt};
|
||||||
|
|
||||||
|
use crate::core::protocol::{
|
||||||
|
ClientToServerMessageStream, Request, Response
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::common::erroneous_server_response;
|
||||||
|
|
||||||
|
#[allow(clippy::enum_variant_names)]
|
||||||
|
#[derive(Parser, Debug, Clone)]
|
||||||
|
pub enum OtherCommand {
|
||||||
|
/// Check if the tool is set up correctly, and the server is running.
|
||||||
|
#[command()]
|
||||||
|
Status(StatusArgs),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Parser, Debug, Clone)]
|
||||||
|
pub struct StatusArgs {
|
||||||
|
/// Print the information as JSON
|
||||||
|
#[arg(short, long)]
|
||||||
|
json: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handle_command(
|
||||||
|
command: OtherCommand,
|
||||||
|
server_connection: ClientToServerMessageStream,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
match command {
|
||||||
|
OtherCommand::Status(args) => status(args, server_connection).await,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// TODO: this should be moved all the way out to the main function, so that
|
||||||
|
/// we can teste the server connection before it fails to be established.
|
||||||
|
async fn status(
|
||||||
|
args: StatusArgs,
|
||||||
|
mut server_connection: ClientToServerMessageStream,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
if let Err(err) = server_connection.send(Request::Ping).await {
|
||||||
|
server_connection.close().await.ok();
|
||||||
|
anyhow::bail!(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
match server_connection.next().await {
|
||||||
|
Some(Ok(Response::Pong)) => (),
|
||||||
|
response => return erroneous_server_response(response),
|
||||||
|
};
|
||||||
|
|
||||||
|
server_connection.send(Request::Exit).await?;
|
||||||
|
|
||||||
|
if args.json {
|
||||||
|
// print_drop_users_output_status_json(&result);
|
||||||
|
} else {
|
||||||
|
// print_drop_users_output_status(&result);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -125,6 +125,8 @@ impl From<String> for MySQLDatabase {
|
|||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
pub enum Request {
|
pub enum Request {
|
||||||
|
Ping,
|
||||||
|
|
||||||
CreateDatabases(Vec<MySQLDatabase>),
|
CreateDatabases(Vec<MySQLDatabase>),
|
||||||
DropDatabases(Vec<MySQLDatabase>),
|
DropDatabases(Vec<MySQLDatabase>),
|
||||||
ListDatabases(Option<Vec<MySQLDatabase>>),
|
ListDatabases(Option<Vec<MySQLDatabase>>),
|
||||||
@@ -147,6 +149,8 @@ pub enum Request {
|
|||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
pub enum Response {
|
pub enum Response {
|
||||||
|
Pong,
|
||||||
|
|
||||||
// Specific data for specific commands
|
// Specific data for specific commands
|
||||||
CreateDatabases(CreateDatabasesOutput),
|
CreateDatabases(CreateDatabasesOutput),
|
||||||
DropDatabases(DropDatabasesOutput),
|
DropDatabases(DropDatabasesOutput),
|
||||||
|
|||||||
@@ -82,6 +82,9 @@ enum Command {
|
|||||||
#[command(flatten)]
|
#[command(flatten)]
|
||||||
User(cli::user_command::UserCommand),
|
User(cli::user_command::UserCommand),
|
||||||
|
|
||||||
|
#[command(flatten)]
|
||||||
|
Other(cli::other_command::OtherCommand),
|
||||||
|
|
||||||
#[command(hide = true)]
|
#[command(hide = true)]
|
||||||
Server(server::command::ServerArgs),
|
Server(server::command::ServerArgs),
|
||||||
|
|
||||||
@@ -247,6 +250,9 @@ fn tokio_run_command(command: Command, server_connection: StdUnixStream) -> anyh
|
|||||||
Command::Db(db_args) => {
|
Command::Db(db_args) => {
|
||||||
cli::database_command::handle_command(db_args, message_stream).await
|
cli::database_command::handle_command(db_args, message_stream).await
|
||||||
}
|
}
|
||||||
|
Command::Other(other_args) => {
|
||||||
|
cli::other_command::handle_command(other_args, message_stream).await
|
||||||
|
}
|
||||||
Command::Server(_) => unreachable!(),
|
Command::Server(_) => unreachable!(),
|
||||||
Command::GenerateCompletions(_) => unreachable!(),
|
Command::GenerateCompletions(_) => unreachable!(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -241,6 +241,8 @@ async fn handle_requests_for_single_session_with_db_connection(
|
|||||||
log::info!("Received request: {:#?}", request_to_display);
|
log::info!("Received request: {:#?}", request_to_display);
|
||||||
|
|
||||||
let response = match request {
|
let response = match request {
|
||||||
|
Request::Ping => Response::Pong,
|
||||||
|
|
||||||
Request::CreateDatabases(databases_names) => {
|
Request::CreateDatabases(databases_names) => {
|
||||||
let result = create_databases(databases_names, unix_user, db_connection).await;
|
let result = create_databases(databases_names, unix_user, db_connection).await;
|
||||||
Response::CreateDatabases(result)
|
Response::CreateDatabases(result)
|
||||||
|
|||||||
Reference in New Issue
Block a user