Files
dockermcmgr/src/main.rs
2026-06-12 18:32:52 +02:00

93 lines
3.5 KiB
Rust

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.rs :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/29 21:22:17 by tomoron #+# #+# */
/* Updated: 2026/06/12 18:32:19 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
mod minecraft;
use minecraft::mc_socket_listen;
use diesel::{prelude::*, r2d2::{ConnectionManager, Pool}};
pub mod schema;
pub mod models;
pub mod status;
pub mod srvmgr;
mod cli;
use cli::*;
mod config;
pub use config::Config;
use clap::Command;
pub type DbPool = Pool<ConnectionManager<SqliteConnection>>;
use bollard::Docker;
mod migrations;
fn get_connection_pool() -> DbPool {
let database_url = "sqlite://".to_string() + &Config::load().config_path + "/db.sqlite";
let manager = ConnectionManager::<SqliteConnection>::new(&database_url);
Pool::builder()
.build(manager)
.unwrap_or_else(|e| panic!("Error creating pool for {}\nerror: {:?}", database_url, e))
}
fn cli() -> Command {
Command::new("dmm")
.about("Docker Minecraft server Manager cli")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
Command::new("serverMode")
.about("Start the server manager")
)
.subcommand(add::create_subcommand())
.subcommand(remove::create_subcommand())
.subcommand(edit::create_subcommand())
.subcommand(ls::create_subcommand())
.subcommand(set_state::create_subcommand())
.subcommand(start::create_subcommand())
.subcommand(stop::create_subcommand())
.subcommand(archive::create_subcommand())
}
#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<(), String> {
let arg = cli().get_matches();
let pool = get_connection_pool();
migrations::run_migrations(&pool).map_err(|e| format!("Failed to run migrations : {}", e))?;
let docker = Docker::connect_with_defaults().expect("Failed to connect to local docker");
match arg.subcommand() {
Some(("serverMode", _)) => { mc_socket_listen(pool, docker).await.map_err(|_| "socket error".to_string())?; },
Some(("add", sub_matches)) => { add::subcommand(sub_matches, pool).await?; },
Some(("remove", sub_matches)) => { remove::subcommand(sub_matches, &pool, &docker).await?; },
Some(("ls", sub_matches)) => { ls::subcommand(sub_matches, pool).await?; },
Some(("edit", sub_matches)) => { edit::subcommand(sub_matches, pool).await?; },
Some(("setState", sub_matches)) => { set_state::subcommand(sub_matches, pool).await? },
Some(("start", sub_matches)) => { start::subcommand(sub_matches, &pool, &docker).await?; },
Some(("stop", sub_matches)) => { stop::subcommand(sub_matches, &pool, &docker).await?; },
Some(("archive", sub_matches)) => { archive::subcommand(sub_matches, &pool, &docker).await? },
_ => {panic!("subcommand not implemented")}
}
Ok(())
}