2024-04-21 06:03:25 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate prettytable;
|
|
|
|
|
2024-08-05 22:37:23 +02:00
|
|
|
#[cfg(feature = "mysql-admutils-compatibility")]
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
#[cfg(feature = "mysql-admutils-compatibility")]
|
|
|
|
use crate::cli::mysql_admutils_compatibility::{mysql_dbadm, mysql_useradm};
|
|
|
|
|
2024-04-20 04:17:16 +02:00
|
|
|
use clap::Parser;
|
|
|
|
|
2024-04-21 06:03:25 +02:00
|
|
|
mod cli;
|
|
|
|
mod core;
|
2024-04-20 04:17:16 +02:00
|
|
|
|
2024-04-23 00:36:06 +02:00
|
|
|
#[cfg(feature = "tui")]
|
|
|
|
mod tui;
|
|
|
|
|
2024-04-20 04:17:16 +02:00
|
|
|
#[derive(Parser)]
|
|
|
|
struct Args {
|
2024-04-21 06:03:25 +02:00
|
|
|
#[command(subcommand)]
|
2024-04-20 04:17:16 +02:00
|
|
|
command: Command,
|
2024-04-21 06:03:25 +02:00
|
|
|
|
|
|
|
#[command(flatten)]
|
2024-07-09 19:54:27 +02:00
|
|
|
config_overrides: core::config::GlobalConfigArgs,
|
2024-04-23 00:36:06 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "tui")]
|
|
|
|
#[arg(short, long, alias = "tui", global = true)]
|
|
|
|
interactive: bool,
|
2024-04-20 04:17:16 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 19:54:27 +02:00
|
|
|
/// Database administration tool for non-admin users to manage their own MySQL databases and users.
|
2024-04-21 06:03:25 +02:00
|
|
|
///
|
2024-07-09 19:54:27 +02:00
|
|
|
/// This tool allows you to manage users and databases in MySQL.
|
|
|
|
///
|
|
|
|
/// You are only allowed to manage databases and users that are prefixed with
|
|
|
|
/// either your username, or a group that you are a member of.
|
2024-04-20 04:17:16 +02:00
|
|
|
#[derive(Parser)]
|
2024-04-21 06:03:25 +02:00
|
|
|
#[command(version, about, disable_help_subcommand = true)]
|
2024-04-20 04:17:16 +02:00
|
|
|
enum Command {
|
2024-07-09 19:54:27 +02:00
|
|
|
#[command(flatten)]
|
|
|
|
Db(cli::database_command::DatabaseCommand),
|
|
|
|
|
|
|
|
#[command(flatten)]
|
|
|
|
User(cli::user_command::UserCommand),
|
2024-04-20 04:17:16 +02:00
|
|
|
}
|
|
|
|
|
2024-08-07 16:45:24 +02:00
|
|
|
#[tokio::main(flavor = "current_thread")]
|
2024-04-21 06:03:25 +02:00
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
env_logger::init();
|
2024-08-05 22:37:23 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "mysql-admutils-compatibility")]
|
|
|
|
{
|
|
|
|
let argv0 = std::env::args().next().and_then(|s| {
|
|
|
|
PathBuf::from(s)
|
|
|
|
.file_name()
|
|
|
|
.map(|s| s.to_string_lossy().to_string())
|
|
|
|
});
|
|
|
|
|
|
|
|
match argv0.as_deref() {
|
|
|
|
Some("mysql-dbadm") => return mysql_dbadm::main().await,
|
|
|
|
Some("mysql-useradm") => return mysql_useradm::main().await,
|
|
|
|
_ => { /* fall through */ }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-20 04:17:16 +02:00
|
|
|
let args: Args = Args::parse();
|
2024-04-21 06:03:25 +02:00
|
|
|
let config = core::config::get_config(args.config_overrides)?;
|
|
|
|
let connection = core::config::mysql_connection_from_config(config).await?;
|
|
|
|
|
2024-08-07 16:54:26 +02:00
|
|
|
let result = match args.command {
|
2024-07-09 19:54:27 +02:00
|
|
|
Command::Db(command) => cli::database_command::handle_command(command, connection).await,
|
2024-04-21 06:03:25 +02:00
|
|
|
Command::User(user_args) => cli::user_command::handle_command(user_args, connection).await,
|
2024-08-07 16:54:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => println!("Changes committed to database"),
|
|
|
|
Err(_) => println!("Changes reverted due to error"),
|
2024-04-20 04:17:16 +02:00
|
|
|
}
|
2024-08-07 16:54:26 +02:00
|
|
|
|
|
|
|
result
|
2024-04-26 00:30:32 +02:00
|
|
|
}
|